From 66f5fd2a261105b57e656f0777dc7dec0af56dd4 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 29 Apr 2023 18:29:33 +0300 Subject: [PATCH] Fixed: (BroadcastheNet) Add tests & prefer TvdbId --- .../BroadcastheNetRequestGeneratorFixture.cs | 296 ++++++++++++++++++ .../BroadcastheNetRequestGenerator.cs | 10 +- 2 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetRequestGeneratorFixture.cs diff --git a/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetRequestGeneratorFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetRequestGeneratorFixture.cs new file mode 100644 index 000000000..b5962af1e --- /dev/null +++ b/src/NzbDrone.Core.Test/IndexerTests/BroadcastheNetTests/BroadcastheNetRequestGeneratorFixture.cs @@ -0,0 +1,296 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using DryIoc; +using FluentAssertions; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using NUnit.Framework; +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Indexers; +using NzbDrone.Core.Indexers.BroadcastheNet; +using NzbDrone.Core.Indexers.Definitions.HDBits; +using NzbDrone.Core.IndexerSearch.Definitions; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.IndexerTests.BroadcastheNetTests +{ + public class BroadcastheNetRequestGeneratorFixture : CoreTest + { + [SetUp] + public void Setup() + { + Subject.Settings = new BroadcastheNetSettings + { + BaseUrl = "https://api.broadcasthe.net/", + ApiKey = "abc" + }; + + Subject.Capabilities = new IndexerCapabilities + { + LimitsDefault = 100, + LimitsMax = 1000, + TvSearchParams = new List + { + TvSearchParam.Q, TvSearchParam.Season, TvSearchParam.Ep, TvSearchParam.TvdbId, TvSearchParam.RId + } + }; + } + + [Test] + public void should_have_empty_parameters_if_rss_search() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id } + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().BeNull(); + query.Tvrage.Should().BeNull(); + query.Search.Should().BeNullOrWhiteSpace(); + query.Category.Should().BeNullOrWhiteSpace(); + query.Name.Should().BeNullOrWhiteSpace(); + } + + [Test] + public void should_search_by_tvdbid_season_if_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + TvdbId = 371980, + Season = 1 + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(2); + + var firstPage = results.GetAllTiers().First().First(); + var firstQuery = ParseTorrentQueryFromRequest(firstPage.HttpRequest); + + firstQuery.Tvdb.Should().Be("371980"); + firstQuery.Tvrage.Should().BeNull(); + firstQuery.Search.Should().BeNull(); + firstQuery.Category.Should().Be("Season"); + firstQuery.Name.Should().Be("Season 1%"); + + var secondPage = results.GetAllTiers().Skip(1).First().First(); + var secondQuery = ParseTorrentQueryFromRequest(secondPage.HttpRequest); + + secondQuery.Tvdb.Should().Be("371980"); + secondQuery.Tvrage.Should().BeNull(); + secondQuery.Search.Should().BeNull(); + secondQuery.Category.Should().Be("Episode"); + secondQuery.Name.Should().Be("S01E%"); + } + + [Test] + public void should_search_by_tvdbid_season_episode_if_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + TvdbId = 371980, + Season = 1, + Episode = "3" + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().Be("371980"); + query.Tvrage.Should().BeNull(); + query.Search.Should().BeNull(); + query.Category.Should().Be("Episode"); + query.Name.Should().Be("S01E03"); + } + + [Test] + public void should_search_by_tvdbid_daily_episode_if_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + TvdbId = 289574, + Season = 2023, + Episode = "01/03" + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().Be("289574"); + query.Tvrage.Should().BeNull(); + query.Search.Should().BeNull(); + query.Category.Should().Be("Episode"); + query.Name.Should().Be("2023.01.03"); + } + + [Test] + public void should_prefer_search_by_tvdbid_if_rid_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + TvdbId = 371980, + RId = 12345 + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().Be("371980"); + query.Tvrage.Should().BeNull(); + query.Search.Should().BeNull(); + query.Category.Should().BeNull(); + query.Name.Should().BeNull(); + } + + [Test] + public void should_search_by_term_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + SearchTerm = "Malcolm in the Middle" + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().BeNull(); + query.Tvrage.Should().BeNull(); + query.Search.Should().Be("Malcolm%in%the%Middle"); + query.Category.Should().BeNull(); + query.Name.Should().BeNull(); + } + + [Test] + public void should_search_by_term_season_if_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + SearchTerm = "Malcolm in the Middle", + Season = 2 + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(2); + + var firstPage = results.GetAllTiers().First().First(); + var firstQuery = ParseTorrentQueryFromRequest(firstPage.HttpRequest); + + firstQuery.Tvdb.Should().BeNull(); + firstQuery.Tvrage.Should().BeNull(); + firstQuery.Search.Should().Be("Malcolm%in%the%Middle"); + firstQuery.Category.Should().Be("Season"); + firstQuery.Name.Should().Be("Season 2%"); + + var secondPage = results.GetAllTiers().Skip(1).First().First(); + var secondQuery = ParseTorrentQueryFromRequest(secondPage.HttpRequest); + + secondQuery.Tvdb.Should().BeNull(); + secondQuery.Tvrage.Should().BeNull(); + secondQuery.Search.Should().Be("Malcolm%in%the%Middle"); + secondQuery.Category.Should().Be("Episode"); + secondQuery.Name.Should().Be("S02E%"); + } + + [Test] + public void should_search_by_term_season_episode_if_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + SearchTerm = "Malcolm in the Middle", + Season = 2, + Episode = "3" + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().BeNull(); + query.Tvrage.Should().BeNull(); + query.Search.Should().Be("Malcolm%in%the%Middle"); + query.Category.Should().Be("Episode"); + query.Name.Should().Be("S02E03"); + } + + [Test] + public void should_search_by_term_daily_episode_if_supported() + { + var tvSearchCriteria = new TvSearchCriteria + { + Categories = new[] { NewznabStandardCategory.TV.Id, NewznabStandardCategory.TVHD.Id }, + SearchTerm = "The Late Show with Stephen Colbert", + Season = 2023, + Episode = "01/03" + }; + + var results = Subject.GetSearchRequests(tvSearchCriteria); + + results.Tiers.Should().Be(1); + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + var query = ParseTorrentQueryFromRequest(page.HttpRequest); + + query.Tvdb.Should().BeNull(); + query.Tvrage.Should().BeNull(); + query.Search.Should().Be("The%Late%Show%with%Stephen%Colbert"); + query.Category.Should().Be("Episode"); + query.Name.Should().Be("2023.01.03"); + } + + private static BroadcastheNetTorrentQuery ParseTorrentQueryFromRequest(HttpRequest httpRequest) + { + var encoding = HttpHeader.GetEncodingFromContentType(httpRequest.Headers.ContentType); + var body = encoding.GetString(httpRequest.ContentData); + + var rpcBody = JsonConvert.DeserializeObject>(body); + + return JsonConvert.DeserializeObject(((JArray)rpcBody["params"])[1].ToJson()); + } + } +} diff --git a/src/NzbDrone.Core/Indexers/Definitions/BroadcastheNet/BroadcastheNetRequestGenerator.cs b/src/NzbDrone.Core/Indexers/Definitions/BroadcastheNet/BroadcastheNetRequestGenerator.cs index a0e3ec1b2..17fc12a44 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/BroadcastheNet/BroadcastheNetRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/BroadcastheNet/BroadcastheNetRequestGenerator.cs @@ -50,7 +50,7 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet var parameters = new BroadcastheNetTorrentQuery(); - var searchString = searchCriteria.SearchTerm ?? ""; + var searchString = searchCriteria.SearchTerm ?? string.Empty; var btnResults = searchCriteria.Limit.GetValueOrDefault(); if (btnResults == 0) @@ -64,11 +64,14 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet { parameters.Tvdb = $"{searchCriteria.TvdbId}"; } - - if (searchCriteria.RId > 0) + else if (searchCriteria.RId > 0) { parameters.Tvrage = $"{searchCriteria.RId}"; } + else if (searchString.IsNotNullOrWhiteSpace()) + { + parameters.Search = searchString.Replace(" ", "%"); + } // If only the season/episode is searched for then change format to match expected format if (searchCriteria.Season > 0 && searchCriteria.Episode.IsNullOrWhiteSpace()) @@ -102,7 +105,6 @@ namespace NzbDrone.Core.Indexers.BroadcastheNet else { // Neither a season only search nor daily nor standard, fall back to query - parameters.Search = searchString.Replace(" ", "%"); pageableRequests.Add(GetPagedRequests(parameters, btnResults, btnOffset)); }