jellyfin/MediaBrowser.Providers/Movies/FanartMovieImageProvider.cs

334 lines
12 KiB
C#
Raw Normal View History

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