jellyfin/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbSearch.cs

267 lines
11 KiB
C#
Raw Normal View History

using System;
2014-01-30 21:23:54 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
2020-05-31 06:23:09 +00:00
using System.Text.RegularExpressions;
2014-01-30 21:23:54 +00:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
2020-05-31 06:23:09 +00:00
using MediaBrowser.Providers.Plugins.Tmdb.Models.Search;
using Microsoft.Extensions.Logging;
2014-01-30 21:23:54 +00:00
2020-05-31 06:23:09 +00:00
namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
2014-01-30 21:23:54 +00:00
{
public class TmdbSearch
2014-01-30 21:23:54 +00:00
{
private static readonly CultureInfo _usCulture = new CultureInfo("en-US");
private static readonly Regex _cleanEnclosed = new Regex(@"\p{Ps}.*\p{Pe}", RegexOptions.Compiled);
private static readonly Regex _cleanNonWord = new Regex(@"[\W_]+", RegexOptions.Compiled);
private static readonly Regex _cleanStopWords = new Regex(@"\b( # Start at word boundary
19[0-9]{2}|20[0-9]{2}| # 1900-2099
S[0-9]{2}| # Season
E[0-9]{2}| # Episode
(2160|1080|720|576|480)[ip]?| # Resolution
[xh]?264| # Encoding
(web|dvd|bd|hdtv|hd)rip| # *Rip
web|hdtv|mp4|bluray|ktr|dl|single|imageset|internal|doku|dubbed|retail|xxx|flac
).* # Match rest of string",
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase);
private const string _searchURL = TmdbUtils.BaseTmdbApiUrl + @"3/search/{3}?api_key={1}&query={0}&language={2}";
2016-06-23 17:04:18 +00:00
2014-01-30 21:23:54 +00:00
private readonly ILogger _logger;
private readonly IJsonSerializer _json;
2014-11-18 02:48:22 +00:00
private readonly ILibraryManager _libraryManager;
2014-01-30 21:23:54 +00:00
public TmdbSearch(ILogger logger, IJsonSerializer json, ILibraryManager libraryManager)
2014-01-30 21:23:54 +00:00
{
_logger = logger;
_json = json;
2014-11-18 02:48:22 +00:00
_libraryManager = libraryManager;
2014-01-30 21:23:54 +00:00
}
2014-03-02 17:09:35 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo idInfo, CancellationToken cancellationToken)
2014-02-09 06:08:10 +00:00
{
2014-03-01 22:34:27 +00:00
return GetSearchResults(idInfo, "tv", cancellationToken);
2014-02-09 06:08:10 +00:00
}
2014-03-02 17:09:35 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetMovieSearchResults(ItemLookupInfo idInfo, CancellationToken cancellationToken)
2014-01-30 21:23:54 +00:00
{
2014-03-01 22:34:27 +00:00
return GetSearchResults(idInfo, "movie", cancellationToken);
2014-01-30 21:23:54 +00:00
}
2014-03-02 17:09:35 +00:00
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo idInfo, CancellationToken cancellationToken)
2014-01-30 21:23:54 +00:00
{
2014-03-01 22:34:27 +00:00
return GetSearchResults(idInfo, "collection", cancellationToken);
2014-01-30 21:23:54 +00:00
}
2014-03-02 17:09:35 +00:00
private async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ItemLookupInfo idInfo, string searchType, CancellationToken cancellationToken)
2014-01-30 21:23:54 +00:00
{
var name = idInfo.Name;
2014-02-13 05:11:54 +00:00
var year = idInfo.Year;
2014-02-14 04:00:13 +00:00
2016-06-23 17:04:18 +00:00
if (string.IsNullOrWhiteSpace(name))
{
return new List<RemoteSearchResult>();
}
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
2014-03-02 17:09:35 +00:00
2018-09-12 17:26:21 +00:00
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
2014-11-16 22:46:01 +00:00
// ParseName is required here.
// Caller provides the filename with extension stripped and NOT the parsed filename
var parsedName = _libraryManager.ParseName(name);
var yearInName = parsedName.Year;
name = parsedName.Name;
year ??= yearInName;
2014-01-30 21:23:54 +00:00
2019-01-27 11:03:43 +00:00
var language = idInfo.MetadataLanguage.ToLowerInvariant();
2014-01-30 21:23:54 +00:00
// Replace sequences of non-word characters with space
// TMDB expects a space separated list of words make sure that is the case
name = _cleanNonWord.Replace(name, " ").Trim();
2014-01-30 21:23:54 +00:00
_logger.LogInformation("TmdbSearch: Finding id for item: {0} ({1})", name, year);
2014-03-02 17:09:35 +00:00
var results = await GetSearchResults(name, searchType, year, language, tmdbImageUrl, cancellationToken).ConfigureAwait(false);
2016-06-23 17:04:18 +00:00
2014-03-01 22:34:27 +00:00
if (results.Count == 0)
2014-01-30 21:23:54 +00:00
{
//try in english if wasn't before
2014-03-01 22:34:27 +00:00
if (!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
2014-01-30 21:23:54 +00:00
{
2014-03-02 17:09:35 +00:00
results = await GetSearchResults(name, searchType, year, "en", tmdbImageUrl, cancellationToken).ConfigureAwait(false);
2014-01-30 21:23:54 +00:00
}
2014-03-01 22:34:27 +00:00
}
2014-01-30 21:23:54 +00:00
// TODO: retrying alternatives should be done outside the search
// provider so that the retry logic can be common for all search
// providers
2014-03-01 22:34:27 +00:00
if (results.Count == 0)
{
var name2 = parsedName.Name;
// Remove things enclosed in []{}() etc
name2 = _cleanEnclosed.Replace(name2, string.Empty);
// Replace sequences of non-word characters with space
name2 = _cleanNonWord.Replace(name2, " ");
// Clean based on common stop words / tokens
name2 = _cleanStopWords.Replace(name2, string.Empty);
// Trim whitespace
name2 = name2.Trim();
2014-01-30 21:23:54 +00:00
2014-03-01 22:34:27 +00:00
// Search again if the new name is different
if (!string.Equals(name2, name) && !string.IsNullOrWhiteSpace(name2))
2014-03-01 22:34:27 +00:00
{
_logger.LogInformation("TmdbSearch: Finding id for item: {0} ({1})", name2, year);
results = await GetSearchResults(name2, searchType, year, language, tmdbImageUrl, cancellationToken).ConfigureAwait(false);
2014-03-01 22:34:27 +00:00
if (results.Count == 0 && !string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
{
//one more time, in english
results = await GetSearchResults(name2, searchType, year, "en", tmdbImageUrl, cancellationToken).ConfigureAwait(false);
2014-01-30 21:23:54 +00:00
}
}
}
2014-03-02 17:09:35 +00:00
return results.Where(i =>
{
if (year.HasValue && i.ProductionYear.HasValue)
{
// Allow one year tolerance
return Math.Abs(year.Value - i.ProductionYear.Value) <= 1;
2014-03-02 17:09:35 +00:00
}
return true;
});
2014-01-30 21:23:54 +00:00
}
2016-06-23 17:04:18 +00:00
private Task<List<RemoteSearchResult>> GetSearchResults(string name, string type, int? year, string language, string baseImageUrl, CancellationToken cancellationToken)
2014-01-30 21:23:54 +00:00
{
switch (type)
{
case "tv":
2016-06-23 17:04:18 +00:00
return GetSearchResultsTv(name, year, language, baseImageUrl, cancellationToken);
default:
2016-06-23 17:04:18 +00:00
return GetSearchResultsGeneric(name, type, year, language, baseImageUrl, cancellationToken);
}
}
private async Task<List<RemoteSearchResult>> GetSearchResultsGeneric(string name, string type, int? year, string language, string baseImageUrl, CancellationToken cancellationToken)
{
2016-06-23 17:04:18 +00:00
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("name");
}
var url3 = string.Format(_searchURL, WebUtility.UrlEncode(name), TmdbUtils.ApiKey, language, type);
2014-01-30 21:23:54 +00:00
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
2014-01-30 21:23:54 +00:00
{
Url = url3,
CancellationToken = cancellationToken,
AcceptHeader = TmdbUtils.AcceptHeader
2014-01-30 21:23:54 +00:00
}).ConfigureAwait(false))
{
2017-10-20 16:16:56 +00:00
using (var json = response.Content)
{
2019-08-18 12:44:13 +00:00
var searchResults = await _json.DeserializeFromStreamAsync<TmdbSearchResult<MovieResult>>(json).ConfigureAwait(false);
2014-03-01 22:34:27 +00:00
2019-08-18 12:44:13 +00:00
var results = searchResults.Results ?? new List<MovieResult>();
2014-03-01 22:34:27 +00:00
2017-10-20 16:16:56 +00:00
return results
.Select(i =>
2014-03-02 17:09:35 +00:00
{
2017-10-20 16:16:56 +00:00
var remoteResult = new RemoteSearchResult
{
SearchProviderName = TmdbMovieProvider.Current.Name,
2019-08-18 12:44:13 +00:00
Name = i.Title ?? i.Name ?? i.Original_Title,
ImageUrl = string.IsNullOrWhiteSpace(i.Poster_Path) ? null : baseImageUrl + i.Poster_Path
2017-10-20 16:16:56 +00:00
};
2019-08-18 12:44:13 +00:00
if (!string.IsNullOrWhiteSpace(i.Release_Date))
2017-10-20 16:16:56 +00:00
{
// These dates are always in this exact format
if (DateTime.TryParseExact(i.Release_Date, "yyyy-MM-dd", _usCulture, DateTimeStyles.None, out var r))
2017-10-20 16:16:56 +00:00
{
remoteResult.PremiereDate = r.ToUniversalTime();
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
}
2014-03-02 17:09:35 +00:00
}
2020-06-06 19:17:49 +00:00
remoteResult.SetProviderId(MetadataProvider.Tmdb, i.Id.ToString(_usCulture));
2014-03-02 17:09:35 +00:00
2017-10-20 16:16:56 +00:00
return remoteResult;
2014-03-02 17:09:35 +00:00
2017-10-20 16:16:56 +00:00
})
.ToList();
}
2014-03-01 22:34:27 +00:00
}
}
private async Task<List<RemoteSearchResult>> GetSearchResultsTv(string name, int? year, string language, string baseImageUrl, CancellationToken cancellationToken)
{
2016-06-23 17:04:18 +00:00
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("name");
}
var url3 = string.Format(_searchURL, WebUtility.UrlEncode(name), TmdbUtils.ApiKey, language, "tv");
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
{
Url = url3,
CancellationToken = cancellationToken,
AcceptHeader = TmdbUtils.AcceptHeader
}).ConfigureAwait(false))
{
2017-10-20 16:16:56 +00:00
using (var json = response.Content)
{
2019-08-18 12:44:13 +00:00
var searchResults = await _json.DeserializeFromStreamAsync<TmdbSearchResult<TvResult>>(json).ConfigureAwait(false);
2019-08-18 12:44:13 +00:00
var results = searchResults.Results ?? new List<TvResult>();
2017-10-20 16:16:56 +00:00
return results
.Select(i =>
{
2017-10-20 16:16:56 +00:00
var remoteResult = new RemoteSearchResult
{
SearchProviderName = TmdbMovieProvider.Current.Name,
2019-08-18 12:44:13 +00:00
Name = i.Name ?? i.Original_Name,
ImageUrl = string.IsNullOrWhiteSpace(i.Poster_Path) ? null : baseImageUrl + i.Poster_Path
2017-10-20 16:16:56 +00:00
};
2019-08-18 12:44:13 +00:00
if (!string.IsNullOrWhiteSpace(i.First_Air_Date))
2017-10-20 16:16:56 +00:00
{
// These dates are always in this exact format
if (DateTime.TryParseExact(i.First_Air_Date, "yyyy-MM-dd", _usCulture, DateTimeStyles.None, out var r))
2017-10-20 16:16:56 +00:00
{
remoteResult.PremiereDate = r.ToUniversalTime();
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
}
}
2020-06-06 19:17:49 +00:00
remoteResult.SetProviderId(MetadataProvider.Tmdb, i.Id.ToString(_usCulture));
2017-10-20 16:16:56 +00:00
return remoteResult;
2017-10-20 16:16:56 +00:00
})
.ToList();
}
}
}
2014-01-30 21:23:54 +00:00
}
}