1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138 |
- using MediaBrowser.Common.Configuration;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Entities.Movies;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Model.Serialization;
- using MediaBrowser.Providers.Savers;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MediaBrowser.Providers.Movies
- {
- /// <summary>
- /// Class MovieDbProvider
- /// </summary>
- public class MovieDbProvider : BaseMetadataProvider, IDisposable
- {
- protected static CultureInfo EnUs = new CultureInfo("en-US");
- protected readonly IProviderManager ProviderManager;
- /// <summary>
- /// The movie db
- /// </summary>
- internal readonly SemaphoreSlim MovieDbResourcePool = new SemaphoreSlim(1, 1);
- internal static MovieDbProvider Current { get; private set; }
- /// <summary>
- /// Gets the json serializer.
- /// </summary>
- /// <value>The json serializer.</value>
- protected IJsonSerializer JsonSerializer { get; private set; }
- /// <summary>
- /// Gets the HTTP client.
- /// </summary>
- /// <value>The HTTP client.</value>
- protected IHttpClient HttpClient { get; private set; }
- /// <summary>
- /// Initializes a new instance of the <see cref="MovieDbProvider" /> class.
- /// </summary>
- /// <param name="logManager">The log manager.</param>
- /// <param name="configurationManager">The configuration manager.</param>
- /// <param name="jsonSerializer">The json serializer.</param>
- /// <param name="httpClient">The HTTP client.</param>
- /// <param name="providerManager">The provider manager.</param>
- public MovieDbProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, IProviderManager providerManager)
- : base(logManager, configurationManager)
- {
- JsonSerializer = jsonSerializer;
- HttpClient = httpClient;
- ProviderManager = providerManager;
- Current = this;
- }
- /// <summary>
- /// Releases unmanaged and - optionally - managed resources.
- /// </summary>
- /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
- protected virtual void Dispose(bool dispose)
- {
- if (dispose)
- {
- MovieDbResourcePool.Dispose();
- }
- }
- /// <summary>
- /// Gets the priority.
- /// </summary>
- /// <value>The priority.</value>
- public override MetadataProviderPriority Priority
- {
- get { return MetadataProviderPriority.Third; }
- }
- /// <summary>
- /// Supportses the specified item.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
- public override bool Supports(BaseItem item)
- {
- var trailer = item as Trailer;
- if (trailer != null)
- {
- return !trailer.IsLocalTrailer;
- }
- // Don't support local trailers
- return item is Movie || item is BoxSet || item is MusicVideo;
- }
- /// <summary>
- /// Gets a value indicating whether [requires internet].
- /// </summary>
- /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
- public override bool RequiresInternet
- {
- get
- {
- return true;
- }
- }
- protected override bool RefreshOnVersionChange
- {
- get
- {
- return true;
- }
- }
- protected override string ProviderVersion
- {
- get
- {
- return "2";
- }
- }
- /// <summary>
- /// The _TMDB settings task
- /// </summary>
- private TmdbSettingsResult _tmdbSettings;
- private readonly SemaphoreSlim _tmdbSettingsSemaphore = new SemaphoreSlim(1, 1);
- /// <summary>
- /// Gets the TMDB settings.
- /// </summary>
- /// <returns>Task{TmdbSettingsResult}.</returns>
- internal async Task<TmdbSettingsResult> GetTmdbSettings(CancellationToken cancellationToken)
- {
- if (_tmdbSettings != null)
- {
- return _tmdbSettings;
- }
- await _tmdbSettingsSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
- try
- {
- // Check again in case it got populated while we were waiting.
- if (_tmdbSettings != null)
- {
- return _tmdbSettings;
- }
- using (var json = await GetMovieDbResponse(new HttpRequestOptions
- {
- Url = string.Format(TmdbConfigUrl, ApiKey),
- CancellationToken = cancellationToken,
- AcceptHeader = AcceptHeader
- }).ConfigureAwait(false))
- {
- _tmdbSettings = JsonSerializer.DeserializeFromStream<TmdbSettingsResult>(json);
- return _tmdbSettings;
- }
- }
- finally
- {
- _tmdbSettingsSemaphore.Release();
- }
- }
- private const string TmdbConfigUrl = "http://api.themoviedb.org/3/configuration?api_key={0}";
- private const string Search3 = @"http://api.themoviedb.org/3/search/{3}?api_key={1}&query={0}&language={2}";
- private const string GetMovieInfo3 = @"http://api.themoviedb.org/3/movie/{0}?api_key={1}&language={2}&append_to_response=casts,releases,images,keywords,trailers";
- private const string GetBoxSetInfo3 = @"http://api.themoviedb.org/3/collection/{0}?api_key={1}&language={2}&append_to_response=images";
- internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669";
- internal static string AcceptHeader = "application/json,image/*";
- static readonly Regex[] NameMatches = new[] {
- new Regex(@"(?<name>.*)\((?<year>\d{4})\)"), // matches "My Movie (2001)" and gives us the name and the year
- new Regex(@"(?<name>.*)") // last resort matches the whole string as the name
- };
- protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
- {
- if (HasAltMeta(item) && !ConfigurationManager.Configuration.EnableTmdbUpdates)
- return false;
- // Boxsets require two passes because we need the children to be refreshed
- if (item is BoxSet && string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb)))
- {
- return true;
- }
- return base.NeedsRefreshInternal(item, providerInfo);
- }
- protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
- {
- var language = ConfigurationManager.Configuration.PreferredMetadataLanguage;
- var path = GetDataFilePath(item, language);
- if (!string.IsNullOrEmpty(path))
- {
- var fileInfo = new FileInfo(path);
- if (fileInfo.Exists)
- {
- return fileInfo.LastWriteTimeUtc > providerInfo.LastRefreshed;
- }
- }
- return base.NeedsRefreshBasedOnCompareDate(item, providerInfo);
- }
- /// <summary>
- /// Gets the movie data path.
- /// </summary>
- /// <param name="appPaths">The app paths.</param>
- /// <param name="isBoxSet">if set to <c>true</c> [is box set].</param>
- /// <param name="tmdbId">The TMDB id.</param>
- /// <returns>System.String.</returns>
- internal static string GetMovieDataPath(IApplicationPaths appPaths, bool isBoxSet, string tmdbId)
- {
- var dataPath = isBoxSet ? GetBoxSetsDataPath(appPaths) : GetMoviesDataPath(appPaths);
- return Path.Combine(dataPath, tmdbId);
- }
- internal static string GetMoviesDataPath(IApplicationPaths appPaths)
- {
- var dataPath = Path.Combine(appPaths.DataPath, "tmdb-movies");
- return dataPath;
- }
- internal static string GetBoxSetsDataPath(IApplicationPaths appPaths)
- {
- var dataPath = Path.Combine(appPaths.DataPath, "tmdb-collections");
- return dataPath;
- }
- /// <summary>
- /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="force">if set to <c>true</c> [force].</param>
- /// <param name="cancellationToken">The cancellation token</param>
- /// <returns>Task{System.Boolean}.</returns>
- public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
- {
- cancellationToken.ThrowIfCancellationRequested();
- var id = item.GetProviderId(MetadataProviders.Tmdb);
- if (string.IsNullOrEmpty(id))
- {
- id = item.GetProviderId(MetadataProviders.Imdb);
- }
- if (string.IsNullOrEmpty(id))
- {
- id = await FindId(item, cancellationToken).ConfigureAwait(false);
- if (!string.IsNullOrEmpty(id))
- {
- item.SetProviderId(MetadataProviders.Tmdb, id);
- }
- }
- if (!string.IsNullOrEmpty(id))
- {
- cancellationToken.ThrowIfCancellationRequested();
- await FetchMovieData(item, id, force, cancellationToken).ConfigureAwait(false);
- }
- SetLastRefreshed(item, DateTime.UtcNow);
- return true;
- }
- /// <summary>
- /// Determines whether [has alt meta] [the specified item].
- /// </summary>
- /// <param name="item">The item.</param>
- /// <returns><c>true</c> if [has alt meta] [the specified item]; otherwise, <c>false</c>.</returns>
- internal static bool HasAltMeta(BaseItem item)
- {
- if (item is BoxSet)
- {
- return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName("collection.xml");
- }
- var path = MovieXmlSaver.GetMovieSavePath(item);
- if (item.LocationType == LocationType.FileSystem)
- {
- // If mixed with multiple movies in one folder, resolve args won't have the file system children
- return item.ResolveArgs.ContainsMetaFileByName(Path.GetFileName(path)) || File.Exists(path);
- }
- return false;
- }
- /// <summary>
- /// Parses the name.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <param name="justName">Name of the just.</param>
- /// <param name="year">The year.</param>
- protected void ParseName(string name, out string justName, out int? year)
- {
- justName = null;
- year = null;
- foreach (var re in NameMatches)
- {
- Match m = re.Match(name);
- if (m.Success)
- {
- justName = m.Groups["name"].Value.Trim();
- string y = m.Groups["year"] != null ? m.Groups["year"].Value : null;
- int temp;
- year = Int32.TryParse(y, out temp) ? temp : (int?)null;
- break;
- }
- }
- }
- /// <summary>
- /// Finds the id.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="cancellationToken">The cancellation token</param>
- /// <returns>Task{System.String}.</returns>
- public async Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
- {
- int? yearInName;
- string name = item.Name;
- ParseName(name, out name, out yearInName);
- var year = item.ProductionYear ?? yearInName;
- Logger.Info("MovieDbProvider: Finding id for item: " + name);
- string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
- //if we are a boxset - look at our first child
- var boxset = item as BoxSet;
- if (boxset != null)
- {
- // See if any movies have a collection id already
- var collId = boxset.Children.Concat(boxset.GetLinkedChildren()).OfType<Video>()
- .Select(i => i.GetProviderId(MetadataProviders.TmdbCollection))
- .FirstOrDefault(i => i != null);
- if (collId != null) return collId;
- }
- //nope - search for it
- var searchType = item is BoxSet ? "collection" : "movie";
- var id = await AttemptFindId(name, searchType, year, language, cancellationToken).ConfigureAwait(false);
- if (id == null)
- {
- //try in english if wasn't before
- if (language != "en")
- {
- id = await AttemptFindId(name, searchType, year, "en", cancellationToken).ConfigureAwait(false);
- }
- else
- {
- // try with dot and _ turned to space
- var originalName = name;
- name = name.Replace(",", " ");
- name = name.Replace(".", " ");
- name = name.Replace("_", " ");
- name = name.Replace("-", "");
- // Search again if the new name is different
- if (!string.Equals(name, originalName))
- {
- id = await AttemptFindId(name, searchType, year, language, cancellationToken).ConfigureAwait(false);
- if (id == null && language != "en")
- {
- //one more time, in english
- id = await AttemptFindId(name, searchType, year, "en", cancellationToken).ConfigureAwait(false);
- }
- }
- if (id == null && item.LocationType == LocationType.FileSystem)
- {
- //last resort - try using the actual folder name
- var pathName = Path.GetFileName(item.ResolveArgs.Path);
- // Only search if it's a name we haven't already tried.
- if (!string.Equals(pathName, name, StringComparison.OrdinalIgnoreCase)
- && !string.Equals(pathName, originalName, StringComparison.OrdinalIgnoreCase))
- {
- id = await AttemptFindId(pathName, searchType, year, "en", cancellationToken).ConfigureAwait(false);
- }
- }
- }
- }
- return id;
- }
- /// <summary>
- /// Attempts the find id.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <param name="type">movie or collection</param>
- /// <param name="year">The year.</param>
- /// <param name="language">The language.</param>
- /// <param name="cancellationToken">The cancellation token</param>
- /// <returns>Task{System.String}.</returns>
- private async Task<string> AttemptFindId(string name, string type, int? year, string language, CancellationToken cancellationToken)
- {
- string url3 = string.Format(Search3, UrlEncode(name), ApiKey, language, type);
- TmdbMovieSearchResults searchResult = null;
- using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
- {
- Url = url3,
- CancellationToken = cancellationToken,
- AcceptHeader = AcceptHeader
- }).ConfigureAwait(false))
- {
- searchResult = JsonSerializer.DeserializeFromStream<TmdbMovieSearchResults>(json);
- }
- if (searchResult != null)
- {
- foreach (var possible in searchResult.results)
- {
- string matchedName = possible.title ?? possible.name;
- string id = possible.id.ToString(CultureInfo.InvariantCulture);
- if (matchedName != null)
- {
- Logger.Debug("Match " + matchedName + " for " + name);
- if (year != null)
- {
- DateTime r;
- //These dates are always in this exact format
- if (DateTime.TryParseExact(possible.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r))
- {
- if (Math.Abs(r.Year - year.Value) > 1) // allow a 1 year tolerance on release date
- {
- Logger.Debug("Result " + matchedName + " released on " + r + " did not match year " + year);
- continue;
- }
- }
- }
- //matched name and year
- return id;
- }
- }
- }
- return null;
- }
- /// <summary>
- /// URLs the encode.
- /// </summary>
- /// <param name="name">The name.</param>
- /// <returns>System.String.</returns>
- private static string UrlEncode(string name)
- {
- return WebUtility.UrlEncode(name);
- }
- private readonly CultureInfo _usCulture = new CultureInfo("en-US");
- /// <summary>
- /// Fetches the movie data.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="id">The id.</param>
- /// <param name="isForcedRefresh">if set to <c>true</c> [is forced refresh].</param>
- /// <param name="cancellationToken">The cancellation token</param>
- /// <returns>Task.</returns>
- private async Task FetchMovieData(BaseItem item, string id, bool isForcedRefresh, CancellationToken cancellationToken)
- {
- // Id could be ImdbId or TmdbId
- var language = ConfigurationManager.Configuration.PreferredMetadataLanguage;
- var dataFilePath = GetDataFilePath(item, language);
- var hasAltMeta = HasAltMeta(item);
- var isRefreshingDueToTmdbUpdate = hasAltMeta && !isForcedRefresh;
- if (string.IsNullOrEmpty(dataFilePath) || !File.Exists(dataFilePath))
- {
- var isBoxSet = item is BoxSet;
- var mainResult = await FetchMainResult(id, isBoxSet, cancellationToken).ConfigureAwait(false);
- if (mainResult == null) return;
- var path = GetMovieDataPath(ConfigurationManager.ApplicationPaths, isBoxSet, mainResult.id.ToString(_usCulture));
- dataFilePath = Path.Combine(path, language + ".json");
- var directory = Path.GetDirectoryName(dataFilePath);
- Directory.CreateDirectory(directory);
- JsonSerializer.SerializeToFile(mainResult, dataFilePath);
- isRefreshingDueToTmdbUpdate = false;
- }
- if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !hasAltMeta)
- {
- dataFilePath = GetDataFilePath(item, language);
- var mainResult = JsonSerializer.DeserializeFromFile<CompleteMovieData>(dataFilePath);
- ProcessMainInfo(item, mainResult, isRefreshingDueToTmdbUpdate);
- }
- }
- /// <summary>
- /// Downloads the movie info.
- /// </summary>
- /// <param name="id">The id.</param>
- /// <param name="isBoxSet">if set to <c>true</c> [is box set].</param>
- /// <param name="dataPath">The data path.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task.</returns>
- internal async Task DownloadMovieInfo(string id, bool isBoxSet, string dataPath, CancellationToken cancellationToken)
- {
- var language = ConfigurationManager.Configuration.PreferredMetadataLanguage;
- var mainResult = await FetchMainResult(id, isBoxSet, cancellationToken).ConfigureAwait(false);
- if (mainResult == null) return;
- var dataFilePath = Path.Combine(dataPath, language + ".json");
- Directory.CreateDirectory(dataPath);
- JsonSerializer.SerializeToFile(mainResult, dataFilePath);
- }
- /// <summary>
- /// Gets the data file path.
- /// </summary>
- /// <param name="item">The item.</param>
- /// <param name="language">The language.</param>
- /// <returns>System.String.</returns>
- private string GetDataFilePath(BaseItem item, string language)
- {
- var id = item.GetProviderId(MetadataProviders.Tmdb);
- if (string.IsNullOrEmpty(id))
- {
- return null;
- }
- var path = GetMovieDataPath(ConfigurationManager.ApplicationPaths, item is BoxSet, id);
- path = Path.Combine(path, language + ".json");
- return path;
- }
- /// <summary>
- /// Fetches the main result.
- /// </summary>
- /// <param name="id">The id.</param>
- /// <param name="isBoxSet">if set to <c>true</c> [is box set].</param>
- /// <param name="cancellationToken">The cancellation token</param>
- /// <returns>Task{CompleteMovieData}.</returns>
- protected async Task<CompleteMovieData> FetchMainResult(string id, bool isBoxSet, CancellationToken cancellationToken)
- {
- var baseUrl = isBoxSet ? GetBoxSetInfo3 : GetMovieInfo3;
- string url = string.Format(baseUrl, id, ApiKey, ConfigurationManager.Configuration.PreferredMetadataLanguage);
- CompleteMovieData mainResult;
- cancellationToken.ThrowIfCancellationRequested();
- using (var json = await GetMovieDbResponse(new HttpRequestOptions
- {
- Url = url,
- CancellationToken = cancellationToken,
- AcceptHeader = AcceptHeader
- }).ConfigureAwait(false))
- {
- mainResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
- }
- cancellationToken.ThrowIfCancellationRequested();
- if (mainResult != null && string.IsNullOrEmpty(mainResult.overview))
- {
- if (ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower() != "en")
- {
- Logger.Info("MovieDbProvider couldn't find meta for language " + ConfigurationManager.Configuration.PreferredMetadataLanguage + ". Trying English...");
- url = string.Format(baseUrl, id, ApiKey, "en");
- using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
- {
- Url = url,
- CancellationToken = cancellationToken,
- AcceptHeader = AcceptHeader
- }).ConfigureAwait(false))
- {
- mainResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
- }
- if (String.IsNullOrEmpty(mainResult.overview))
- {
- Logger.Error("MovieDbProvider - Unable to find information for (id:" + id + ")");
- return null;
- }
- }
- }
- return mainResult;
- }
- /// <summary>
- /// Processes the main info.
- /// </summary>
- /// <param name="movie">The movie.</param>
- /// <param name="movieData">The movie data.</param>
- /// <param name="isRefreshingDueToTmdbUpdate">if set to <c>true</c> [is refreshing due to TMDB update].</param>
- protected virtual void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData, bool isRefreshingDueToTmdbUpdate)
- {
- if (movie != null && movieData != null)
- {
- if (!movie.LockedFields.Contains(MetadataFields.Name))
- {
- movie.Name = movieData.title ?? movieData.original_title ?? movieData.name ?? movie.Name;
- }
- if (!movie.LockedFields.Contains(MetadataFields.Overview))
- {
- movie.Overview = WebUtility.HtmlDecode(movieData.overview);
- movie.Overview = movie.Overview != null ? movie.Overview.Replace("\n\n", "\n") : null;
- }
- movie.HomePageUrl = movieData.homepage;
- movie.Budget = movieData.budget;
- movie.Revenue = movieData.revenue;
- if (!string.IsNullOrEmpty(movieData.tagline))
- {
- movie.Taglines.Clear();
- movie.AddTagline(movieData.tagline);
- }
- movie.SetProviderId(MetadataProviders.Imdb, movieData.imdb_id);
- if (movieData.belongs_to_collection != null)
- {
- movie.SetProviderId(MetadataProviders.TmdbCollection, movieData.belongs_to_collection.id.ToString(CultureInfo.InvariantCulture));
- }
- else
- {
- movie.SetProviderId(MetadataProviders.TmdbCollection, null); // clear out any old entry
- }
- float rating;
- string voteAvg = movieData.vote_average.ToString(CultureInfo.InvariantCulture);
- // tmdb appears to have unified their numbers to always report "7.3" regardless of country
- // so I removed the culture-specific processing here because it was not working for other countries -ebr
- // Don't import this when responding to tmdb updates because we don't want to blow away imdb data
- if (!isRefreshingDueToTmdbUpdate && float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out rating))
- {
- movie.CommunityRating = rating;
- }
- // Don't import this when responding to tmdb updates because we don't want to blow away imdb data
- if (!isRefreshingDueToTmdbUpdate)
- {
- movie.VoteCount = movieData.vote_count;
- }
- //release date and certification are retrieved based on configured country and we fall back on US if not there and to minimun release date if still no match
- if (movieData.releases != null && movieData.releases.countries != null)
- {
- var ourRelease = movieData.releases.countries.FirstOrDefault(c => c.iso_3166_1.Equals(ConfigurationManager.Configuration.MetadataCountryCode, StringComparison.OrdinalIgnoreCase)) ?? new Country();
- var usRelease = movieData.releases.countries.FirstOrDefault(c => c.iso_3166_1.Equals("US", StringComparison.OrdinalIgnoreCase)) ?? new Country();
- var minimunRelease = movieData.releases.countries.OrderBy(c => c.release_date).FirstOrDefault() ?? new Country();
- if (!movie.LockedFields.Contains(MetadataFields.OfficialRating))
- {
- var ratingPrefix = ConfigurationManager.Configuration.MetadataCountryCode.Equals("us", StringComparison.OrdinalIgnoreCase) ? "" : ConfigurationManager.Configuration.MetadataCountryCode + "-";
- movie.OfficialRating = !string.IsNullOrEmpty(ourRelease.certification)
- ? ratingPrefix + ourRelease.certification
- : !string.IsNullOrEmpty(usRelease.certification)
- ? usRelease.certification
- : !string.IsNullOrEmpty(minimunRelease.certification)
- ? minimunRelease.iso_3166_1 + "-" + minimunRelease.certification
- : null;
- }
- if (ourRelease.release_date != default(DateTime))
- {
- if (ourRelease.release_date.Year != 1)
- {
- movie.PremiereDate = ourRelease.release_date.ToUniversalTime();
- movie.ProductionYear = ourRelease.release_date.Year;
- }
- }
- else if (usRelease.release_date != default(DateTime))
- {
- if (usRelease.release_date.Year != 1)
- {
- movie.PremiereDate = usRelease.release_date.ToUniversalTime();
- movie.ProductionYear = usRelease.release_date.Year;
- }
- }
- else if (minimunRelease.release_date != default(DateTime))
- {
- if (minimunRelease.release_date.Year != 1)
- {
- movie.PremiereDate = minimunRelease.release_date.ToUniversalTime();
- movie.ProductionYear = minimunRelease.release_date.Year;
- }
- }
- }
- else
- {
- if (movieData.release_date.Year != 1)
- {
- //no specific country release info at all
- movie.PremiereDate = movieData.release_date.ToUniversalTime();
- movie.ProductionYear = movieData.release_date.Year;
- }
- }
- //if that didn't find a rating and we are a boxset, use the one from our first child
- if (movie.OfficialRating == null && movie is BoxSet && !movie.LockedFields.Contains(MetadataFields.OfficialRating))
- {
- var boxset = movie as BoxSet;
- Logger.Info("MovieDbProvider - Using rating of first child of boxset...");
- var firstChild = boxset.Children.Concat(boxset.GetLinkedChildren()).FirstOrDefault();
- boxset.OfficialRating = firstChild != null ? firstChild.OfficialRating : null;
- }
- if (movieData.runtime > 0)
- movie.OriginalRunTimeTicks = TimeSpan.FromMinutes(movieData.runtime).Ticks;
- //studios
- if (movieData.production_companies != null && !movie.LockedFields.Contains(MetadataFields.Studios))
- {
- movie.Studios.Clear();
- foreach (var studio in movieData.production_companies.Select(c => c.name))
- {
- movie.AddStudio(studio);
- }
- }
- // genres
- // Don't import this when responding to tmdb updates because we don't want to blow away imdb data
- if (movieData.genres != null && !movie.LockedFields.Contains(MetadataFields.Genres) && !isRefreshingDueToTmdbUpdate)
- {
- movie.Genres.Clear();
- foreach (var genre in movieData.genres.Select(g => g.name))
- {
- movie.AddGenre(genre);
- }
- }
- if (!movie.LockedFields.Contains(MetadataFields.Cast))
- {
- movie.People.Clear();
- //Actors, Directors, Writers - all in People
- //actors come from cast
- if (movieData.casts != null && movieData.casts.cast != null)
- {
- foreach (var actor in movieData.casts.cast.OrderBy(a => a.order)) movie.AddPerson(new PersonInfo { Name = actor.name.Trim(), Role = actor.character, Type = PersonType.Actor });
- }
- //and the rest from crew
- if (movieData.casts != null && movieData.casts.crew != null)
- {
- foreach (var person in movieData.casts.crew) movie.AddPerson(new PersonInfo { Name = person.name.Trim(), Role = person.job, Type = person.department });
- }
- }
- if (movieData.keywords != null && movieData.keywords.keywords != null && !movie.LockedFields.Contains(MetadataFields.Tags))
- {
- movie.Tags = movieData.keywords.keywords.Select(i => i.name).ToList();
- }
- if (movieData.trailers != null && movieData.trailers.youtube != null &&
- movieData.trailers.youtube.Count > 0)
- {
- movie.RemoteTrailers = movieData.trailers.youtube.Select(i => new MediaUrl
- {
- Url = string.Format("http://www.youtube.com/watch?v={0}", i.source),
- IsDirectLink = false,
- Name = i.name,
- VideoSize = string.Equals("hd", i.size, StringComparison.OrdinalIgnoreCase) ? VideoSize.HighDefinition : VideoSize.StandardDefinition
- }).ToList();
- }
- }
- }
- private DateTime _lastRequestDate = DateTime.MinValue;
- /// <summary>
- /// Gets the movie db response.
- /// </summary>
- internal async Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
- {
- var cancellationToken = options.CancellationToken;
- await MovieDbResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
- try
- {
- // Limit to three requests per second
- var diff = 340 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
- if (diff > 0)
- {
- await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
- }
- _lastRequestDate = DateTime.Now;
- return await HttpClient.Get(options).ConfigureAwait(false);
- }
- finally
- {
- _lastRequestDate = DateTime.Now;
- MovieDbResourcePool.Release();
- }
- }
- #region Result Objects
- /// <summary>
- /// Class TmdbTitle
- /// </summary>
- protected class TmdbTitle
- {
- /// <summary>
- /// Gets or sets the iso_3166_1.
- /// </summary>
- /// <value>The iso_3166_1.</value>
- public string iso_3166_1 { get; set; }
- /// <summary>
- /// Gets or sets the title.
- /// </summary>
- /// <value>The title.</value>
- public string title { get; set; }
- }
- /// <summary>
- /// Class TmdbAltTitleResults
- /// </summary>
- protected class TmdbAltTitleResults
- {
- /// <summary>
- /// Gets or sets the id.
- /// </summary>
- /// <value>The id.</value>
- public int id { get; set; }
- /// <summary>
- /// Gets or sets the titles.
- /// </summary>
- /// <value>The titles.</value>
- public List<TmdbTitle> titles { get; set; }
- }
- /// <summary>
- /// Class TmdbMovieSearchResult
- /// </summary>
- protected class TmdbMovieSearchResult
- {
- /// <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>
- /// 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>
- protected 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; }
- }
- protected class BelongsToCollection
- {
- public int id { get; set; }
- public string name { get; set; }
- public string poster_path { get; set; }
- public string backdrop_path { get; set; }
- }
- protected class GenreItem
- {
- public int id { get; set; }
- public string name { get; set; }
- }
- protected class ProductionCompany
- {
- public string name { get; set; }
- public int id { get; set; }
- }
- protected class ProductionCountry
- {
- public string iso_3166_1 { get; set; }
- public string name { get; set; }
- }
- protected class SpokenLanguage
- {
- public string iso_639_1 { get; set; }
- public string name { get; set; }
- }
- protected class Cast
- {
- public int id { get; set; }
- public string name { get; set; }
- public string character { get; set; }
- public int order { get; set; }
- public int cast_id { get; set; }
- public string profile_path { get; set; }
- }
- protected class Crew
- {
- public int id { get; set; }
- public string name { get; set; }
- public string department { get; set; }
- public string job { get; set; }
- public string profile_path { get; set; }
- }
- protected class Casts
- {
- public List<Cast> cast { get; set; }
- public List<Crew> crew { get; set; }
- }
- protected class Country
- {
- public string iso_3166_1 { get; set; }
- public string certification { get; set; }
- public DateTime release_date { get; set; }
- }
- protected class Releases
- {
- public List<Country> countries { get; set; }
- }
- protected class Keyword
- {
- public int id { get; set; }
- public string name { get; set; }
- }
- protected class Keywords
- {
- public List<Keyword> keywords { get; set; }
- }
- protected class CompleteMovieData
- {
- public bool adult { get; set; }
- public string backdrop_path { get; set; }
- public BelongsToCollection belongs_to_collection { get; set; }
- public int budget { get; set; }
- public List<GenreItem> genres { get; set; }
- public string homepage { get; set; }
- public int id { get; set; }
- public string imdb_id { get; set; }
- public string name { get; set; }
- public string original_title { get; set; }
- public string overview { get; set; }
- public double popularity { get; set; }
- public string poster_path { get; set; }
- public List<ProductionCompany> production_companies { get; set; }
- public List<ProductionCountry> production_countries { get; set; }
- public DateTime release_date { get; set; }
- public int revenue { get; set; }
- public int runtime { get; set; }
- public List<SpokenLanguage> spoken_languages { get; set; }
- public string status { get; set; }
- public string tagline { get; set; }
- public string title { get; set; }
- public double vote_average { get; set; }
- public int vote_count { get; set; }
- public Casts casts { get; set; }
- public Releases releases { get; set; }
- public Keywords keywords { get; set; }
- public Trailers trailers { get; set; }
- }
- public class Trailers
- {
- public List<Youtube> youtube { get; set; }
- }
- public class Youtube
- {
- public string name { get; set; }
- public string size { get; set; }
- public string source { get; set; }
- }
- public class TmdbImageSettings
- {
- public List<string> backdrop_sizes { get; set; }
- public string base_url { get; set; }
- public List<string> poster_sizes { get; set; }
- public List<string> profile_sizes { get; set; }
- }
- public class TmdbSettingsResult
- {
- public TmdbImageSettings images { get; set; }
- }
- #endregion
- public void Dispose()
- {
- Dispose(true);
- }
- }
- }
|