use tmdb updates for images

This commit is contained in:
Luke Pulverenti 2013-10-22 15:52:33 -04:00
parent 32cb872b06
commit c997effcb1
3 changed files with 133 additions and 170 deletions

View File

@ -9,6 +9,7 @@ using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -142,6 +143,23 @@ namespace MediaBrowser.Providers.Movies
return base.NeedsRefreshInternal(item, providerInfo); return base.NeedsRefreshInternal(item, providerInfo);
} }
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
{
var path = MovieDbProvider.Current.GetDataFilePath(item, "default");
if (!string.IsNullOrEmpty(path))
{
var fileInfo = new FileInfo(path);
if (fileInfo.Exists)
{
return fileInfo.LastWriteTimeUtc > providerInfo.LastRefreshed;
}
}
return false;
}
/// <summary> /// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary> /// </summary>
@ -151,18 +169,15 @@ namespace MediaBrowser.Providers.Movies
/// <returns>Task{System.Boolean}.</returns> /// <returns>Task{System.Boolean}.</returns>
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{ {
BaseProviderInfo data; var images = FetchImages(item, item.GetProviderId(MetadataProviders.Tmdb), cancellationToken);
if (!item.ProviderData.TryGetValue(Id, out data)) var status = ProviderRefreshStatus.Success;
if (images != null)
{ {
data = new BaseProviderInfo(); status = await ProcessImages(item, images, cancellationToken).ConfigureAwait(false);
item.ProviderData[Id] = data;
} }
var images = await FetchImages(item, item.GetProviderId(MetadataProviders.Tmdb), cancellationToken).ConfigureAwait(false);
var status = await ProcessImages(item, images, cancellationToken).ConfigureAwait(false);
SetLastRefreshed(item, DateTime.UtcNow, status); SetLastRefreshed(item, DateTime.UtcNow, status);
return true; return true;
} }
@ -174,20 +189,23 @@ namespace MediaBrowser.Providers.Movies
/// <param name="id">The id.</param> /// <param name="id">The id.</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{MovieImages}.</returns> /// <returns>Task{MovieImages}.</returns>
private async Task<MovieImages> FetchImages(BaseItem item, string id, CancellationToken cancellationToken) private MovieDbProvider.Images FetchImages(BaseItem item, string id, CancellationToken cancellationToken)
{ {
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions var path = MovieDbProvider.Current.GetDataFilePath(item, "default");
{
Url = string.Format(GetImages, id, MovieDbProvider.ApiKey, item is BoxSet ? "collection" : "movie"),
CancellationToken = cancellationToken,
AcceptHeader = MovieDbProvider.AcceptHeader
}).ConfigureAwait(false)) if (!string.IsNullOrEmpty(path))
{ {
return _jsonSerializer.DeserializeFromStream<MovieImages>(json); var fileInfo = new FileInfo(path);
if (fileInfo.Exists)
{
return _jsonSerializer.DeserializeFromFile<MovieDbProvider.CompleteMovieData>(path).images;
} }
} }
return null;
}
/// <summary> /// <summary>
/// Processes the images. /// Processes the images.
/// </summary> /// </summary>
@ -195,14 +213,14 @@ namespace MediaBrowser.Providers.Movies
/// <param name="images">The images.</param> /// <param name="images">The images.</param>
/// <param name="cancellationToken">The cancellation token</param> /// <param name="cancellationToken">The cancellation token</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
protected virtual async Task<ProviderRefreshStatus> ProcessImages(BaseItem item, MovieImages images, CancellationToken cancellationToken) private async Task<ProviderRefreshStatus> ProcessImages(BaseItem item, MovieDbProvider.Images images, CancellationToken cancellationToken)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var status = ProviderRefreshStatus.Success; var status = ProviderRefreshStatus.Success;
var eligiblePosters = images.posters == null ? var eligiblePosters = images.posters == null ?
new List<Poster>() : new List<MovieDbProvider.Poster>() :
images.posters.Where(i => i.width >= ConfigurationManager.Configuration.MinMoviePosterWidth) images.posters.Where(i => i.width >= ConfigurationManager.Configuration.MinMoviePosterWidth)
.ToList(); .ToList();
@ -255,7 +273,7 @@ namespace MediaBrowser.Providers.Movies
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
var eligibleBackdrops = images.backdrops == null ? new List<Backdrop>() : var eligibleBackdrops = images.backdrops == null ? new List<MovieDbProvider.Backdrop>() :
images.backdrops.Where(i => i.width >= ConfigurationManager.Configuration.MinMovieBackdropWidth) images.backdrops.Where(i => i.width >= ConfigurationManager.Configuration.MinMovieBackdropWidth)
.ToList(); .ToList();
@ -294,107 +312,5 @@ namespace MediaBrowser.Providers.Movies
return status; return status;
} }
/// <summary>
/// Class Backdrop
/// </summary>
protected class Backdrop
{
/// <summary>
/// Gets or sets the file_path.
/// </summary>
/// <value>The file_path.</value>
public string file_path { get; set; }
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>The width.</value>
public int width { get; set; }
/// <summary>
/// Gets or sets the height.
/// </summary>
/// <value>The height.</value>
public int height { get; set; }
/// <summary>
/// Gets or sets the iso_639_1.
/// </summary>
/// <value>The iso_639_1.</value>
public string iso_639_1 { get; set; }
/// <summary>
/// Gets or sets the aspect_ratio.
/// </summary>
/// <value>The aspect_ratio.</value>
public double aspect_ratio { get; set; }
/// <summary>
/// Gets or sets the vote_average.
/// </summary>
/// <value>The vote_average.</value>
public double vote_average { get; set; }
/// <summary>
/// Gets or sets the vote_count.
/// </summary>
/// <value>The vote_count.</value>
public int vote_count { get; set; }
}
/// <summary>
/// Class Poster
/// </summary>
protected class Poster
{
/// <summary>
/// Gets or sets the file_path.
/// </summary>
/// <value>The file_path.</value>
public string file_path { get; set; }
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>The width.</value>
public int width { get; set; }
/// <summary>
/// Gets or sets the height.
/// </summary>
/// <value>The height.</value>
public int height { get; set; }
/// <summary>
/// Gets or sets the iso_639_1.
/// </summary>
/// <value>The iso_639_1.</value>
public string iso_639_1 { get; set; }
/// <summary>
/// Gets or sets the aspect_ratio.
/// </summary>
/// <value>The aspect_ratio.</value>
public double aspect_ratio { get; set; }
/// <summary>
/// Gets or sets the vote_average.
/// </summary>
/// <value>The vote_average.</value>
public double vote_average { get; set; }
/// <summary>
/// Gets or sets the vote_count.
/// </summary>
/// <value>The vote_count.</value>
public int vote_count { get; set; }
}
/// <summary>
/// Class MovieImages
/// </summary>
protected class MovieImages
{
/// <summary>
/// Gets or sets the backdrops.
/// </summary>
/// <value>The backdrops.</value>
public List<Backdrop> backdrops { get; set; }
/// <summary>
/// Gets or sets the posters.
/// </summary>
/// <value>The posters.</value>
public List<Poster> posters { get; set; }
}
} }
} }

View File

@ -181,8 +181,8 @@ namespace MediaBrowser.Providers.Movies
private const string TmdbConfigUrl = "http://api.themoviedb.org/3/configuration?api_key={0}"; 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 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 GetMovieInfo3 = @"http://api.themoviedb.org/3/movie/{0}?api_key={1}&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"; private const string GetBoxSetInfo3 = @"http://api.themoviedb.org/3/collection/{0}?api_key={1}&append_to_response=images";
internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669"; internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669";
internal static string AcceptHeader = "application/json,image/*"; internal static string AcceptHeader = "application/json,image/*";
@ -517,15 +517,22 @@ namespace MediaBrowser.Providers.Movies
if (mainResult == null) return; if (mainResult == null) return;
var path = GetMovieDataPath(ConfigurationManager.ApplicationPaths, isBoxSet, mainResult.id.ToString(_usCulture)); var movieDataPath = GetMovieDataPath(ConfigurationManager.ApplicationPaths, isBoxSet, mainResult.id.ToString(_usCulture));
dataFilePath = Path.Combine(path, language + ".json"); dataFilePath = Path.Combine(movieDataPath, language + ".json");
var directory = Path.GetDirectoryName(dataFilePath); var directory = Path.GetDirectoryName(dataFilePath);
Directory.CreateDirectory(directory); Directory.CreateDirectory(directory);
JsonSerializer.SerializeToFile(mainResult, dataFilePath); JsonSerializer.SerializeToFile(mainResult, dataFilePath);
// Now get the language-less version
mainResult = await FetchMainResult(id, isBoxSet, null, cancellationToken).ConfigureAwait(false);
dataFilePath = Path.Combine(movieDataPath, "default.json");
JsonSerializer.SerializeToFile(mainResult, dataFilePath);
} }
if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !hasAltMeta) if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !hasAltMeta)
@ -559,6 +566,13 @@ namespace MediaBrowser.Providers.Movies
Directory.CreateDirectory(dataPath); Directory.CreateDirectory(dataPath);
JsonSerializer.SerializeToFile(mainResult, dataFilePath); JsonSerializer.SerializeToFile(mainResult, dataFilePath);
// Now get the language-less version
mainResult = await FetchMainResult(id, isBoxSet, null, cancellationToken).ConfigureAwait(false);
dataFilePath = Path.Combine(dataPath, "default.json");
JsonSerializer.SerializeToFile(mainResult, dataFilePath);
} }
/// <summary> /// <summary>
@ -567,7 +581,7 @@ namespace MediaBrowser.Providers.Movies
/// <param name="item">The item.</param> /// <param name="item">The item.</param>
/// <param name="language">The language.</param> /// <param name="language">The language.</param>
/// <returns>System.String.</returns> /// <returns>System.String.</returns>
private string GetDataFilePath(BaseItem item, string language) internal string GetDataFilePath(BaseItem item, string language)
{ {
var id = item.GetProviderId(MetadataProviders.Tmdb); var id = item.GetProviderId(MetadataProviders.Tmdb);
@ -591,11 +605,17 @@ namespace MediaBrowser.Providers.Movies
/// <param name="language">The language.</param> /// <param name="language">The language.</param>
/// <param name="cancellationToken">The cancellation token</param> /// <param name="cancellationToken">The cancellation token</param>
/// <returns>Task{CompleteMovieData}.</returns> /// <returns>Task{CompleteMovieData}.</returns>
protected async Task<CompleteMovieData> FetchMainResult(string id, bool isBoxSet, string language, CancellationToken cancellationToken) private async Task<CompleteMovieData> FetchMainResult(string id, bool isBoxSet, string language, CancellationToken cancellationToken)
{ {
var baseUrl = isBoxSet ? GetBoxSetInfo3 : GetMovieInfo3; var baseUrl = isBoxSet ? GetBoxSetInfo3 : GetMovieInfo3;
string url = string.Format(baseUrl, id, ApiKey, language); var url = string.Format(baseUrl, id, ApiKey);
if (!string.IsNullOrEmpty(language))
{
url += "&language=" + language;
}
CompleteMovieData mainResult; CompleteMovieData mainResult;
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@ -615,7 +635,7 @@ namespace MediaBrowser.Providers.Movies
if (mainResult != null && string.IsNullOrEmpty(mainResult.overview)) if (mainResult != null && string.IsNullOrEmpty(mainResult.overview))
{ {
if (language.ToLower() != "en") if (!string.IsNullOrEmpty(language) && !string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
{ {
Logger.Info("MovieDbProvider couldn't find meta for language " + language + ". Trying English..."); Logger.Info("MovieDbProvider couldn't find meta for language " + language + ". Trying English...");
@ -647,7 +667,7 @@ namespace MediaBrowser.Providers.Movies
/// </summary> /// </summary>
/// <param name="movie">The movie.</param> /// <param name="movie">The movie.</param>
/// <param name="movieData">The movie data.</param> /// <param name="movieData">The movie data.</param>
protected virtual void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData) private void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData)
{ {
if (movie != null && movieData != null) if (movie != null && movieData != null)
{ {
@ -871,13 +891,15 @@ namespace MediaBrowser.Providers.Movies
} }
} }
#region Result Objects public void Dispose()
{
Dispose(true);
}
/// <summary> /// <summary>
/// Class TmdbTitle /// Class TmdbTitle
/// </summary> /// </summary>
protected class TmdbTitle internal class TmdbTitle
{ {
/// <summary> /// <summary>
/// Gets or sets the iso_3166_1. /// Gets or sets the iso_3166_1.
@ -894,7 +916,7 @@ namespace MediaBrowser.Providers.Movies
/// <summary> /// <summary>
/// Class TmdbAltTitleResults /// Class TmdbAltTitleResults
/// </summary> /// </summary>
protected class TmdbAltTitleResults internal class TmdbAltTitleResults
{ {
/// <summary> /// <summary>
/// Gets or sets the id. /// Gets or sets the id.
@ -911,7 +933,7 @@ namespace MediaBrowser.Providers.Movies
/// <summary> /// <summary>
/// Class TmdbMovieSearchResult /// Class TmdbMovieSearchResult
/// </summary> /// </summary>
protected class TmdbMovieSearchResult internal class TmdbMovieSearchResult
{ {
/// <summary> /// <summary>
/// Gets or sets a value indicating whether this <see cref="TmdbMovieSearchResult" /> is adult. /// Gets or sets a value indicating whether this <see cref="TmdbMovieSearchResult" /> is adult.
@ -972,7 +994,7 @@ namespace MediaBrowser.Providers.Movies
/// <summary> /// <summary>
/// Class TmdbMovieSearchResults /// Class TmdbMovieSearchResults
/// </summary> /// </summary>
protected class TmdbMovieSearchResults internal class TmdbMovieSearchResults
{ {
/// <summary> /// <summary>
/// Gets or sets the page. /// Gets or sets the page.
@ -996,7 +1018,7 @@ namespace MediaBrowser.Providers.Movies
public int total_results { get; set; } public int total_results { get; set; }
} }
protected class BelongsToCollection internal class BelongsToCollection
{ {
public int id { get; set; } public int id { get; set; }
public string name { get; set; } public string name { get; set; }
@ -1004,31 +1026,31 @@ namespace MediaBrowser.Providers.Movies
public string backdrop_path { get; set; } public string backdrop_path { get; set; }
} }
protected class GenreItem internal class GenreItem
{ {
public int id { get; set; } public int id { get; set; }
public string name { get; set; } public string name { get; set; }
} }
protected class ProductionCompany internal class ProductionCompany
{ {
public string name { get; set; } public string name { get; set; }
public int id { get; set; } public int id { get; set; }
} }
protected class ProductionCountry internal class ProductionCountry
{ {
public string iso_3166_1 { get; set; } public string iso_3166_1 { get; set; }
public string name { get; set; } public string name { get; set; }
} }
protected class SpokenLanguage internal class SpokenLanguage
{ {
public string iso_639_1 { get; set; } public string iso_639_1 { get; set; }
public string name { get; set; } public string name { get; set; }
} }
protected class Cast internal class Cast
{ {
public int id { get; set; } public int id { get; set; }
public string name { get; set; } public string name { get; set; }
@ -1038,7 +1060,7 @@ namespace MediaBrowser.Providers.Movies
public string profile_path { get; set; } public string profile_path { get; set; }
} }
protected class Crew internal class Crew
{ {
public int id { get; set; } public int id { get; set; }
public string name { get; set; } public string name { get; set; }
@ -1047,36 +1069,77 @@ namespace MediaBrowser.Providers.Movies
public string profile_path { get; set; } public string profile_path { get; set; }
} }
protected class Casts internal class Casts
{ {
public List<Cast> cast { get; set; } public List<Cast> cast { get; set; }
public List<Crew> crew { get; set; } public List<Crew> crew { get; set; }
} }
protected class Country internal class Country
{ {
public string iso_3166_1 { get; set; } public string iso_3166_1 { get; set; }
public string certification { get; set; } public string certification { get; set; }
public DateTime release_date { get; set; } public DateTime release_date { get; set; }
} }
protected class Releases internal class Releases
{ {
public List<Country> countries { get; set; } public List<Country> countries { get; set; }
} }
protected class Keyword internal class Backdrop
{
public string file_path { get; set; }
public int width { get; set; }
public int height { get; set; }
public object iso_639_1 { get; set; }
public double aspect_ratio { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
}
internal class Poster
{
public string file_path { get; set; }
public int width { get; set; }
public int height { get; set; }
public string iso_639_1 { get; set; }
public double aspect_ratio { get; set; }
public double vote_average { get; set; }
public int vote_count { get; set; }
}
internal class Images
{
public List<Backdrop> backdrops { get; set; }
public List<Poster> posters { get; set; }
}
internal class Keyword
{ {
public int id { get; set; } public int id { get; set; }
public string name { get; set; } public string name { get; set; }
} }
protected class Keywords internal class Keywords
{ {
public List<Keyword> keywords { get; set; } public List<Keyword> keywords { get; set; }
} }
protected class CompleteMovieData internal class Youtube
{
public string name { get; set; }
public string size { get; set; }
public string source { get; set; }
}
internal class Trailers
{
public List<object> quicktime { get; set; }
public List<Youtube> youtube { get; set; }
}
internal class CompleteMovieData
{ {
public bool adult { get; set; } public bool adult { get; set; }
public string backdrop_path { get; set; } public string backdrop_path { get; set; }
@ -1086,7 +1149,6 @@ namespace MediaBrowser.Providers.Movies
public string homepage { get; set; } public string homepage { get; set; }
public int id { get; set; } public int id { get; set; }
public string imdb_id { get; set; } public string imdb_id { get; set; }
public string name { get; set; }
public string original_title { get; set; } public string original_title { get; set; }
public string overview { get; set; } public string overview { get; set; }
public double popularity { get; set; } public double popularity { get; set; }
@ -1100,27 +1162,17 @@ namespace MediaBrowser.Providers.Movies
public string status { get; set; } public string status { get; set; }
public string tagline { get; set; } public string tagline { get; set; }
public string title { get; set; } public string title { get; set; }
public string name { get; set; }
public double vote_average { get; set; } public double vote_average { get; set; }
public int vote_count { get; set; } public int vote_count { get; set; }
public Casts casts { get; set; } public Casts casts { get; set; }
public Releases releases { get; set; } public Releases releases { get; set; }
public Images images { get; set; }
public Keywords keywords { get; set; } public Keywords keywords { get; set; }
public Trailers trailers { get; set; } public Trailers trailers { get; set; }
} }
public class Trailers internal class TmdbImageSettings
{
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 List<string> backdrop_sizes { get; set; }
public string base_url { get; set; } public string base_url { get; set; }
@ -1128,15 +1180,9 @@ namespace MediaBrowser.Providers.Movies
public List<string> profile_sizes { get; set; } public List<string> profile_sizes { get; set; }
} }
public class TmdbSettingsResult internal class TmdbSettingsResult
{ {
public TmdbImageSettings images { get; set; } public TmdbImageSettings images { get; set; }
} }
#endregion
public void Dispose()
{
Dispose(true);
}
} }
} }

View File

@ -97,6 +97,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators
var sources = additionalBackdrops var sources = additionalBackdrops
.Select(musicArtist.GetImageSourceInfo) .Select(musicArtist.GetImageSourceInfo)
.Where(i => i != null)
.ToList(); .ToList();
foreach (var path in additionalBackdrops) foreach (var path in additionalBackdrops)