From 8fbcbf25c3c02b87c58d70497c56d9a1d87f5f39 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 25 Mar 2020 12:56:22 -0500 Subject: [PATCH] core: Remove reflection in Nullable TryParse extension (#7844) --- .../Indexers/Feeds/BaseNewznabIndexer.cs | 12 +++++--- src/Jackett.Common/Utils/Extensions.cs | 28 ------------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/Jackett.Common/Indexers/Feeds/BaseNewznabIndexer.cs b/src/Jackett.Common/Indexers/Feeds/BaseNewznabIndexer.cs index 84678b4c5..f85f39db8 100644 --- a/src/Jackett.Common/Indexers/Feeds/BaseNewznabIndexer.cs +++ b/src/Jackett.Common/Indexers/Feeds/BaseNewznabIndexer.cs @@ -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.Parse(attributes.First(e => e.Attribute("name").Value == "category").Attribute("value").Value) }, - Size = ReadAttribute(attributes, "size").TryParse(), - Files = ReadAttribute(attributes, "files").TryParse(), + Size = size, + Files = files, Description = item.FirstValue("description"), - Seeders = ReadAttribute(attributes, "seeders").TryParse(), - Peers = ReadAttribute(attributes, "peers").TryParse(), + 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), }; diff --git a/src/Jackett.Common/Utils/Extensions.cs b/src/Jackett.Common/Utils/Extensions.cs index 876d7b3fc..1194267d6 100644 --- a/src/Jackett.Common/Utils/Extensions.cs +++ b/src/Jackett.Common/Utils/Extensions.cs @@ -76,34 +76,6 @@ namespace Jackett.Common.Utils public static IDictionary ToDictionary(this IEnumerable> pairs) => pairs.ToDictionary(x => x.Key, x => x.Value); } - public static class ParseExtension - { - public static T? TryParse(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> Until(this IEnumerable> tasks, TimeSpan timeout)