jellyfin/MediaBrowser.Providers/TV/TheMovieDb/MovieDbSeriesProvider.cs

647 lines
23 KiB
C#
Raw Normal View History

2014-02-09 06:08:10 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
2014-11-18 02:48:22 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Localization;
2014-02-09 06:08:10 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Providers;
2014-02-09 06:08:10 +00:00
using MediaBrowser.Model.Serialization;
using MediaBrowser.Providers.Movies;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
2014-02-09 06:08:10 +00:00
using System.Threading;
using System.Threading.Tasks;
2015-10-04 04:23:11 +00:00
using CommonIO;
2014-02-09 06:08:10 +00:00
namespace MediaBrowser.Providers.TV
{
2014-03-02 15:42:21 +00:00
public class MovieDbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder
2014-02-09 06:08:10 +00:00
{
2016-04-21 05:13:33 +00:00
private const string GetTvInfo3 = @"https://api.themoviedb.org/3/tv/{0}?api_key={1}&append_to_response=credits,images,keywords,external_ids,videos,content_ratings";
2014-02-09 06:08:10 +00:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
internal static MovieDbSeriesProvider Current { get; private set; }
private readonly IJsonSerializer _jsonSerializer;
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _configurationManager;
private readonly ILogger _logger;
private readonly ILocalizationManager _localization;
2014-03-01 22:34:27 +00:00
private readonly IHttpClient _httpClient;
2014-11-18 02:48:22 +00:00
private readonly ILibraryManager _libraryManager;
2014-02-09 06:08:10 +00:00
2015-02-20 18:06:08 +00:00
public MovieDbSeriesProvider(IJsonSerializer jsonSerializer, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILogger logger, ILocalizationManager localization, IHttpClient httpClient, ILibraryManager libraryManager)
2014-02-09 06:08:10 +00:00
{
_jsonSerializer = jsonSerializer;
_fileSystem = fileSystem;
_configurationManager = configurationManager;
_logger = logger;
_localization = localization;
2014-03-01 22:34:27 +00:00
_httpClient = httpClient;
2015-02-20 18:06:08 +00:00
_libraryManager = libraryManager;
2014-02-09 06:08:10 +00:00
Current = this;
}
public string Name
{
get { return "TheMovieDb"; }
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
{
2014-03-02 15:42:21 +00:00
var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
if (!string.IsNullOrEmpty(tmdbId))
{
cancellationToken.ThrowIfCancellationRequested();
await EnsureSeriesInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
2014-03-02 15:42:21 +00:00
var dataFilePath = GetDataFilePath(tmdbId, searchInfo.MetadataLanguage);
var obj = _jsonSerializer.DeserializeFromFile<RootObject>(dataFilePath);
2014-03-02 18:01:46 +00:00
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
var tmdbImageUrl = tmdbSettings.images.base_url + "original";
2014-03-02 15:42:21 +00:00
var remoteResult = new RemoteSearchResult
{
Name = obj.name,
SearchProviderName = Name,
ImageUrl = string.IsNullOrWhiteSpace(obj.poster_path) ? null : tmdbImageUrl + obj.poster_path
};
remoteResult.SetProviderId(MetadataProviders.Tmdb, obj.id.ToString(_usCulture));
remoteResult.SetProviderId(MetadataProviders.Imdb, obj.external_ids.imdb_id);
if (obj.external_ids.tvdb_id > 0)
{
remoteResult.SetProviderId(MetadataProviders.Tvdb, obj.external_ids.tvdb_id.ToString(_usCulture));
}
2014-03-02 15:42:21 +00:00
return new[] { remoteResult };
}
var imdbId = searchInfo.GetProviderId(MetadataProviders.Imdb);
if (!string.IsNullOrEmpty(imdbId))
{
var searchResult = await FindByExternalId(imdbId, "imdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2014-03-02 18:01:46 +00:00
return new[] { searchResult };
2014-03-02 15:42:21 +00:00
}
}
var tvdbId = searchInfo.GetProviderId(MetadataProviders.Tvdb);
if (!string.IsNullOrEmpty(tvdbId))
{
var searchResult = await FindByExternalId(tvdbId, "tvdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2014-03-02 18:01:46 +00:00
return new[] { searchResult };
2014-03-02 15:42:21 +00:00
}
}
2014-11-18 02:48:22 +00:00
return await new MovieDbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
}
2014-02-09 06:08:10 +00:00
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo info, CancellationToken cancellationToken)
{
var result = new MetadataResult<Series>();
var tmdbId = info.GetProviderId(MetadataProviders.Tmdb);
2014-02-13 05:11:54 +00:00
if (string.IsNullOrEmpty(tmdbId))
{
var imdbId = info.GetProviderId(MetadataProviders.Imdb);
2014-02-09 06:08:10 +00:00
2014-02-13 05:11:54 +00:00
if (!string.IsNullOrEmpty(imdbId))
{
2014-03-02 15:42:21 +00:00
var searchResult = await FindByExternalId(imdbId, "imdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2014-03-02 18:01:46 +00:00
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
2014-03-02 15:42:21 +00:00
}
2014-02-13 05:11:54 +00:00
}
}
if (string.IsNullOrEmpty(tmdbId))
{
var tvdbId = info.GetProviderId(MetadataProviders.Tvdb);
if (!string.IsNullOrEmpty(tvdbId))
{
2014-03-02 15:42:21 +00:00
var searchResult = await FindByExternalId(tvdbId, "tvdb_id", cancellationToken).ConfigureAwait(false);
if (searchResult != null)
{
2014-03-02 18:01:46 +00:00
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
2014-03-02 15:42:21 +00:00
}
2014-02-13 05:11:54 +00:00
}
}
if (string.IsNullOrEmpty(tmdbId))
2014-02-09 06:08:10 +00:00
{
2014-11-18 02:48:22 +00:00
var searchResults = await new MovieDbSearch(_logger, _jsonSerializer, _libraryManager).GetSearchResults(info, cancellationToken).ConfigureAwait(false);
2014-03-01 22:34:27 +00:00
var searchResult = searchResults.FirstOrDefault();
2014-02-15 16:36:09 +00:00
if (searchResult != null)
{
2014-03-02 17:09:35 +00:00
tmdbId = searchResult.GetProviderId(MetadataProviders.Tmdb);
2014-02-15 16:36:09 +00:00
}
2014-02-09 06:08:10 +00:00
}
2014-02-13 05:11:54 +00:00
if (!string.IsNullOrEmpty(tmdbId))
2014-02-09 06:08:10 +00:00
{
cancellationToken.ThrowIfCancellationRequested();
result.Item = await FetchMovieData(tmdbId, info.MetadataLanguage, info.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
2014-02-09 06:08:10 +00:00
result.HasMetadata = result.Item != null;
}
2014-03-02 15:42:21 +00:00
2014-02-09 06:08:10 +00:00
return result;
}
private async Task<Series> FetchMovieData(string tmdbId, string language, string preferredCountryCode, CancellationToken cancellationToken)
2014-02-09 06:08:10 +00:00
{
string dataFilePath = null;
RootObject seriesInfo = null;
2014-02-13 05:11:54 +00:00
if (!string.IsNullOrEmpty(tmdbId))
2014-02-09 06:08:10 +00:00
{
2014-02-13 05:11:54 +00:00
seriesInfo = await FetchMainResult(tmdbId, language, cancellationToken).ConfigureAwait(false);
}
2014-02-09 06:08:10 +00:00
2014-02-13 05:11:54 +00:00
if (seriesInfo == null)
{
return null;
}
2014-02-09 06:08:10 +00:00
2014-02-13 05:11:54 +00:00
tmdbId = seriesInfo.id.ToString(_usCulture);
2014-02-09 06:08:10 +00:00
2014-03-02 15:42:21 +00:00
dataFilePath = GetDataFilePath(tmdbId, language);
2015-09-13 21:32:02 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(dataFilePath));
2014-02-13 05:11:54 +00:00
_jsonSerializer.SerializeToFile(seriesInfo, dataFilePath);
2014-02-09 06:08:10 +00:00
await EnsureSeriesInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
var item = new Series();
ProcessMainInfo(item, seriesInfo, preferredCountryCode);
2014-02-09 06:08:10 +00:00
return item;
}
private void ProcessMainInfo(Series series, RootObject seriesInfo, string preferredCountryCode)
2014-02-09 06:08:10 +00:00
{
series.Name = seriesInfo.name;
series.SetProviderId(MetadataProviders.Tmdb, seriesInfo.id.ToString(_usCulture));
series.VoteCount = seriesInfo.vote_count;
string voteAvg = seriesInfo.vote_average.ToString(CultureInfo.InvariantCulture);
float rating;
if (float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out rating))
{
series.CommunityRating = rating;
}
2014-03-02 15:42:21 +00:00
series.Overview = seriesInfo.overview;
2014-02-09 06:08:10 +00:00
if (seriesInfo.networks != null)
{
series.Studios = seriesInfo.networks.Select(i => i.name).ToList();
}
if (seriesInfo.genres != null)
{
series.Genres = seriesInfo.genres.Select(i => i.name).ToList();
}
series.HomePageUrl = seriesInfo.homepage;
2014-02-09 06:08:10 +00:00
series.RunTimeTicks = seriesInfo.episode_run_time.Select(i => TimeSpan.FromMinutes(i).Ticks).FirstOrDefault();
if (string.Equals(seriesInfo.status, "Ended", StringComparison.OrdinalIgnoreCase))
{
series.Status = SeriesStatus.Ended;
series.EndDate = seriesInfo.last_air_date;
2014-02-09 06:08:10 +00:00
}
else
{
series.Status = SeriesStatus.Continuing;
}
series.PremiereDate = seriesInfo.first_air_date;
var ids = seriesInfo.external_ids;
if (ids != null)
{
if (!string.IsNullOrWhiteSpace(ids.imdb_id))
{
series.SetProviderId(MetadataProviders.Imdb, ids.imdb_id);
}
if (ids.tvrage_id > 0)
{
2014-02-09 07:27:44 +00:00
series.SetProviderId(MetadataProviders.TvRage, ids.tvrage_id.ToString(_usCulture));
2014-02-09 06:08:10 +00:00
}
if (ids.tvdb_id > 0)
{
series.SetProviderId(MetadataProviders.Tvdb, ids.tvdb_id.ToString(_usCulture));
}
}
var contentRatings = (seriesInfo.content_ratings ?? new ContentRatings()).results ?? new List<ContentRating>();
var ourRelease = contentRatings.FirstOrDefault(c => string.Equals(c.iso_3166_1, preferredCountryCode, StringComparison.OrdinalIgnoreCase));
var usRelease = contentRatings.FirstOrDefault(c => string.Equals(c.iso_3166_1, "US", StringComparison.OrdinalIgnoreCase));
var minimumRelease = contentRatings.FirstOrDefault();
if (ourRelease != null)
{
series.OfficialRating = ourRelease.rating;
}
else if (usRelease != null)
{
series.OfficialRating = usRelease.rating;
}
else if (minimumRelease != null)
{
series.OfficialRating = minimumRelease.rating;
}
2014-02-09 06:08:10 +00:00
}
internal static string GetSeriesDataPath(IApplicationPaths appPaths, string tmdbId)
{
var dataPath = GetSeriesDataPath(appPaths);
return Path.Combine(dataPath, tmdbId);
}
internal static string GetSeriesDataPath(IApplicationPaths appPaths)
{
2014-02-20 16:37:41 +00:00
var dataPath = Path.Combine(appPaths.CachePath, "tmdb-tv");
2014-02-09 06:08:10 +00:00
return dataPath;
}
2014-03-02 15:42:21 +00:00
2014-02-09 06:08:10 +00:00
internal async Task DownloadSeriesInfo(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
{
var mainResult = await FetchMainResult(id, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
if (mainResult == null) return;
var dataFilePath = GetDataFilePath(id, preferredMetadataLanguage);
2015-09-13 21:32:02 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(dataFilePath));
2014-02-09 06:08:10 +00:00
_jsonSerializer.SerializeToFile(mainResult, dataFilePath);
}
internal async Task<RootObject> FetchMainResult(string id, string language, CancellationToken cancellationToken)
{
var url = string.Format(GetTvInfo3, id, MovieDbProvider.ApiKey);
if (!string.IsNullOrEmpty(language))
{
2016-03-12 15:19:35 +00:00
url += string.Format("&language={0}", MovieDbProvider.NormalizeLanguage(language));
2014-03-02 15:42:21 +00:00
// Get images in english and with no language
url += "&include_image_language=" + MovieDbProvider.GetImageLanguagesParam(language);
}
2014-02-09 06:08:10 +00:00
cancellationToken.ThrowIfCancellationRequested();
RootObject mainResult;
2014-02-09 06:08:10 +00:00
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
{
Url = url,
CancellationToken = cancellationToken,
AcceptHeader = MovieDbProvider.AcceptHeader
}).ConfigureAwait(false))
{
mainResult = _jsonSerializer.DeserializeFromStream<RootObject>(json);
2014-02-09 06:08:10 +00:00
}
cancellationToken.ThrowIfCancellationRequested();
// If the language preference isn't english, then have the overview fallback to english if it's blank
if (mainResult != null &&
string.IsNullOrEmpty(mainResult.overview) &&
!string.IsNullOrEmpty(language) &&
!string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
{
_logger.Info("MovieDbSeriesProvider couldn't find meta for language " + language + ". Trying English...");
url = string.Format(GetTvInfo3, id, MovieDbProvider.ApiKey) + "&language=en";
if (!string.IsNullOrEmpty(language))
{
// Get images in english and with no language
url += "&include_image_language=" + MovieDbProvider.GetImageLanguagesParam(language);
}
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
{
Url = url,
CancellationToken = cancellationToken,
AcceptHeader = MovieDbProvider.AcceptHeader
}).ConfigureAwait(false))
{
var englishResult = _jsonSerializer.DeserializeFromStream<RootObject>(json);
mainResult.overview = englishResult.overview;
}
}
return mainResult;
2014-02-09 06:08:10 +00:00
}
2014-03-02 15:42:21 +00:00
2014-02-09 06:08:10 +00:00
private readonly Task _cachedTask = Task.FromResult(true);
internal Task EnsureSeriesInfo(string tmdbId, string language, CancellationToken cancellationToken)
{
if (string.IsNullOrEmpty(tmdbId))
{
throw new ArgumentNullException("tmdbId");
}
var path = GetDataFilePath(tmdbId, language);
var fileInfo = _fileSystem.GetFileSystemInfo(path);
if (fileInfo.Exists)
{
// If it's recent or automatic updates are enabled, don't re-download
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 3)
2014-02-09 06:08:10 +00:00
{
return _cachedTask;
}
}
return DownloadSeriesInfo(tmdbId, language, cancellationToken);
}
internal string GetDataFilePath(string tmdbId, string preferredLanguage)
{
if (string.IsNullOrEmpty(tmdbId))
{
throw new ArgumentNullException("tmdbId");
}
var path = GetSeriesDataPath(_configurationManager.ApplicationPaths, tmdbId);
var filename = string.Format("series-{0}.json", preferredLanguage ?? string.Empty);
2014-02-09 06:08:10 +00:00
return Path.Combine(path, filename);
}
2016-04-27 21:09:52 +00:00
public bool HasChanged(IHasMetadata item)
2014-02-09 06:08:10 +00:00
{
2015-01-18 05:45:10 +00:00
if (!MovieDbProvider.Current.GetTheMovieDbOptions().EnableAutomaticUpdates)
2014-02-09 06:08:10 +00:00
{
return false;
}
var tmdbId = item.GetProviderId(MetadataProviders.Tmdb);
if (!String.IsNullOrEmpty(tmdbId))
{
// Process images
var dataFilePath = GetDataFilePath(tmdbId, item.GetPreferredMetadataLanguage());
2015-10-04 03:38:46 +00:00
var fileInfo = _fileSystem.GetFileInfo(dataFilePath);
2014-02-09 06:08:10 +00:00
2016-04-27 21:09:52 +00:00
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > item.DateLastRefreshed;
2014-02-09 06:08:10 +00:00
}
return false;
}
2014-03-02 18:01:46 +00:00
private async Task<RemoteSearchResult> FindByExternalId(string id, string externalSource, CancellationToken cancellationToken)
2014-02-13 05:11:54 +00:00
{
2016-04-21 05:13:33 +00:00
var url = string.Format("https://api.themoviedb.org/3/tv/find/{0}?api_key={1}&external_source={2}",
2014-03-02 15:42:21 +00:00
id,
2014-02-13 05:11:54 +00:00
MovieDbProvider.ApiKey,
externalSource);
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
{
Url = url,
CancellationToken = cancellationToken,
AcceptHeader = MovieDbProvider.AcceptHeader
}).ConfigureAwait(false))
{
var result = _jsonSerializer.DeserializeFromStream<MovieDbSearch.ExternalIdLookupResult>(json);
if (result != null && result.tv_results != null)
{
var tv = result.tv_results.FirstOrDefault();
if (tv != null)
{
2014-03-02 18:01:46 +00:00
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
var tmdbImageUrl = tmdbSettings.images.base_url + "original";
var remoteResult = new RemoteSearchResult
{
Name = tv.name,
SearchProviderName = Name,
ImageUrl = string.IsNullOrWhiteSpace(tv.poster_path) ? null : tmdbImageUrl + tv.poster_path
};
remoteResult.SetProviderId(MetadataProviders.Tmdb, tv.id.ToString(_usCulture));
return remoteResult;
2014-02-13 05:11:54 +00:00
}
}
}
return null;
}
2014-02-09 06:08:10 +00:00
public class CreatedBy
{
public int id { get; set; }
public string name { get; set; }
public string profile_path { get; set; }
}
public class Genre
{
public int id { get; set; }
public string name { get; set; }
}
public class Network
{
public int id { get; set; }
public string name { get; set; }
}
public class Season
{
public string air_date { get; set; }
public int episode_count { get; set; }
public int id { get; set; }
2014-02-09 06:08:10 +00:00
public string poster_path { get; set; }
public int season_number { get; set; }
}
public class Cast
{
public string character { get; set; }
public string credit_id { get; set; }
public int id { get; set; }
public string name { get; set; }
public string profile_path { get; set; }
public int order { get; set; }
}
public class Crew
{
public string credit_id { get; set; }
public string department { get; set; }
public int id { get; set; }
public string name { get; set; }
public string job { get; set; }
public string profile_path { get; set; }
}
public class Credits
{
public List<Cast> cast { get; set; }
public List<Crew> crew { get; set; }
}
2014-02-09 06:08:10 +00:00
public class Backdrop
{
public double aspect_ratio { get; set; }
public string file_path { get; set; }
public int height { get; set; }
public string iso_639_1 { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
public int width { get; set; }
}
public class Poster
{
public double aspect_ratio { get; set; }
public string file_path { get; set; }
public int height { get; set; }
public string iso_639_1 { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
public int width { get; set; }
}
public class Images
{
public List<Backdrop> backdrops { get; set; }
public List<Poster> posters { get; set; }
}
public class Keywords
{
public List<object> results { get; set; }
}
2014-02-09 06:08:10 +00:00
public class ExternalIds
{
public string imdb_id { get; set; }
public string freebase_id { get; set; }
public string freebase_mid { get; set; }
public int tvdb_id { get; set; }
public int tvrage_id { get; set; }
}
public class Videos
{
public List<object> results { get; set; }
}
public class ContentRating
{
public string iso_3166_1 { get; set; }
public string rating { get; set; }
}
public class ContentRatings
{
public List<ContentRating> results { get; set; }
}
2014-02-09 06:08:10 +00:00
public class RootObject
{
public string backdrop_path { get; set; }
public List<CreatedBy> created_by { get; set; }
public List<int> episode_run_time { get; set; }
public DateTime first_air_date { get; set; }
public List<Genre> genres { get; set; }
public string homepage { get; set; }
public int id { get; set; }
public bool in_production { get; set; }
public List<string> languages { get; set; }
public DateTime last_air_date { get; set; }
public string name { get; set; }
public List<Network> networks { get; set; }
public int number_of_episodes { get; set; }
public int number_of_seasons { get; set; }
public string original_name { get; set; }
public List<string> origin_country { get; set; }
public string overview { get; set; }
public string popularity { get; set; }
public string poster_path { get; set; }
public List<Season> seasons { get; set; }
public string status { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
public Credits credits { get; set; }
2014-02-09 06:08:10 +00:00
public Images images { get; set; }
public Keywords keywords { get; set; }
2014-02-09 06:08:10 +00:00
public ExternalIds external_ids { get; set; }
public Videos videos { get; set; }
public ContentRatings content_ratings { get; set; }
2014-02-09 06:08:10 +00:00
}
public int Order
{
get
{
2014-02-15 16:36:09 +00:00
// After Omdb and Tvdb
2014-02-09 06:08:10 +00:00
return 2;
}
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
2014-03-01 22:34:27 +00:00
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = url,
ResourcePool = MovieDbProvider.Current.MovieDbResourcePool
});
}
2014-02-09 06:08:10 +00:00
}
}