apisearch: parse season/episode only if it's at the end of the query (#14007)

This commit is contained in:
Bogdan 2023-02-13 12:31:37 +02:00 committed by GitHub
parent d7437f2a0f
commit eb63aecf8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 26 deletions

View File

@ -247,7 +247,6 @@ namespace Jackett.Common.Indexers
protected override async Task<IEnumerable<ReleaseInfo>> PerformQuery(TorznabQuery query)
{
var searchString = query.GetQueryString();
var prevCook = CookieHeader + "";
var categoryMapping = MapTorznabCapsToTrackers(query);
@ -261,12 +260,14 @@ namespace Jackett.Common.Indexers
{ "order", GetOrder }
};
var searchString = Regex.Replace(query.GetQueryString(), @"[ -._]+", " ").Trim();
if (query.IsImdbQuery)
{
searchParams.Add("keywords", query.ImdbID);
searchParams.Add("search_type", "t_both");
}
else
else if (!string.IsNullOrWhiteSpace(searchString))
{
searchParams.Add("keywords", searchString);
searchParams.Add("search_type", "t_name");

View File

@ -1,3 +1,4 @@
using System;
using System.Text.RegularExpressions;
using Jackett.Common.Utils;
@ -16,48 +17,53 @@ namespace Jackett.Common.Models.DTO
QueryType = "search"
};
var queryStr = request.Query;
if (queryStr != null)
var queryStr = $"{request.Query}".Trim();
if (!string.IsNullOrWhiteSpace(queryStr))
{
var seasonMatch = Regex.Match(queryStr, @"S(\d{2,4})");
if (seasonMatch.Success)
var seasonEpisodeMatch = Regex.Match(queryStr, @"\bS(\d{2,4})E(\d{2,4}[A-Za-z]?)$");
if (seasonEpisodeMatch.Success)
{
stringQuery.Season = int.Parse(seasonMatch.Groups[1].Value);
queryStr = queryStr.Remove(seasonMatch.Index, seasonMatch.Length);
stringQuery.Season = int.Parse(seasonEpisodeMatch.Groups[1].Value);
stringQuery.Episode = seasonEpisodeMatch.Groups[2].Value.TrimStart('0');
queryStr = queryStr.Remove(seasonEpisodeMatch.Index, seasonEpisodeMatch.Length).Trim();
}
else
{
var episodeMatch = Regex.Match(queryStr, @"\bE(\d{2,4}[A-Za-z]?)$");
if (episodeMatch.Success)
{
stringQuery.Episode = episodeMatch.Groups[1].Value.TrimStart('0');
queryStr = queryStr.Remove(episodeMatch.Index, episodeMatch.Length).Trim();
}
var seasonMatch = Regex.Match(queryStr, @"\bS(\d{2,4})$");
if (seasonMatch.Success)
{
stringQuery.Season = int.Parse(seasonMatch.Groups[1].Value);
queryStr = queryStr.Remove(seasonMatch.Index, seasonMatch.Length).Trim();
}
}
var episodeMatch = Regex.Match(queryStr, @"E(\d{2,4}[A-Za-z]?)");
if (episodeMatch.Success)
{
stringQuery.Episode = episodeMatch.Groups[1].Value.TrimStart(new char[] { '0' });
queryStr = queryStr.Remove(episodeMatch.Index, episodeMatch.Length);
}
queryStr = queryStr.Trim();
}
else
{
queryStr = ""; // empty string search is interpreted as null
}
stringQuery.SearchTerm = queryStr;
stringQuery.Categories = request.Category ?? new int[0];
stringQuery.Categories = request.Category ?? Array.Empty<int>();
// try to build an IMDB Query (tt plus 6 to 8 digits)
if (stringQuery.SanitizedSearchTerm.StartsWith("tt") && stringQuery.SanitizedSearchTerm.Length <= 10)
{
var imdbID = ParseUtil.GetFullImdbID(stringQuery.SanitizedSearchTerm);
TorznabQuery imdbQuery = null;
if (imdbID != null)
var imdbId = ParseUtil.GetFullImdbID(stringQuery.SanitizedSearchTerm);
if (imdbId != null)
{
imdbQuery = new TorznabQuery()
return new TorznabQuery
{
ImdbID = imdbID,
ImdbID = imdbId,
Categories = stringQuery.Categories,
Season = stringQuery.Season,
Episode = stringQuery.Episode,
};
return imdbQuery;
}
}

View File

@ -0,0 +1,32 @@
using Jackett.Common.Models.DTO;
using NUnit.Framework;
namespace Jackett.Test.Common.Models.DTO
{
[TestFixture]
public class ApiSearchTests
{
[TestCase("The.Good.Lord.S01E05.720p.WEB.H264-CAKES", "The.Good.Lord.S01E05.720p.WEB.H264-CAKES", 0, null)]
[TestCase("The.Good.Lord.S01E05.", "The.Good.Lord.S01E05.", 0, null)]
[TestCase("The.Good.Lord.S01E05", "The.Good.Lord. S01E05", 1, "5")]
[TestCase("The Good Lord S01E05", "The Good Lord S01E05", 1, "5")]
[TestCase("The Good Lord S01 E05", "The Good Lord S01E05", 1, "5")]
[TestCase("The Good Lord S01", "The Good Lord S01", 1, null)]
[TestCase("The Good Lord E05", "The Good Lord", 0, "5")]
[TestCase("The.Good.Lord.s01e05", "The.Good.Lord.s01e05", 0, null)]
[TestCase("The.Good.Lord.S01e05", "The.Good.Lord.S01e05", 0, null)]
[TestCase("The.Good.Lord.s01E05", "The.Good.Lord.s01E05", 0, null)]
[TestCase("The.Good.Lord.S1E5", "The.Good.Lord.S1E5", 0, null)]
[TestCase("The.Good.Lord.S11E5", "The.Good.Lord.S11E5", 0, null)]
[TestCase("The.Good.Lord.S1E15", "The.Good.Lord.S1E15", 0, null)]
public void TestToTorznabQuery(string query, string expected, int season, string episode)
{
var request = new ApiSearch { Query = query };
var currentQuery = ApiSearch.ToTorznabQuery(request);
Assert.AreEqual(expected, currentQuery.GetQueryString());
Assert.AreEqual(season, currentQuery.Season);
Assert.AreEqual(episode, currentQuery.Episode);
}
}
}