From 7fe9942c2872cd2235157ac31729e4916ce46b19 Mon Sep 17 00:00:00 2001 From: SetekhZ Date: Sun, 28 May 2023 18:09:59 +0300 Subject: [PATCH] Fixed: (RuTracker) Use supported 200 categories per search request --- .../Indexers/Definitions/RuTracker.cs | 111 +++++++++--------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/src/NzbDrone.Core/Indexers/Definitions/RuTracker.cs b/src/NzbDrone.Core/Indexers/Definitions/RuTracker.cs index 2274a0f8a..5c25f3a47 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/RuTracker.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/RuTracker.cs @@ -1465,7 +1465,54 @@ namespace NzbDrone.Core.Indexers.Definitions _capabilities = capabilities; } - private IEnumerable GetPagedRequests(string term, int[] categories, int season = 0) + public IndexerPageableRequestChain GetSearchRequests(MovieSearchCriteria searchCriteria) + { + return GetPageableRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories); + } + + public IndexerPageableRequestChain GetSearchRequests(MusicSearchCriteria searchCriteria) + { + return GetPageableRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories); + } + + public IndexerPageableRequestChain GetSearchRequests(TvSearchCriteria searchCriteria) + { + return GetPageableRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories, searchCriteria.Season ?? 0); + } + + public IndexerPageableRequestChain GetSearchRequests(BookSearchCriteria searchCriteria) + { + return GetPageableRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories); + } + + public IndexerPageableRequestChain GetSearchRequests(BasicSearchCriteria searchCriteria) + { + return GetPageableRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories); + } + + private IndexerPageableRequestChain GetPageableRequests(string searchTerm, int[] categories, int season = 0) + { + var pageableRequests = new IndexerPageableRequestChain(); + + if (categories is { Length: > 0 }) + { + var trackerCategories = _capabilities.Categories.MapTorznabCapsToTrackers(categories).Distinct().ToList(); + + // RuTracker supports maximum 200 categories in one search + foreach (var trackerCategoriesChunk in trackerCategories.Chunk(200)) + { + pageableRequests.Add(GetPagedRequests(searchTerm, trackerCategoriesChunk, season)); + } + } + else + { + pageableRequests.Add(GetPagedRequests(searchTerm, null, season)); + } + + return pageableRequests; + } + + private IEnumerable GetPagedRequests(string term, string[] trackerCategories, int season = 0) { var parameters = new NameValueCollection(); @@ -1488,9 +1535,9 @@ namespace NzbDrone.Core.Indexers.Definitions parameters.Set("nm", searchString); } - if (categories != null && categories.Length > 0) + if (trackerCategories is { Length: > 0 }) { - parameters.Set("f", string.Join(",", _capabilities.Categories.MapTorznabCapsToTrackers(categories))); + parameters.Set("f", string.Join(",", trackerCategories)); } var searchUrl = $"{_settings.BaseUrl}forum/tracker.php"; @@ -1511,56 +1558,6 @@ namespace NzbDrone.Core.Indexers.Definitions yield return request; } - public IndexerPageableRequestChain GetSearchRequests(MovieSearchCriteria searchCriteria) - { - var pageableRequests = new IndexerPageableRequestChain(); - - pageableRequests.Add(GetPagedRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories)); - - return pageableRequests; - } - - public IndexerPageableRequestChain GetSearchRequests(MusicSearchCriteria searchCriteria) - { - var pageableRequests = new IndexerPageableRequestChain(); - - pageableRequests.Add(GetPagedRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories)); - - return pageableRequests; - } - - public IndexerPageableRequestChain GetSearchRequests(TvSearchCriteria searchCriteria) - { - var pageableRequests = new IndexerPageableRequestChain(); - - if (searchCriteria.Season == null) - { - searchCriteria.Season = 0; - } - - pageableRequests.Add(GetPagedRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories)); - - return pageableRequests; - } - - public IndexerPageableRequestChain GetSearchRequests(BookSearchCriteria searchCriteria) - { - var pageableRequests = new IndexerPageableRequestChain(); - - pageableRequests.Add(GetPagedRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories)); - - return pageableRequests; - } - - public IndexerPageableRequestChain GetSearchRequests(BasicSearchCriteria searchCriteria) - { - var pageableRequests = new IndexerPageableRequestChain(); - - pageableRequests.Add(GetPagedRequests(searchCriteria.SanitizedSearchTerm, searchCriteria.Categories)); - - return pageableRequests; - } - public Func> GetCookies { get; set; } public Action, DateTime?> CookiesUpdater { get; set; } } @@ -1580,7 +1577,7 @@ namespace NzbDrone.Core.Indexers.Definitions public IList ParseResponse(IndexerResponse indexerResponse) { - var torrentInfos = new List(); + var releaseInfos = new List(); var parser = new HtmlParser(); var doc = parser.ParseDocument(indexerResponse.Content); @@ -1591,11 +1588,13 @@ namespace NzbDrone.Core.Indexers.Definitions var release = ParseReleaseRow(row); if (release != null) { - torrentInfos.Add(release); + releaseInfos.Add(release); } } - return torrentInfos.ToArray(); + return releaseInfos + .OrderByDescending(o => o.PublishDate) + .ToArray(); } private TorrentInfo ParseReleaseRow(IElement row)