From c694d1ef4470f6e4245e9d946f59f21b22fc158b Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 31 May 2021 00:17:05 -0400 Subject: [PATCH] Cleanup UA Parser --- src/NzbDrone.Common/Http/UserAgentParser.cs | 18 +++++++++++++- src/NzbDrone.Core/Parser/UserAgentParser.cs | 27 --------------------- 2 files changed, 17 insertions(+), 28 deletions(-) delete mode 100644 src/NzbDrone.Core/Parser/UserAgentParser.cs diff --git a/src/NzbDrone.Common/Http/UserAgentParser.cs b/src/NzbDrone.Common/Http/UserAgentParser.cs index 2c382236a..e1164b9e8 100644 --- a/src/NzbDrone.Common/Http/UserAgentParser.cs +++ b/src/NzbDrone.Common/Http/UserAgentParser.cs @@ -1,13 +1,17 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace NzbDrone.Common.Http { public static class UserAgentParser { + private static readonly Regex AppSourceRegex = new Regex(@"(?.*)\/.*(\(.*\))?", + RegexOptions.IgnoreCase | RegexOptions.Compiled); + public static string SimplifyUserAgent(string userAgent) { if (userAgent == null || userAgent.StartsWith("Mozilla/5.0")) @@ -17,5 +21,17 @@ namespace NzbDrone.Common.Http return userAgent; } + + public static string ParseSource(string userAgent) + { + var match = AppSourceRegex.Match(SimplifyUserAgent(userAgent) ?? string.Empty); + + if (match.Groups["agent"].Success) + { + return match.Groups["agent"].Value; + } + + return "Other"; + } } } diff --git a/src/NzbDrone.Core/Parser/UserAgentParser.cs b/src/NzbDrone.Core/Parser/UserAgentParser.cs deleted file mode 100644 index dd82df4f8..000000000 --- a/src/NzbDrone.Core/Parser/UserAgentParser.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Collections.Generic; -using System.Text.RegularExpressions; - -namespace NzbDrone.Core.Parser -{ - public static class UserAgentParser - { - private static readonly List UserAgentWhiteList = new List { "Radarr", "Sonarr", "Readarr", "Lidarr", "Prowlarr" }; - - private static readonly Regex AppSourceRegex = new Regex(@"(?.*)\/.*(\(.*\))?", - RegexOptions.IgnoreCase | RegexOptions.Compiled); - public static string ParseSource(string userAgent) - { - var match = AppSourceRegex.Match(userAgent); - - if (match.Groups["agent"].Success) - { - if (UserAgentWhiteList.Contains(match.Groups["agent"].Value)) - { - return match.Groups["agent"].Value; - } - } - - return "Other"; - } - } -}