2014-02-06 04:39:16 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-09-22 21:56:54 +00:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2014-01-30 21:23:54 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2014-09-22 21:56:54 +00:00
|
|
|
|
using MediaBrowser.Model.Channels;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
using MediaBrowser.Providers.Music;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
public class FanartMovieImageProvider : IRemoteImageProvider, IHasChangeMonitor, IHasOrder
|
2013-11-01 01:48:14 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
private const string FanArtBaseUrl = "http://api.fanart.tv/webservice/movie/{0}/{1}/xml/all/1/1";
|
|
|
|
|
|
|
|
|
|
internal static FanartMovieImageProvider Current;
|
|
|
|
|
|
|
|
|
|
public FanartMovieImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-11-01 01:48:14 +00:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
_httpClient = httpClient;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
|
|
Current = this;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
2013-11-01 15:55:25 +00:00
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
2013-11-01 01:48:14 +00:00
|
|
|
|
{
|
|
|
|
|
get { return "FanArt"; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-11-01 01:48:14 +00:00
|
|
|
|
{
|
2014-01-30 21:23:54 +00:00
|
|
|
|
var trailer = item as Trailer;
|
|
|
|
|
|
|
|
|
|
if (trailer != null)
|
|
|
|
|
{
|
|
|
|
|
return !trailer.IsLocalTrailer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item is Movie || item is BoxSet || item is MusicVideo;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
|
|
|
|
{
|
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary,
|
|
|
|
|
ImageType.Thumb,
|
|
|
|
|
ImageType.Art,
|
|
|
|
|
ImageType.Logo,
|
|
|
|
|
ImageType.Disc,
|
|
|
|
|
ImageType.Banner,
|
|
|
|
|
ImageType.Backdrop
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
|
2013-11-01 01:48:14 +00:00
|
|
|
|
{
|
2013-12-21 18:37:34 +00:00
|
|
|
|
var baseItem = (BaseItem)item;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
var movieId = baseItem.GetProviderId(MetadataProviders.Tmdb);
|
2013-11-01 01:48:14 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(movieId))
|
|
|
|
|
{
|
2014-02-06 04:39:16 +00:00
|
|
|
|
await EnsureMovieXml(movieId, cancellationToken).ConfigureAwait(false);
|
2014-01-30 21:23:54 +00:00
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
|
var xmlPath = GetFanartXmlPath(movieId);
|
2013-11-01 01:48:14 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
AddImages(list, xmlPath, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie. Don't blow up
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 00:23:58 +00:00
|
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
2013-11-01 01:48:14 +00:00
|
|
|
|
|
|
|
|
|
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
|
2013-12-21 18:37:34 +00:00
|
|
|
|
|
2013-11-01 15:55:25 +00:00
|
|
|
|
// Sort first by width to prioritize HD versions
|
2014-01-30 21:23:54 +00:00
|
|
|
|
return list.OrderByDescending(i => i.Width ?? 0)
|
2013-11-01 01:48:14 +00:00
|
|
|
|
.ThenByDescending(i =>
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
if (!isLanguageEn)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(i.Language))
|
|
|
|
|
{
|
|
|
|
|
return isLanguageEn ? 3 : 2;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
})
|
2014-01-30 21:23:54 +00:00
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0);
|
2013-11-01 01:48:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, string xmlPath, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
using (var streamReader = new StreamReader(xmlPath, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "movie":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImages(list, subReader, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "hdmoviecleararts":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Art, 1000, 562);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "hdmovielogos":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Logo, 800, 310);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "moviediscs":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Disc, 1000, 1000);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "movieposters":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Primary, 1000, 1426);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "movielogos":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Logo, 400, 155);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "moviearts":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Art, 500, 281);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "moviethumbs":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Thumb, 1000, 562);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "moviebanners":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Banner, 1000, 185);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case "moviebackgrounds":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Backdrop, 1920, 1080);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2013-11-01 20:34:26 +00:00
|
|
|
|
{
|
|
|
|
|
using (reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-01 01:48:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PopulateImageCategory(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken, ImageType type, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "hdmovielogo":
|
|
|
|
|
case "moviedisc":
|
|
|
|
|
case "hdmovieclearart":
|
|
|
|
|
case "movieposter":
|
|
|
|
|
case "movielogo":
|
|
|
|
|
case "movieart":
|
|
|
|
|
case "moviethumb":
|
|
|
|
|
case "moviebanner":
|
|
|
|
|
case "moviebackground":
|
|
|
|
|
{
|
|
|
|
|
var url = reader.GetAttribute("url");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
var likesString = reader.GetAttribute("likes");
|
|
|
|
|
int likes;
|
|
|
|
|
|
|
|
|
|
var info = new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
RatingType = RatingType.Likes,
|
|
|
|
|
Type = type,
|
|
|
|
|
Width = width,
|
|
|
|
|
Height = height,
|
|
|
|
|
ProviderName = Name,
|
|
|
|
|
Url = url,
|
|
|
|
|
Language = reader.GetAttribute("lang")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
|
|
|
|
|
{
|
|
|
|
|
info.CommunityRating = likes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list.Add(info);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public int Order
|
2013-11-01 01:48:14 +00:00
|
|
|
|
{
|
|
|
|
|
get { return 1; }
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
2014-02-25 15:40:16 +00:00
|
|
|
|
ResourcePool = FanartArtistProvider.Current.FanArtResourcePool
|
2014-01-28 18:37:01 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2014-01-31 04:50:09 +00:00
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
2014-01-31 04:50:09 +00:00
|
|
|
|
{
|
2014-02-07 00:43:45 +00:00
|
|
|
|
if (!_config.Configuration.EnableFanArtUpdates)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 04:50:09 +00:00
|
|
|
|
var id = item.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(id))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
2014-02-06 04:39:16 +00:00
|
|
|
|
var xmlPath = GetFanartXmlPath(id);
|
2014-01-31 04:50:09 +00:00
|
|
|
|
|
|
|
|
|
var fileInfo = new FileInfo(xmlPath);
|
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
|
2014-01-31 04:50:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the movie data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="tmdbId">The TMDB id.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetMovieDataPath(IApplicationPaths appPaths, string tmdbId)
|
|
|
|
|
{
|
|
|
|
|
var dataPath = Path.Combine(GetMoviesDataPath(appPaths), tmdbId);
|
|
|
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the movie data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetMoviesDataPath(IApplicationPaths appPaths)
|
|
|
|
|
{
|
2014-03-01 04:13:44 +00:00
|
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "fanart-movies");
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
|
|
return dataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetFanartXmlPath(string tmdbId)
|
|
|
|
|
{
|
|
|
|
|
var movieDataPath = GetMovieDataPath(_config.ApplicationPaths, tmdbId);
|
|
|
|
|
return Path.Combine(movieDataPath, "fanart.xml");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads the movie XML.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="tmdbId">The TMDB id.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
internal async Task DownloadMovieXml(string tmdbId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var url = string.Format(FanArtBaseUrl, FanartArtistProvider.ApiKey, tmdbId);
|
|
|
|
|
|
|
|
|
|
var xmlPath = GetFanartXmlPath(tmdbId);
|
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(xmlPath));
|
|
|
|
|
|
|
|
|
|
using (var response = await _httpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
2014-02-25 15:40:16 +00:00
|
|
|
|
ResourcePool = FanartArtistProvider.Current.FanArtResourcePool,
|
2014-02-06 04:39:16 +00:00
|
|
|
|
CancellationToken = cancellationToken
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
using (var xmlFileStream = _fileSystem.GetFileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
|
|
|
|
{
|
|
|
|
|
await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly Task _cachedTask = Task.FromResult(true);
|
|
|
|
|
internal Task EnsureMovieXml(string tmdbId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var path = GetFanartXmlPath(tmdbId);
|
|
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(path);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
|
{
|
2014-02-19 18:50:37 +00:00
|
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
|
2014-02-06 04:39:16 +00:00
|
|
|
|
{
|
|
|
|
|
return _cachedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DownloadMovieXml(tmdbId, cancellationToken);
|
|
|
|
|
}
|
2013-11-01 01:48:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|