2014-10-25 18:32:58 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
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;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-10-25 18:32:58 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-11-01 20:14:21 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-10-23 04:26:01 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
using MediaBrowser.Providers.Music;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
using System;
|
2013-11-01 20:14:21 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
using System.Globalization;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
using System.IO;
|
2013-11-01 20:14:21 +00:00
|
|
|
|
using System.Linq;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.TV
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-03-16 22:08:23 +00:00
|
|
|
|
public class FanArtSeasonProvider : IRemoteImageProvider, IHasOrder, IHasChangeMonitor
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-10-23 04:26:01 +00:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2013-05-24 16:53:59 +00:00
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
|
public FanArtSeasonProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer json)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
_config = config;
|
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-10-23 04:26:01 +00:00
|
|
|
|
_json = json;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
public string Name
|
2013-06-25 01:22:21 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
get { return ProviderName; }
|
2013-06-25 01:22:21 +00:00
|
|
|
|
}
|
2014-02-03 05:35:43 +00:00
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
|
|
|
|
{
|
|
|
|
|
get { return "FanArt"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Supports(IHasImages item)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
|
|
|
|
return item is Season;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-10-16 14:22:13 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Backdrop,
|
2015-02-22 19:05:29 +00:00
|
|
|
|
ImageType.Thumb,
|
|
|
|
|
ImageType.Banner,
|
|
|
|
|
ImageType.Primary
|
2014-02-03 05:35:43 +00:00
|
|
|
|
};
|
2013-10-16 14:22:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
|
2014-02-03 05:35:43 +00:00
|
|
|
|
{
|
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
2013-05-24 16:51:42 +00:00
|
|
|
|
var season = (Season)item;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
var series = season.Series;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
if (series != null)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
var id = series.GetProviderId(MetadataProviders.Tvdb);
|
2013-05-24 16:51:42 +00:00
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(id) && season.IndexNumber.HasValue)
|
2013-06-20 14:48:49 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
{
|
2014-03-27 02:14:06 +00:00
|
|
|
|
int seasonNumber = AdjustForSeriesOffset(series, season.IndexNumber.Value);
|
2014-10-23 04:26:01 +00:00
|
|
|
|
AddImages(list, seasonNumber, 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
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie. Don't blow up
|
2014-02-03 05:35:43 +00:00
|
|
|
|
}
|
2013-06-20 14:48:49 +00:00
|
|
|
|
}
|
2013-05-24 16:51:42 +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);
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
private int AdjustForSeriesOffset(Series series, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
var offset = TvdbSeriesProvider.GetSeriesOffset(series.ProviderIds);
|
|
|
|
|
if (offset != null)
|
|
|
|
|
return (int)(seasonNumber + offset);
|
|
|
|
|
|
|
|
|
|
return seasonNumber;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, int seasonNumber, string path, CancellationToken cancellationToken)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
|
var root = _json.DeserializeFromFile<FanartSeriesProvider.RootObject>(path);
|
2013-05-24 16:51:42 +00:00
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
|
AddImages(list, root, seasonNumber, cancellationToken);
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 04:26:01 +00:00
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, FanartSeriesProvider.RootObject obj, int seasonNumber, CancellationToken cancellationToken)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2015-02-22 19:05:29 +00:00
|
|
|
|
PopulateImages(list, obj.seasonposter, ImageType.Primary, 1000, 1426, seasonNumber);
|
|
|
|
|
PopulateImages(list, obj.seasonbanner, ImageType.Primary, 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);
|
2013-11-30 06:08:14 +00:00
|
|
|
|
}
|
2013-11-01 20:14:21 +00:00
|
|
|
|
|
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)
|
2013-11-30 06:08:14 +00:00
|
|
|
|
{
|
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 =>
|
2013-11-30 06:08:14 +00:00
|
|
|
|
{
|
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
|
|
|
|
int imageSeasonNumber;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(url) &&
|
|
|
|
|
!string.IsNullOrEmpty(season) &&
|
|
|
|
|
int.TryParse(season, NumberStyles.Any, _usCulture, out imageSeasonNumber) &&
|
|
|
|
|
seasonNumber == imageSeasonNumber)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
|
var likesString = i.likes;
|
|
|
|
|
int likes;
|
|
|
|
|
|
|
|
|
|
var info = new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
RatingType = RatingType.Likes,
|
|
|
|
|
Type = type,
|
|
|
|
|
Width = width,
|
|
|
|
|
Height = height,
|
|
|
|
|
ProviderName = Name,
|
|
|
|
|
Url = url,
|
|
|
|
|
Language = i.lang
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
|
2013-11-30 06:08:14 +00:00
|
|
|
|
{
|
2014-10-23 04:26:01 +00:00
|
|
|
|
info.CommunityRating = likes;
|
2013-11-30 06:08:14 +00:00
|
|
|
|
}
|
2014-10-23 04:26:01 +00:00
|
|
|
|
|
|
|
|
|
return info;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
2014-10-23 04:26:01 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}).Where(i => i != null));
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
public int Order
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
get { return 1; }
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
2014-02-25 15:40:16 +00:00
|
|
|
|
ResourcePool = FanartArtistProvider.Current.FanArtResourcePool
|
2014-02-03 05:35:43 +00:00
|
|
|
|
});
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2015-01-18 05:45:10 +00:00
|
|
|
|
var options = FanartSeriesProvider.Current.GetFanartOptions();
|
|
|
|
|
if (!options.EnableAutomaticUpdates)
|
2014-02-07 00:43:45 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
var season = (Season)item;
|
|
|
|
|
var series = season.Series;
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
2013-05-24 16:51:42 +00:00
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
return false;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
2014-02-03 05:35:43 +00:00
|
|
|
|
|
|
|
|
|
var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(tvdbId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
2014-10-23 04:26:01 +00:00
|
|
|
|
var imagesFilePath = FanartSeriesProvider.Current.GetFanartJsonPath(tvdbId);
|
2014-02-03 05:35:43 +00:00
|
|
|
|
|
2015-10-04 03:38:46 +00:00
|
|
|
|
var fileInfo = _fileSystem.GetFileInfo(imagesFilePath);
|
2014-02-03 05:35:43 +00:00
|
|
|
|
|
2014-02-07 00:43:45 +00:00
|
|
|
|
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2013-05-24 16:51:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|