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

253 lines
10 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;
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;
using MediaBrowser.Providers.Tmdb.Models.Search;
using Microsoft.Extensions.Logging;
2014-01-30 21:23:54 +00:00
namespace MediaBrowser.Providers.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 EnUs = new CultureInfo("en-US");
2019-08-18 11:34:44 +00:00
private const string Search3 = 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
2015-05-31 18:22:51 +00:00
if (!string.IsNullOrWhiteSpace(name))
{
var parsedName = _libraryManager.ParseName(name);
var yearInName = parsedName.Year;
name = parsedName.Name;
year = year ?? yearInName;
}
2014-01-30 21:23:54 +00:00
_logger.LogInformation("MovieDbProvider: Finding id for item: " + name);
2019-01-27 11:03:43 +00:00
var language = idInfo.MetadataLanguage.ToLowerInvariant();
2014-01-30 21:23:54 +00:00
//nope - search for it
//var searchType = item is BoxSet ? "collection" : "movie";
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
2014-03-01 22:34:27 +00:00
if (results.Count == 0)
{
// try with dot and _ turned to space
var originalName = name;
2014-02-09 04:52:52 +00:00
2014-03-01 22:34:27 +00:00
name = name.Replace(",", " ");
name = name.Replace(".", " ");
name = name.Replace("_", " ");
name = name.Replace("-", " ");
name = name.Replace("!", " ");
name = name.Replace("?", " ");
2014-01-30 21:23:54 +00:00
var parenthIndex = name.IndexOf('(');
if (parenthIndex != -1)
{
name = name.Substring(0, parenthIndex);
}
2014-03-01 22:34:27 +00:00
name = name.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(name, originalName))
{
2014-03-02 17:09:35 +00:00
results = await GetSearchResults(name, 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
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-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(Search3, 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
2019-08-18 12:44:13 +00:00
if (DateTime.TryParseExact(i.Release_Date, "yyyy-MM-dd", EnUs, 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
}
2019-08-18 12:44:13 +00:00
remoteResult.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(EnUs));
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(Search3, 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
2019-08-18 12:44:13 +00:00
if (DateTime.TryParseExact(i.First_Air_Date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out var r))
2017-10-20 16:16:56 +00:00
{
remoteResult.PremiereDate = r.ToUniversalTime();
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
}
}
2019-08-18 12:44:13 +00:00
remoteResult.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(EnUs));
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
}
}