From 8f0c872873f27780cde6fe0240949f531a2b71fb Mon Sep 17 00:00:00 2001 From: Qstick Date: Tue, 29 Dec 2020 23:06:41 -0500 Subject: [PATCH] Parse and Store App from UA for Nab Requests Stats --- src/NzbDrone.Core/History/HistoryService.cs | 1 + .../Definitions/SearchCriteriaBase.cs | 1 + .../IndexerSearch/NewznabRequest.cs | 1 + .../IndexerSearch/NzbSearchService.cs | 1 + src/NzbDrone.Core/Parser/UserAgentParser.cs | 21 +++++++++++++++++++ src/Prowlarr.Api.V1/Indexers/IndexerModule.cs | 2 ++ 6 files changed, 27 insertions(+) create mode 100644 src/NzbDrone.Core/Parser/UserAgentParser.cs diff --git a/src/NzbDrone.Core/History/HistoryService.cs b/src/NzbDrone.Core/History/HistoryService.cs index c65107c55..c04acc0e5 100644 --- a/src/NzbDrone.Core/History/HistoryService.cs +++ b/src/NzbDrone.Core/History/HistoryService.cs @@ -92,6 +92,7 @@ namespace NzbDrone.Core.History history.Data.Add("ElapsedTime", message.Time.ToString()); history.Data.Add("Query", message.Query.SearchTerm ?? string.Empty); history.Data.Add("Categories", string.Join(",", message.Query.Categories) ?? string.Empty); + history.Data.Add("Source", message.Query.Source ?? string.Empty); history.Data.Add("Successful", message.Successful.ToString()); history.Data.Add("QueryResults", message.Results.HasValue ? message.Results.ToString() : null); diff --git a/src/NzbDrone.Core/IndexerSearch/Definitions/SearchCriteriaBase.cs b/src/NzbDrone.Core/IndexerSearch/Definitions/SearchCriteriaBase.cs index bf189a728..a48dba735 100644 --- a/src/NzbDrone.Core/IndexerSearch/Definitions/SearchCriteriaBase.cs +++ b/src/NzbDrone.Core/IndexerSearch/Definitions/SearchCriteriaBase.cs @@ -19,5 +19,6 @@ namespace NzbDrone.Core.IndexerSearch.Definitions public string SearchType { get; set; } public int? Limit { get; set; } public int? Offset { get; set; } + public string Source { get; set; } } } diff --git a/src/NzbDrone.Core/IndexerSearch/NewznabRequest.cs b/src/NzbDrone.Core/IndexerSearch/NewznabRequest.cs index 0cdae0c8b..fa29b359b 100644 --- a/src/NzbDrone.Core/IndexerSearch/NewznabRequest.cs +++ b/src/NzbDrone.Core/IndexerSearch/NewznabRequest.cs @@ -26,5 +26,6 @@ namespace NzbDrone.Core.IndexerSearch public string author { get; set; } public string title { get; set; } public string configured { get; set; } + public string source { get; set; } } } diff --git a/src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs b/src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs index d7405d97c..7233eb6d9 100644 --- a/src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs +++ b/src/NzbDrone.Core/IndexerSearch/NzbSearchService.cs @@ -121,6 +121,7 @@ namespace NzbDrone.Core.IndexerSearch spec.SearchType = query.t; spec.Limit = query.limit; spec.Offset = query.offset; + spec.Source = query.source; spec.IndexerIds = indexerIds; diff --git a/src/NzbDrone.Core/Parser/UserAgentParser.cs b/src/NzbDrone.Core/Parser/UserAgentParser.cs new file mode 100644 index 000000000..13e33e980 --- /dev/null +++ b/src/NzbDrone.Core/Parser/UserAgentParser.cs @@ -0,0 +1,21 @@ +using System.Text.RegularExpressions; + +namespace NzbDrone.Core.Parser +{ + public static class UserAgentParser + { + 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) + { + return match.Groups["agent"].Value; + } + + return "Other"; + } + } +} diff --git a/src/Prowlarr.Api.V1/Indexers/IndexerModule.cs b/src/Prowlarr.Api.V1/Indexers/IndexerModule.cs index 4bafcc69b..2cac4e7a6 100644 --- a/src/Prowlarr.Api.V1/Indexers/IndexerModule.cs +++ b/src/Prowlarr.Api.V1/Indexers/IndexerModule.cs @@ -4,6 +4,7 @@ using Nancy.ModelBinding; using NzbDrone.Common.Extensions; using NzbDrone.Core.Indexers; using NzbDrone.Core.IndexerSearch; +using NzbDrone.Core.Parser; using Prowlarr.Http.REST; namespace Prowlarr.Api.V1.Indexers @@ -41,6 +42,7 @@ namespace Prowlarr.Api.V1.Indexers private object GetNewznabResponse(NewznabRequest request) { var requestType = request.t; + request.source = UserAgentParser.ParseSource(Request.Headers.UserAgent); if (requestType.IsNullOrWhiteSpace()) {