jellyfin-server/MediaBrowser.Providers/Movies/MovieDbSearch.cs

399 lines
15 KiB
C#
Raw Normal View History

2014-01-30 21:23:54 +00:00
using MediaBrowser.Common.Net;
2014-11-16 22:46:01 +00:00
using MediaBrowser.Controller.Library;
2014-01-30 21:23:54 +00:00
using MediaBrowser.Controller.Providers;
2014-03-02 17:09:35 +00:00
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
2014-03-02 17:09:35 +00:00
using MediaBrowser.Model.Providers;
2014-01-30 21:23:54 +00:00
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Movies
{
public class MovieDbSearch
{
private static readonly CultureInfo EnUs = new CultureInfo("en-US");
2018-09-12 17:26:21 +00:00
private const string Search3 = MovieDbProvider.BaseMovieDbUrl + @"3/search/{3}?api_key={1}&query={0}&language={2}";
2014-01-30 21:23:54 +00:00
2019-01-09 17:09:40 +00:00
internal static string ApiKey = "4219e299c89411838049ab0dab19ebd5";
2014-01-30 21:23:54 +00:00
internal static string AcceptHeader = "application/json,image/*";
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
2014-11-18 02:48:22 +00:00
public MovieDbSearch(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>();
}
2014-03-02 17:09:35 +00:00
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
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);
2014-01-30 21:23:54 +00:00
var language = idInfo.MetadataLanguage.ToLower();
//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");
}
2014-01-30 21:23:54 +00:00
var url3 = string.Format(Search3, WebUtility.UrlEncode(name), ApiKey, language, type);
2017-10-20 16:16:56 +00:00
using (var response = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
2014-01-30 21:23:54 +00:00
{
Url = url3,
CancellationToken = cancellationToken,
AcceptHeader = AcceptHeader
}).ConfigureAwait(false))
{
2017-10-20 16:16:56 +00:00
using (var json = response.Content)
{
2018-09-12 17:26:21 +00:00
var searchResults = await _json.DeserializeFromStreamAsync<TmdbMovieSearchResults>(json).ConfigureAwait(false);
2014-03-01 22:34:27 +00:00
2017-10-20 16:16:56 +00:00
var results = searchResults.results ?? new List<TmdbMovieSearchResult>();
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 = MovieDbProvider.Current.Name,
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
if (!string.IsNullOrWhiteSpace(i.release_date))
{
DateTime r;
2014-03-02 17:09:35 +00:00
// These dates are always in this exact format
if (DateTime.TryParseExact(i.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out 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
}
2017-10-20 16:16:56 +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), ApiKey, language, "tv");
2017-10-20 16:16:56 +00:00
using (var response = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
{
Url = url3,
CancellationToken = cancellationToken,
AcceptHeader = AcceptHeader
}).ConfigureAwait(false))
{
2017-10-20 16:16:56 +00:00
using (var json = response.Content)
{
2018-09-12 17:26:21 +00:00
var searchResults = await _json.DeserializeFromStreamAsync<TmdbTvSearchResults>(json).ConfigureAwait(false);
2017-10-20 16:16:56 +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 = MovieDbProvider.Current.Name,
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
if (!string.IsNullOrWhiteSpace(i.first_air_date))
{
DateTime r;
// These dates are always in this exact format
if (DateTime.TryParseExact(i.first_air_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r))
2017-10-20 16:16:56 +00:00
{
remoteResult.PremiereDate = r.ToUniversalTime();
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
}
}
2017-10-20 16:16:56 +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
/// <summary>
/// Class TmdbMovieSearchResult
/// </summary>
2014-02-15 16:36:09 +00:00
public class TmdbMovieSearchResult
2014-01-30 21:23:54 +00:00
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="TmdbMovieSearchResult" /> is adult.
/// </summary>
/// <value><c>true</c> if adult; otherwise, <c>false</c>.</value>
public bool adult { get; set; }
/// <summary>
/// Gets or sets the backdrop_path.
/// </summary>
/// <value>The backdrop_path.</value>
public string backdrop_path { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public int id { get; set; }
/// <summary>
/// Gets or sets the original_title.
/// </summary>
/// <value>The original_title.</value>
public string original_title { get; set; }
/// <summary>
2015-03-10 01:30:20 +00:00
/// Gets or sets the original_name.
/// </summary>
/// <value>The original_name.</value>
public string original_name { get; set; }
/// <summary>
2014-01-30 21:23:54 +00:00
/// Gets or sets the release_date.
/// </summary>
/// <value>The release_date.</value>
public string release_date { get; set; }
/// <summary>
/// Gets or sets the poster_path.
/// </summary>
/// <value>The poster_path.</value>
public string poster_path { get; set; }
/// <summary>
/// Gets or sets the popularity.
/// </summary>
/// <value>The popularity.</value>
public double popularity { get; set; }
/// <summary>
/// Gets or sets the title.
/// </summary>
/// <value>The title.</value>
public string title { get; set; }
/// <summary>
/// Gets or sets the vote_average.
/// </summary>
/// <value>The vote_average.</value>
public double vote_average { get; set; }
/// <summary>
/// For collection search results
/// </summary>
public string name { get; set; }
/// <summary>
/// Gets or sets the vote_count.
/// </summary>
/// <value>The vote_count.</value>
public int vote_count { get; set; }
}
/// <summary>
/// Class TmdbMovieSearchResults
/// </summary>
private class TmdbMovieSearchResults
{
/// <summary>
/// Gets or sets the page.
/// </summary>
/// <value>The page.</value>
public int page { get; set; }
/// <summary>
/// Gets or sets the results.
/// </summary>
/// <value>The results.</value>
public List<TmdbMovieSearchResult> results { get; set; }
/// <summary>
/// Gets or sets the total_pages.
/// </summary>
/// <value>The total_pages.</value>
public int total_pages { get; set; }
/// <summary>
/// Gets or sets the total_results.
/// </summary>
/// <value>The total_results.</value>
public int total_results { get; set; }
}
2014-02-13 05:11:54 +00:00
public class TvResult
{
public string backdrop_path { get; set; }
public string first_air_date { get; set; }
2014-02-13 05:11:54 +00:00
public int id { get; set; }
public string original_name { get; set; }
public string poster_path { get; set; }
public double popularity { get; set; }
public string name { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
}
/// <summary>
/// Class TmdbTvSearchResults
/// </summary>
private class TmdbTvSearchResults
{
/// <summary>
/// Gets or sets the page.
/// </summary>
/// <value>The page.</value>
public int page { get; set; }
/// <summary>
/// Gets or sets the results.
/// </summary>
/// <value>The results.</value>
public List<TvResult> results { get; set; }
/// <summary>
/// Gets or sets the total_pages.
/// </summary>
/// <value>The total_pages.</value>
public int total_pages { get; set; }
/// <summary>
/// Gets or sets the total_results.
/// </summary>
/// <value>The total_results.</value>
public int total_results { get; set; }
}
2014-02-13 05:11:54 +00:00
public class ExternalIdLookupResult
{
public List<object> movie_results { get; set; }
public List<object> person_results { get; set; }
public List<TvResult> tv_results { get; set; }
}
2014-01-30 21:23:54 +00:00
}
}