2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2014-10-25 18:32:58 +00:00
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-01-31 04:50:09 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
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;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2014-10-25 18:32:58 +00:00
|
|
|
using MediaBrowser.Model.Net;
|
2013-11-01 01:48:14 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-10-23 04:26:01 +00:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-02-06 04:39:16 +00:00
|
|
|
using MediaBrowser.Providers.Music;
|
2015-01-18 05:45:10 +00:00
|
|
|
using MediaBrowser.Providers.TV;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
|
|
|
{
|
2016-05-25 00:42:12 +00:00
|
|
|
public class FanartMovieImageProvider : IRemoteImageProvider, 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;
|
2014-10-23 04:26:01 +00:00
|
|
|
private readonly IJsonSerializer _json;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
2016-04-21 05:13:33 +00:00
|
|
|
private const string FanArtBaseUrl = "https://webservice.fanart.tv/v3/movies/{1}?api_key={0}";
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
internal static FanartMovieImageProvider Current;
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
public FanartMovieImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer json)
|
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-10-23 04:26:01 +00:00
|
|
|
_json = json;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
Current = this;
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => ProviderName;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public static string ProviderName => "FanArt";
|
2013-11-01 01:48:14 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-01-30 21:23:54 +00:00
|
|
|
return item is Movie || item is BoxSet || item is MusicVideo;
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
2019-01-07 23:24:34 +00:00
|
|
|
ImageType.Primary,
|
2014-01-28 18:37:01 +00:00
|
|
|
ImageType.Thumb,
|
|
|
|
ImageType.Art,
|
|
|
|
ImageType.Logo,
|
|
|
|
ImageType.Disc,
|
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Backdrop
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var 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-10-25 18:32:58 +00:00
|
|
|
// Bad id entered
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await EnsureMovieJson(movieId, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (HttpException ex)
|
|
|
|
{
|
|
|
|
if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2014-01-30 21:23:54 +00:00
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
var path = GetFanartJsonPath(movieId);
|
2013-11-01 01:48:14 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
AddImages(list, path, cancellationToken);
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
{
|
|
|
|
// No biggie. Don't blow up
|
|
|
|
}
|
2016-10-27 18:30:20 +00:00
|
|
|
catch (IOException)
|
2014-12-23 03:58:14 +00:00
|
|
|
{
|
|
|
|
// No biggie. Don't blow up
|
|
|
|
}
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
private void AddImages(List<RemoteImageInfo> list, string path, CancellationToken cancellationToken)
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
var root = _json.DeserializeFromFile<RootObject>(path);
|
2013-11-01 01:48:14 +00:00
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
AddImages(list, root, cancellationToken);
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
private void AddImages(List<RemoteImageInfo> list, RootObject obj, CancellationToken cancellationToken)
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
PopulateImages(list, obj.hdmovieclearart, ImageType.Art, 1000, 562);
|
|
|
|
PopulateImages(list, obj.hdmovielogo, ImageType.Logo, 800, 310);
|
|
|
|
PopulateImages(list, obj.moviedisc, ImageType.Disc, 1000, 1000);
|
|
|
|
PopulateImages(list, obj.movieposter, ImageType.Primary, 1000, 1426);
|
|
|
|
PopulateImages(list, obj.movielogo, ImageType.Logo, 400, 155);
|
|
|
|
PopulateImages(list, obj.movieart, ImageType.Art, 500, 281);
|
|
|
|
PopulateImages(list, obj.moviethumb, ImageType.Thumb, 1000, 562);
|
|
|
|
PopulateImages(list, obj.moviebanner, ImageType.Banner, 1000, 185);
|
|
|
|
PopulateImages(list, obj.moviebackground, ImageType.Backdrop, 1920, 1080);
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
private void PopulateImages(List<RemoteImageInfo> list, List<Image> images, ImageType type, int width, int height)
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
if (images == null)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-11-01 01:48:14 +00:00
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
list.AddRange(images.Select(i =>
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
var url = i.url;
|
2013-11-01 01:48:14 +00:00
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
if (!string.IsNullOrEmpty(url))
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
var likesString = i.likes;
|
|
|
|
|
|
|
|
var info = new RemoteImageInfo
|
|
|
|
{
|
|
|
|
RatingType = RatingType.Likes,
|
|
|
|
Type = type,
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
ProviderName = Name,
|
2018-09-12 17:26:21 +00:00
|
|
|
Url = url,
|
2014-10-23 04:26:01 +00:00
|
|
|
Language = i.lang
|
|
|
|
};
|
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Integer, _usCulture, out var likes))
|
2013-11-01 01:48:14 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
info.CommunityRating = likes;
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
2014-10-23 04:26:01 +00:00
|
|
|
|
|
|
|
return info;
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
2014-10-23 04:26:01 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}).Where(i => i != null));
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 1;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 18:39:41 +00:00
|
|
|
Url = url
|
2014-01-28 18:37:01 +00:00
|
|
|
});
|
|
|
|
}
|
2014-01-31 04:50:09 +00:00
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the movie data path.
|
|
|
|
/// </summary>
|
2014-10-23 04:26:01 +00:00
|
|
|
/// <param name="appPaths">The application paths.</param>
|
|
|
|
/// <param name="id">The identifier.</param>
|
2014-02-06 04:39:16 +00:00
|
|
|
/// <returns>System.String.</returns>
|
2014-10-23 04:26:01 +00:00
|
|
|
internal static string GetMovieDataPath(IApplicationPaths appPaths, string id)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
var dataPath = Path.Combine(GetMoviesDataPath(appPaths), id);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
public string GetFanartJsonPath(string id)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
var movieDataPath = GetMovieDataPath(_config.ApplicationPaths, id);
|
|
|
|
return Path.Combine(movieDataPath, "fanart.json");
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2014-10-23 04:26:01 +00:00
|
|
|
/// Downloads the movie json.
|
2014-02-06 04:39:16 +00:00
|
|
|
/// </summary>
|
2014-10-23 04:26:01 +00:00
|
|
|
/// <param name="id">The identifier.</param>
|
2014-02-06 04:39:16 +00:00
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2014-10-23 04:26:01 +00:00
|
|
|
internal async Task DownloadMovieJson(string id, CancellationToken cancellationToken)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
var url = string.Format(FanArtBaseUrl, FanartArtistProvider.ApiKey, id);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2015-01-18 05:45:10 +00:00
|
|
|
var clientKey = FanartSeriesProvider.Current.GetFanartOptions().UserApiKey;
|
|
|
|
if (!string.IsNullOrWhiteSpace(clientKey))
|
2015-01-12 07:01:19 +00:00
|
|
|
{
|
2015-01-18 05:45:10 +00:00
|
|
|
url += "&client_key=" + clientKey;
|
2015-01-12 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
var path = GetFanartJsonPath(id);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2014-11-29 02:40:46 +00:00
|
|
|
try
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var httpResponse = await _httpClient.SendAsync(new HttpRequestOptions
|
2014-11-29 02:40:46 +00:00
|
|
|
{
|
|
|
|
Url = url,
|
2016-10-31 18:39:41 +00:00
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
BufferContent = true
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2017-10-20 16:16:56 +00:00
|
|
|
}, "GET").ConfigureAwait(false))
|
2014-11-29 02:40:46 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var response = httpResponse.Content)
|
2014-11-29 02:40:46 +00:00
|
|
|
{
|
2017-10-20 16:16:56 +00:00
|
|
|
using (var fileStream = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
|
|
|
|
{
|
|
|
|
await response.CopyToAsync(fileStream).ConfigureAwait(false);
|
|
|
|
}
|
2014-11-29 02:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (HttpException exception)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-11-29 02:40:46 +00:00
|
|
|
if (exception.StatusCode.HasValue && exception.StatusCode.Value == HttpStatusCode.NotFound)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-11-29 02:40:46 +00:00
|
|
|
// If the user has automatic updates enabled, save a dummy object to prevent repeated download attempts
|
|
|
|
_json.SerializeToFile(new RootObject(), path);
|
|
|
|
|
|
|
|
return;
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
2014-11-29 02:40:46 +00:00
|
|
|
|
|
|
|
throw;
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
internal Task EnsureMovieJson(string id, CancellationToken cancellationToken)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
var path = GetFanartJsonPath(id);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(path);
|
|
|
|
|
|
|
|
if (fileInfo.Exists)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
return Task.CompletedTask;
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
return DownloadMovieJson(id, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Image
|
|
|
|
{
|
|
|
|
public string id { get; set; }
|
|
|
|
public string url { get; set; }
|
|
|
|
public string lang { get; set; }
|
|
|
|
public string likes { get; set; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class RootObject
|
|
|
|
{
|
|
|
|
public string name { get; set; }
|
|
|
|
public string tmdb_id { get; set; }
|
|
|
|
public string imdb_id { get; set; }
|
|
|
|
public List<Image> hdmovielogo { get; set; }
|
|
|
|
public List<Image> moviedisc { get; set; }
|
|
|
|
public List<Image> movielogo { get; set; }
|
|
|
|
public List<Image> movieposter { get; set; }
|
|
|
|
public List<Image> hdmovieclearart { get; set; }
|
|
|
|
public List<Image> movieart { get; set; }
|
|
|
|
public List<Image> moviebackground { get; set; }
|
|
|
|
public List<Image> moviebanner { get; set; }
|
|
|
|
public List<Image> moviethumb { get; set; }
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
2013-11-01 01:48:14 +00:00
|
|
|
}
|
|
|
|
}
|