mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
core: Remove reflection in Nullable TryParse extension (#7844)
This commit is contained in:
@@ -29,6 +29,10 @@ namespace Jackett.Common.Indexers.Feeds
|
||||
protected virtual ReleaseInfo ResultFromFeedItem(XElement item)
|
||||
{
|
||||
var attributes = item.Descendants().Where(e => e.Name.LocalName == "attr");
|
||||
var size = long.TryParse(ReadAttribute(attributes, "size"), out var longVal ) ? (long?)longVal : null;
|
||||
var files = long.TryParse(ReadAttribute(attributes, "files"), out longVal) ? (long?)longVal : null;
|
||||
var seeders = int.TryParse(ReadAttribute(attributes, "seeders"), out var intVal) ? (int?)intVal : null;
|
||||
var peers = int.TryParse(ReadAttribute(attributes, "peers"), out intVal) ? (int?)intVal : null;
|
||||
var release = new ReleaseInfo
|
||||
{
|
||||
Title = item.FirstValue("title"),
|
||||
@@ -37,11 +41,11 @@ namespace Jackett.Common.Indexers.Feeds
|
||||
Comments = new Uri(item.FirstValue("comments")),
|
||||
PublishDate = DateTime.Parse(item.FirstValue("pubDate")),
|
||||
Category = new List<int> { int.Parse(attributes.First(e => e.Attribute("name").Value == "category").Attribute("value").Value) },
|
||||
Size = ReadAttribute(attributes, "size").TryParse<long>(),
|
||||
Files = ReadAttribute(attributes, "files").TryParse<long>(),
|
||||
Size = size,
|
||||
Files = files,
|
||||
Description = item.FirstValue("description"),
|
||||
Seeders = ReadAttribute(attributes, "seeders").TryParse<int>(),
|
||||
Peers = ReadAttribute(attributes, "peers").TryParse<int>(),
|
||||
Seeders = seeders,
|
||||
Peers = peers,
|
||||
InfoHash = attributes.First(e => e.Attribute("name").Value == "infohash").Attribute("value").Value,
|
||||
MagnetUri = new Uri(attributes.First(e => e.Attribute("name").Value == "magneturl").Attribute("value").Value),
|
||||
};
|
||||
|
@@ -76,34 +76,6 @@ namespace Jackett.Common.Utils
|
||||
public static IDictionary<Key, Value> ToDictionary<Key, Value>(this IEnumerable<KeyValuePair<Key, Value>> pairs) => pairs.ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
|
||||
public static class ParseExtension
|
||||
{
|
||||
public static T? TryParse<T>(this string value) where T : struct
|
||||
{
|
||||
var type = typeof(T);
|
||||
var parseMethods = type.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static).Where(m => m.Name == "Parse");
|
||||
var parseMethod = parseMethods.Where(m =>
|
||||
{
|
||||
var parameters = m.GetParameters();
|
||||
var hasOnlyOneParameter = parameters.Count() == 1;
|
||||
var firstParameterIsString = parameters.First().ParameterType == typeof(string);
|
||||
|
||||
return hasOnlyOneParameter && firstParameterIsString;
|
||||
}).First();
|
||||
if (parseMethod == null)
|
||||
return null;
|
||||
try
|
||||
{
|
||||
var val = parseMethod.Invoke(null, new object[] { value });
|
||||
return (T)val;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class TaskExtensions
|
||||
{
|
||||
public static Task<IEnumerable<TResult>> Until<TResult>(this IEnumerable<Task<TResult>> tasks, TimeSpan timeout)
|
||||
|
Reference in New Issue
Block a user