2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2014-01-30 22:23:54 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 20:26:31 +01:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-08-18 13:20:52 +02:00
|
|
|
using MediaBrowser.Providers.Tmdb.Models.Search;
|
2019-01-13 20:26:31 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
namespace MediaBrowser.Providers.Tmdb.Movies
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
public class TmdbSearch
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
|
|
|
private static readonly CultureInfo EnUs = new CultureInfo("en-US");
|
2019-08-18 13:34:44 +02:00
|
|
|
private const string Search3 = TmdbUtils.BaseTmdbApiUrl + @"3/search/{3}?api_key={1}&query={0}&language={2}";
|
2016-06-23 19:04:18 +02:00
|
|
|
|
2014-01-30 22:23:54 +01:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IJsonSerializer _json;
|
2014-11-18 03:48:22 +01:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
public TmdbSearch(ILogger logger, IJsonSerializer json, ILibraryManager libraryManager)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_json = json;
|
2014-11-18 03:48:22 +01:00
|
|
|
_libraryManager = libraryManager;
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo idInfo, CancellationToken cancellationToken)
|
2014-02-09 07:08:10 +01:00
|
|
|
{
|
2014-03-01 23:34:27 +01:00
|
|
|
return GetSearchResults(idInfo, "tv", cancellationToken);
|
2014-02-09 07:08:10 +01:00
|
|
|
}
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetMovieSearchResults(ItemLookupInfo idInfo, CancellationToken cancellationToken)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2014-03-01 23:34:27 +01:00
|
|
|
return GetSearchResults(idInfo, "movie", cancellationToken);
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(BoxSetInfo idInfo, CancellationToken cancellationToken)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2014-03-01 23:34:27 +01:00
|
|
|
return GetSearchResults(idInfo, "collection", cancellationToken);
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
private async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ItemLookupInfo idInfo, string searchType, CancellationToken cancellationToken)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
|
|
|
var name = idInfo.Name;
|
2014-02-13 06:11:54 +01:00
|
|
|
var year = idInfo.Year;
|
2014-02-14 05:00:13 +01:00
|
|
|
|
2016-06-23 19:04:18 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
{
|
|
|
|
return new List<RemoteSearchResult>();
|
|
|
|
}
|
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2014-03-02 18:09:35 +01:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2014-11-16 23:46:01 +01:00
|
|
|
|
2015-05-31 20:22:51 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
|
|
{
|
|
|
|
var parsedName = _libraryManager.ParseName(name);
|
|
|
|
var yearInName = parsedName.Year;
|
|
|
|
name = parsedName.Name;
|
|
|
|
year = year ?? yearInName;
|
|
|
|
}
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2018-12-13 14:18:25 +01:00
|
|
|
_logger.LogInformation("MovieDbProvider: Finding id for item: " + name);
|
2019-01-27 12:03:43 +01:00
|
|
|
var language = idInfo.MetadataLanguage.ToLowerInvariant();
|
2014-01-30 22:23:54 +01:00
|
|
|
|
|
|
|
//nope - search for it
|
|
|
|
//var searchType = item is BoxSet ? "collection" : "movie";
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
var results = await GetSearchResults(name, searchType, year, language, tmdbImageUrl, cancellationToken).ConfigureAwait(false);
|
2016-06-23 19:04:18 +02:00
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
if (results.Count == 0)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
|
|
|
//try in english if wasn't before
|
2014-03-01 23:34:27 +01:00
|
|
|
if (!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2014-03-02 18:09:35 +01:00
|
|
|
results = await GetSearchResults(name, searchType, year, "en", tmdbImageUrl, cancellationToken).ConfigureAwait(false);
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
2014-03-01 23:34:27 +01:00
|
|
|
}
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
if (results.Count == 0)
|
|
|
|
{
|
|
|
|
// try with dot and _ turned to space
|
|
|
|
var originalName = name;
|
2014-02-09 05:52:52 +01:00
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
name = name.Replace(",", " ");
|
|
|
|
name = name.Replace(".", " ");
|
|
|
|
name = name.Replace("_", " ");
|
|
|
|
name = name.Replace("-", " ");
|
|
|
|
name = name.Replace("!", " ");
|
|
|
|
name = name.Replace("?", " ");
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2017-10-14 08:52:05 +02:00
|
|
|
var parenthIndex = name.IndexOf('(');
|
|
|
|
if (parenthIndex != -1)
|
|
|
|
{
|
|
|
|
name = name.Substring(0, parenthIndex);
|
|
|
|
}
|
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
name = name.Trim();
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
// Search again if the new name is different
|
|
|
|
if (!string.Equals(name, originalName))
|
|
|
|
{
|
2014-03-02 18:09:35 +01:00
|
|
|
results = await GetSearchResults(name, searchType, year, language, tmdbImageUrl, cancellationToken).ConfigureAwait(false);
|
2014-03-01 23:34:27 +01:00
|
|
|
|
|
|
|
if (results.Count == 0 && !string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
//one more time, in english
|
2014-03-02 18:09:35 +01:00
|
|
|
results = await GetSearchResults(name, searchType, year, "en", tmdbImageUrl, cancellationToken).ConfigureAwait(false);
|
2014-01-30 22:23:54 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 18:09:35 +01:00
|
|
|
return results.Where(i =>
|
|
|
|
{
|
|
|
|
if (year.HasValue && i.ProductionYear.HasValue)
|
|
|
|
{
|
2014-07-04 21:17:22 +02:00
|
|
|
// Allow one year tolerance
|
|
|
|
return Math.Abs(year.Value - i.ProductionYear.Value) <= 1;
|
2014-03-02 18:09:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
|
2016-06-23 19:04:18 +02:00
|
|
|
private Task<List<RemoteSearchResult>> GetSearchResults(string name, string type, int? year, string language, string baseImageUrl, CancellationToken cancellationToken)
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
2015-10-06 00:13:50 +02:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case "tv":
|
2016-06-23 19:04:18 +02:00
|
|
|
return GetSearchResultsTv(name, year, language, baseImageUrl, cancellationToken);
|
2015-10-06 00:13:50 +02:00
|
|
|
default:
|
2016-06-23 19:04:18 +02:00
|
|
|
return GetSearchResultsGeneric(name, type, year, language, baseImageUrl, cancellationToken);
|
2015-10-06 00:13:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<List<RemoteSearchResult>> GetSearchResultsGeneric(string name, string type, int? year, string language, string baseImageUrl, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-06-23 19:04:18 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("name");
|
|
|
|
}
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
var url3 = string.Format(Search3, WebUtility.UrlEncode(name), TmdbUtils.ApiKey, language, type);
|
2014-01-30 22:23:54 +01:00
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2014-01-30 22:23:54 +01:00
|
|
|
{
|
|
|
|
Url = url3,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 13:20:52 +02:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2014-01-30 22:23:54 +01:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
using (var json = response.Content)
|
|
|
|
{
|
2019-08-18 14:44:13 +02:00
|
|
|
var searchResults = await _json.DeserializeFromStreamAsync<TmdbSearchResult<MovieResult>>(json).ConfigureAwait(false);
|
2014-03-01 23:34:27 +01:00
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
var results = searchResults.Results ?? new List<MovieResult>();
|
2014-03-01 23:34:27 +01:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
return results
|
|
|
|
.Select(i =>
|
2014-03-02 18:09:35 +01:00
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
var remoteResult = new RemoteSearchResult
|
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
SearchProviderName = TmdbMovieProvider.Current.Name,
|
2019-08-18 14:44:13 +02:00
|
|
|
Name = i.Title ?? i.Name ?? i.Original_Title,
|
|
|
|
ImageUrl = string.IsNullOrWhiteSpace(i.Poster_Path) ? null : baseImageUrl + i.Poster_Path
|
2017-10-20 18:16:56 +02:00
|
|
|
};
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(i.Release_Date))
|
2017-10-20 18:16:56 +02:00
|
|
|
{
|
2019-01-13 20:26:31 +01:00
|
|
|
// These dates are always in this exact format
|
2019-08-18 14:44:13 +02:00
|
|
|
if (DateTime.TryParseExact(i.Release_Date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out var r))
|
2017-10-20 18:16:56 +02:00
|
|
|
{
|
|
|
|
remoteResult.PremiereDate = r.ToUniversalTime();
|
|
|
|
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
|
|
|
|
}
|
2014-03-02 18:09:35 +01:00
|
|
|
}
|
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
remoteResult.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(EnUs));
|
2014-03-02 18:09:35 +01:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
return remoteResult;
|
2014-03-02 18:09:35 +01:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
})
|
|
|
|
.ToList();
|
|
|
|
}
|
2014-03-01 23:34:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:13:50 +02:00
|
|
|
private async Task<List<RemoteSearchResult>> GetSearchResultsTv(string name, int? year, string language, string baseImageUrl, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-06-23 19:04:18 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
{
|
|
|
|
throw new ArgumentException("name");
|
|
|
|
}
|
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
var url3 = string.Format(Search3, WebUtility.UrlEncode(name), TmdbUtils.ApiKey, language, "tv");
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2019-08-18 13:20:52 +02:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2015-10-06 00:13:50 +02:00
|
|
|
{
|
|
|
|
Url = url3,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 13:20:52 +02:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2015-10-06 00:13:50 +02:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
using (var json = response.Content)
|
|
|
|
{
|
2019-08-18 14:44:13 +02:00
|
|
|
var searchResults = await _json.DeserializeFromStreamAsync<TmdbSearchResult<TvResult>>(json).ConfigureAwait(false);
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
var results = searchResults.Results ?? new List<TvResult>();
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
return results
|
|
|
|
.Select(i =>
|
2015-10-06 00:13:50 +02:00
|
|
|
{
|
2017-10-20 18:16:56 +02:00
|
|
|
var remoteResult = new RemoteSearchResult
|
|
|
|
{
|
2019-08-18 13:20:52 +02:00
|
|
|
SearchProviderName = TmdbMovieProvider.Current.Name,
|
2019-08-18 14:44:13 +02:00
|
|
|
Name = i.Name ?? i.Original_Name,
|
|
|
|
ImageUrl = string.IsNullOrWhiteSpace(i.Poster_Path) ? null : baseImageUrl + i.Poster_Path
|
2017-10-20 18:16:56 +02:00
|
|
|
};
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(i.First_Air_Date))
|
2017-10-20 18:16:56 +02:00
|
|
|
{
|
2019-01-13 20:26:31 +01:00
|
|
|
// These dates are always in this exact format
|
2019-08-18 14:44:13 +02:00
|
|
|
if (DateTime.TryParseExact(i.First_Air_Date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out var r))
|
2017-10-20 18:16:56 +02:00
|
|
|
{
|
|
|
|
remoteResult.PremiereDate = r.ToUniversalTime();
|
|
|
|
remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
|
|
|
|
}
|
2015-10-06 00:13:50 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 14:44:13 +02:00
|
|
|
remoteResult.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(EnUs));
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
return remoteResult;
|
2015-10-06 00:13:50 +02:00
|
|
|
|
2017-10-20 18:16:56 +02:00
|
|
|
})
|
|
|
|
.ToList();
|
|
|
|
}
|
2015-10-06 00:13:50 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-30 22:23:54 +01:00
|
|
|
}
|
|
|
|
}
|