jellyfin-server/MediaBrowser.Providers/TV/FanArt/FanArtSeasonProvider.cs

206 lines
7.1 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-02-03 05:35:43 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
2013-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
2014-02-03 05:35:43 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.IO;
2014-10-25 18:32:58 +00:00
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Providers;
2014-10-23 04:26:01 +00:00
using MediaBrowser.Model.Serialization;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.TV
{
2016-05-25 00:42:12 +00:00
public class FanArtSeasonProvider : IRemoteImageProvider, IHasOrder
{
2014-02-03 05:35:43 +00:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
2014-10-23 04:26:01 +00:00
private readonly IJsonSerializer _json;
2014-10-23 04:26:01 +00:00
public FanArtSeasonProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer json)
{
2014-02-03 05:35:43 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
2014-10-23 04:26:01 +00:00
_json = json;
}
public string Name => ProviderName;
2014-02-03 05:35:43 +00:00
public static string ProviderName => "FanArt";
2014-02-03 05:35:43 +00:00
2018-09-12 17:26:21 +00:00
public bool Supports(BaseItem item)
{
return item is Season;
}
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
2014-02-03 05:35:43 +00:00
return new List<ImageType>
{
2019-01-07 23:27:46 +00:00
ImageType.Backdrop,
ImageType.Thumb,
ImageType.Banner,
ImageType.Primary
2014-02-03 05:35:43 +00:00
};
}
2018-09-12 17:26:21 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-02-03 05:35:43 +00:00
{
var list = new List<RemoteImageInfo>();
var season = (Season)item;
2014-02-03 05:35:43 +00:00
var series = season.Series;
2014-02-03 05:35:43 +00:00
if (series != null)
{
2014-02-03 05:35:43 +00:00
var id = series.GetProviderId(MetadataProviders.Tvdb);
2014-02-03 05:35:43 +00:00
if (!string.IsNullOrEmpty(id) && season.IndexNumber.HasValue)
{
2014-10-25 18:32:58 +00:00
// Bad id entered
try
{
await FanartSeriesProvider.Current.EnsureSeriesJson(id, cancellationToken).ConfigureAwait(false);
}
catch (HttpException ex)
{
if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound)
{
throw;
}
}
2014-02-03 05:35:43 +00:00
2014-10-23 04:26:01 +00:00
var path = FanartSeriesProvider.Current.GetFanartJsonPath(id);
2014-02-03 05:35:43 +00:00
try
{
AddImages(list, season.IndexNumber.Value, path, cancellationToken);
2014-02-03 05:35:43 +00:00
}
catch (FileNotFoundException)
{
// No biggie. Don't blow up
2014-12-23 03:58:14 +00:00
}
2016-10-27 19:03:23 +00:00
catch (IOException)
2014-12-23 03:58:14 +00:00
{
// No biggie. Don't blow up
2014-02-03 05:35:43 +00:00
}
}
}
2014-02-03 05:35:43 +00:00
var language = item.GetPreferredMetadataLanguage();
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
// Sort first by width to prioritize HD versions
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;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
2014-10-23 04:26:01 +00:00
private void AddImages(List<RemoteImageInfo> list, int seasonNumber, string path, CancellationToken cancellationToken)
{
2014-10-23 04:26:01 +00:00
var root = _json.DeserializeFromFile<FanartSeriesProvider.RootObject>(path);
2014-10-23 04:26:01 +00:00
AddImages(list, root, seasonNumber, cancellationToken);
}
2014-10-23 04:26:01 +00:00
private void AddImages(List<RemoteImageInfo> list, FanartSeriesProvider.RootObject obj, int seasonNumber, CancellationToken cancellationToken)
{
PopulateImages(list, obj.seasonposter, ImageType.Primary, 1000, 1426, seasonNumber);
2015-11-26 02:58:41 +00:00
PopulateImages(list, obj.seasonbanner, ImageType.Banner, 1000, 185, seasonNumber);
2014-10-23 04:26:01 +00:00
PopulateImages(list, obj.seasonthumb, ImageType.Thumb, 500, 281, seasonNumber);
PopulateImages(list, obj.showbackground, ImageType.Backdrop, 1920, 1080, seasonNumber);
}
2014-10-23 04:26:01 +00:00
private void PopulateImages(List<RemoteImageInfo> list,
List<FanartSeriesProvider.Image> images,
ImageType type,
int width,
int height,
int seasonNumber)
{
2014-10-23 04:26:01 +00:00
if (images == null)
{
return;
}
2014-02-03 05:35:43 +00:00
2014-10-23 04:26:01 +00:00
list.AddRange(images.Select(i =>
{
2014-10-23 04:26:01 +00:00
var url = i.url;
var season = i.season;
2014-02-03 05:35:43 +00:00
2014-10-23 04:26:01 +00:00
if (!string.IsNullOrEmpty(url) &&
!string.IsNullOrEmpty(season) &&
int.TryParse(season, NumberStyles.Integer, _usCulture, out var imageSeasonNumber) &&
2014-10-23 04:26:01 +00:00
seasonNumber == imageSeasonNumber)
{
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.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase),
2014-10-23 04:26:01 +00:00
Language = i.lang
};
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Integer, _usCulture, out var likes))
{
2014-10-23 04:26:01 +00:00
info.CommunityRating = likes;
}
2014-10-23 04:26:01 +00:00
return info;
}
2014-10-23 04:26:01 +00:00
return null;
}).Where(i => i != null));
}
public int Order => 1;
2014-02-03 05:35:43 +00:00
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
2014-02-03 05:35:43 +00:00
return _httpClient.GetResponse(new HttpRequestOptions
{
2014-02-03 05:35:43 +00:00
CancellationToken = cancellationToken,
2016-10-31 18:39:41 +00:00
Url = url
2014-02-03 05:35:43 +00:00
});
}
}
}