diff --git a/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs index badfe7c92..85ea8bf52 100644 --- a/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs +++ b/src/NzbDrone.Core.Test/ParserTests/ParseUtilFixture.cs @@ -63,5 +63,22 @@ namespace NzbDrone.Core.Test.ParserTests { ParseUtil.GetLongFromString(original).Should().Be(parsedInt); } + + [TestCase("tt0183790", "tt0183790")] + [TestCase("0183790", "tt0183790")] + [TestCase("183790", "tt0183790")] + [TestCase("tt10001870", "tt10001870")] + [TestCase("10001870", "tt10001870")] + [TestCase("tt", null)] + [TestCase("tt0", null)] + [TestCase("abc", null)] + [TestCase("abc0", null)] + [TestCase("0", null)] + [TestCase("", null)] + [TestCase(null, null)] + public void should_parse_full_imdb_id_from_string(string input, string expected) + { + ParseUtil.GetFullImdbId(input).Should().Be(expected); + } } } diff --git a/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs b/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs index 49fce0ba3..a98a981f3 100644 --- a/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs +++ b/src/NzbDrone.Core/IndexerSearch/ReleaseSearchService.cs @@ -57,7 +57,7 @@ namespace NzbDrone.Core.IndexerSearch { var searchSpec = Get(request, indexerIds, interactiveSearch); - var imdbId = ParseUtil.GetImdbID(request.imdbid); + var imdbId = ParseUtil.GetImdbId(request.imdbid); searchSpec.ImdbId = imdbId?.ToString("D7"); searchSpec.TmdbId = request.tmdbid; @@ -87,7 +87,7 @@ namespace NzbDrone.Core.IndexerSearch { var searchSpec = Get(request, indexerIds, interactiveSearch); - var imdbId = ParseUtil.GetImdbID(request.imdbid); + var imdbId = ParseUtil.GetImdbId(request.imdbid); searchSpec.ImdbId = imdbId?.ToString("D7"); searchSpec.Season = request.season; diff --git a/src/NzbDrone.Core/Indexers/Definitions/Anthelion.cs b/src/NzbDrone.Core/Indexers/Definitions/Anthelion.cs index f49041e6b..429c24cd2 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Anthelion.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Anthelion.cs @@ -246,7 +246,7 @@ namespace NzbDrone.Core.Indexers.Definitions // TODO: TMDb is also available var qImdb = row.QuerySelector("a[href^=\"https://www.imdb.com\"]"); - var imdb = qImdb != null ? ParseUtil.GetImdbID(qImdb.GetAttribute("href").Split('/').Last()) : null; + var imdb = qImdb != null ? ParseUtil.GetImdbId(qImdb.GetAttribute("href").Split('/').Last()) : null; var release = new TorrentInfo { diff --git a/src/NzbDrone.Core/Indexers/Definitions/Avistaz/AvistazParserBase.cs b/src/NzbDrone.Core/Indexers/Definitions/Avistaz/AvistazParserBase.cs index 64ccdc959..68b2d745c 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/Avistaz/AvistazParserBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/Avistaz/AvistazParserBase.cs @@ -75,7 +75,7 @@ namespace NzbDrone.Core.Indexers.Definitions.Avistaz if (row.MovieTvinfo != null) { - release.ImdbId = ParseUtil.GetImdbID(row.MovieTvinfo.Imdb).GetValueOrDefault(); + release.ImdbId = ParseUtil.GetImdbId(row.MovieTvinfo.Imdb).GetValueOrDefault(); release.TmdbId = row.MovieTvinfo.Tmdb.IsNullOrWhiteSpace() ? 0 : ParseUtil.TryCoerceInt(row.MovieTvinfo.Tmdb, out var tmdbResult) ? tmdbResult : 0; release.TvdbId = row.MovieTvinfo.Tvdb.IsNullOrWhiteSpace() ? 0 : ParseUtil.TryCoerceInt(row.MovieTvinfo.Tvdb, out var tvdbResult) ? tvdbResult : 0; } diff --git a/src/NzbDrone.Core/Indexers/Definitions/BeyondHD.cs b/src/NzbDrone.Core/Indexers/Definitions/BeyondHD.cs index d25a358b5..b1e46428d 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/BeyondHD.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/BeyondHD.cs @@ -209,7 +209,7 @@ namespace NzbDrone.Core.Indexers.Definitions // BHD can return crazy values for tmdb var tmdbId = row.TmdbId.IsNullOrWhiteSpace() ? 0 : ParseUtil.TryCoerceInt(row.TmdbId.Split("/")[1], out var tmdbResult) ? tmdbResult : 0; - var imdbId = ParseUtil.GetImdbID(row.ImdbId).GetValueOrDefault(); + var imdbId = ParseUtil.GetImdbId(row.ImdbId).GetValueOrDefault(); var release = new TorrentInfo { diff --git a/src/NzbDrone.Core/Indexers/Definitions/GreatPosterWall.cs b/src/NzbDrone.Core/Indexers/Definitions/GreatPosterWall.cs index a6207d455..ace0e0829 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/GreatPosterWall.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/GreatPosterWall.cs @@ -192,7 +192,7 @@ public class GreatPosterWallParser : GazelleParser MinimumSeedTime = 172800 // 48 hours }; - var imdbId = ParseUtil.GetImdbID(result.ImdbId); + var imdbId = ParseUtil.GetImdbId(result.ImdbId); if (imdbId != null) { release.ImdbId = (int)imdbId; diff --git a/src/NzbDrone.Core/Indexers/Definitions/HDBits/HDBitsRequestGenerator.cs b/src/NzbDrone.Core/Indexers/Definitions/HDBits/HDBitsRequestGenerator.cs index b31861381..0122a296e 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/HDBits/HDBitsRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/HDBits/HDBitsRequestGenerator.cs @@ -20,7 +20,7 @@ namespace NzbDrone.Core.Indexers.Definitions.HDBits { var pageableRequests = new IndexerPageableRequestChain(); var query = new TorrentQuery(); - var imdbId = ParseUtil.GetImdbID(searchCriteria.ImdbId).GetValueOrDefault(0); + var imdbId = ParseUtil.GetImdbId(searchCriteria.ImdbId).GetValueOrDefault(0); if (searchCriteria.Categories?.Length > 0) { @@ -78,7 +78,7 @@ namespace NzbDrone.Core.Indexers.Definitions.HDBits var pageableRequests = new IndexerPageableRequestChain(); var query = new TorrentQuery(); var tvdbId = searchCriteria.TvdbId.GetValueOrDefault(0); - var imdbId = ParseUtil.GetImdbID(searchCriteria.ImdbId).GetValueOrDefault(0); + var imdbId = ParseUtil.GetImdbId(searchCriteria.ImdbId).GetValueOrDefault(0); if (searchCriteria.Categories?.Length > 0) { diff --git a/src/NzbDrone.Core/Indexers/Definitions/HDSpace.cs b/src/NzbDrone.Core/Indexers/Definitions/HDSpace.cs index 0bac28dfd..6d43ff37a 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/HDSpace.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/HDSpace.cs @@ -282,7 +282,7 @@ namespace NzbDrone.Core.Indexers.Definitions var imdbLink = row.QuerySelector("td:nth-child(2) a[href*=imdb]"); if (imdbLink != null) { - release.ImdbId = ParseUtil.GetImdbID(imdbLink.GetAttribute("href").Split('/').Last()).GetValueOrDefault(); + release.ImdbId = ParseUtil.GetImdbId(imdbLink.GetAttribute("href").Split('/').Last()).GetValueOrDefault(); } //"July 11, 2015, 13:34:09", "Today|Yesterday at 20:04:23" diff --git a/src/NzbDrone.Core/Indexers/Definitions/HDTorrents.cs b/src/NzbDrone.Core/Indexers/Definitions/HDTorrents.cs index 958a03d74..ba3f1859d 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/HDTorrents.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/HDTorrents.cs @@ -328,7 +328,7 @@ namespace NzbDrone.Core.Indexers.Definitions } var imdbLink = row.QuerySelector("a[href*=\"www.imdb.com/title/\"]")?.GetAttribute("href"); - var imdb = !string.IsNullOrWhiteSpace(imdbLink) ? ParseUtil.GetImdbID(imdbLink) : null; + var imdb = !string.IsNullOrWhiteSpace(imdbLink) ? ParseUtil.GetImdbId(imdbLink) : null; var flags = new HashSet(); diff --git a/src/NzbDrone.Core/Indexers/Definitions/NorBits.cs b/src/NzbDrone.Core/Indexers/Definitions/NorBits.cs index 877f0459c..470e819ec 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/NorBits.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/NorBits.cs @@ -313,7 +313,7 @@ public class NorBitsParser : IParseIndexerResponse } var imdbLink = row.QuerySelector("a[href*=\"imdb.com/title/tt\"]")?.GetAttribute("href"); - release.ImdbId = ParseUtil.GetImdbID(imdbLink) ?? 0; + release.ImdbId = ParseUtil.GetImdbId(imdbLink) ?? 0; if (row.QuerySelector("img[title=\"100% freeleech\"]") != null) { diff --git a/src/NzbDrone.Core/Indexers/Definitions/PixelHD.cs b/src/NzbDrone.Core/Indexers/Definitions/PixelHD.cs index b5062cb96..83173fc1b 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/PixelHD.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/PixelHD.cs @@ -180,7 +180,7 @@ public class PixelHDParser : IParseIndexerResponse var groupName = group.QuerySelector("strong:has(a[title=\"View Torrent\"])")?.TextContent.Replace(" ]", "]"); var imdbLink = group.QuerySelector("a[href*=\"imdb.com/title/tt\"]")?.GetAttribute("href"); - var imdbId = ParseUtil.GetImdbID(imdbLink) ?? 0; + var imdbId = ParseUtil.GetImdbId(imdbLink) ?? 0; var rows = group.QuerySelectorAll("tr.group_torrent:has(a[href^=\"torrents.php?id=\"])"); foreach (var row in rows) diff --git a/src/NzbDrone.Core/Indexers/Definitions/RevolutionTT.cs b/src/NzbDrone.Core/Indexers/Definitions/RevolutionTT.cs index 08b0ba572..b966a599f 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/RevolutionTT.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/RevolutionTT.cs @@ -286,7 +286,7 @@ namespace NzbDrone.Core.Indexers.Definitions var category = row.QuerySelector(".br_type > a").GetAttribute("href").Replace("browse.php?cat=", string.Empty); var qImdb = row.QuerySelector("a[href*=\"www.imdb.com/\"]"); - var imdb = qImdb != null ? ParseUtil.GetImdbID(qImdb.GetAttribute("href").Split('/').Last()) : null; + var imdb = qImdb != null ? ParseUtil.GetImdbId(qImdb.GetAttribute("href").Split('/').Last()) : null; var release = new TorrentInfo { diff --git a/src/NzbDrone.Core/Indexers/Definitions/SceneHD.cs b/src/NzbDrone.Core/Indexers/Definitions/SceneHD.cs index 651cd280d..c83d8a3ae 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/SceneHD.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/SceneHD.cs @@ -213,7 +213,7 @@ namespace NzbDrone.Core.Indexers.Definitions Files = item.Value("numfiles"), Seeders = item.Value("seeders"), Peers = item.Value("leechers") + item.Value("seeders"), - ImdbId = ParseUtil.GetImdbID(item.Value("imdbid")) ?? 0, + ImdbId = ParseUtil.GetImdbId(item.Value("imdbid")) ?? 0, MinimumRatio = 1, MinimumSeedTime = 0, DownloadVolumeFactor = dlVolumeFactor, diff --git a/src/NzbDrone.Core/Indexers/Definitions/SpeedApp/SpeedAppBase.cs b/src/NzbDrone.Core/Indexers/Definitions/SpeedApp/SpeedAppBase.cs index 942e275a0..abbab8399 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/SpeedApp/SpeedAppBase.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/SpeedApp/SpeedAppBase.cs @@ -262,7 +262,7 @@ namespace NzbDrone.Core.Indexers.Definitions Title = CleanTitle(torrent.Name), Description = torrent.ShortDescription, Size = torrent.Size, - ImdbId = ParseUtil.GetImdbID(torrent.ImdbId).GetValueOrDefault(), + ImdbId = ParseUtil.GetImdbId(torrent.ImdbId).GetValueOrDefault(), DownloadUrl = $"{_settings.BaseUrl}api/torrent/{torrent.Id}/download", PosterUrl = torrent.Poster, InfoUrl = torrent.Url, diff --git a/src/NzbDrone.Core/Indexers/Definitions/TorrentDay.cs b/src/NzbDrone.Core/Indexers/Definitions/TorrentDay.cs index 0d48be5fa..81d900f74 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/TorrentDay.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/TorrentDay.cs @@ -244,7 +244,7 @@ namespace NzbDrone.Core.Indexers.Definitions var downloadMultiplier = (double?)row["download-multiplier"] ?? 1; var link = new Uri(_settings.BaseUrl + "download.php/" + torrentId + "/" + torrentId + ".torrent"); var publishDate = DateTimeUtil.UnixTimestampToDateTime((long)row.ctime).ToLocalTime(); - var imdb = ParseUtil.GetImdbID(imdbId) ?? 0; + var imdb = ParseUtil.GetImdbId(imdbId) ?? 0; var release = new TorrentInfo { diff --git a/src/NzbDrone.Core/Indexers/Definitions/UNIT3D/Unit3dParser.cs b/src/NzbDrone.Core/Indexers/Definitions/UNIT3D/Unit3dParser.cs index 46d305e92..572bec8d7 100644 --- a/src/NzbDrone.Core/Indexers/Definitions/UNIT3D/Unit3dParser.cs +++ b/src/NzbDrone.Core/Indexers/Definitions/UNIT3D/Unit3dParser.cs @@ -60,7 +60,7 @@ namespace NzbDrone.Core.Indexers.Definitions.UNIT3D Files = row.Attributes.Files, Grabs = row.Attributes.Grabs, Seeders = row.Attributes.Seeders, - ImdbId = ParseUtil.GetImdbID(row.Attributes.ImdbId).GetValueOrDefault(), + ImdbId = ParseUtil.GetImdbId(row.Attributes.ImdbId).GetValueOrDefault(), TmdbId = row.Attributes.TmdbId.IsNullOrWhiteSpace() ? 0 : ParseUtil.CoerceInt(row.Attributes.TmdbId), TvdbId = row.Attributes.TvdbId.IsNullOrWhiteSpace() ? 0 : ParseUtil.CoerceInt(row.Attributes.TvdbId), Peers = row.Attributes.Leechers + row.Attributes.Seeders, diff --git a/src/NzbDrone.Core/Parser/ParseUtil.cs b/src/NzbDrone.Core/Parser/ParseUtil.cs index 0b21a8575..ea80afb20 100644 --- a/src/NzbDrone.Core/Parser/ParseUtil.cs +++ b/src/NzbDrone.Core/Parser/ParseUtil.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Core.Parser { public static class ParseUtil { - private static readonly Regex ImdbId = new Regex(@"^(?:tt)?(\d{1,8})$", RegexOptions.Compiled); + private static readonly Regex ImdbIdRegex = new (@"^(?:tt)?(\d{1,8})$", RegexOptions.Compiled); public static string NormalizeMultiSpaces(string s) => new Regex(@"\s+").Replace(s.Trim(), " "); @@ -85,14 +85,15 @@ namespace NzbDrone.Core.Parser return CoerceLong(extractedLong); } - public static int? GetImdbID(string imdbstr) + public static int? GetImdbId(string value) { - if (imdbstr == null) + if (value == null) { return null; } - var match = ImdbId.Match(imdbstr); + var match = ImdbIdRegex.Match(value); + if (!match.Success) { return null; @@ -101,17 +102,16 @@ namespace NzbDrone.Core.Parser return int.Parse(match.Groups[1].Value, NumberStyles.Any, CultureInfo.InvariantCulture); } - public static string GetFullImdbId(string imdbstr) + public static string GetFullImdbId(string value) { - var imdbid = GetImdbID(imdbstr); - if (imdbid == null) + var imdbId = GetImdbId(value); + + if (imdbId is null or 0) { return null; } - var imdbLen = ((int)imdbid > 9999999) ? "D8" : "D7"; - - return "tt" + ((int)imdbid).ToString(imdbLen); + return $"tt{imdbId.GetValueOrDefault():D7}"; } public static string GetArgumentFromQueryString(string url, string argument)