2014-01-28 18:37:01 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-01 19:54:25 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
|
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.TV
|
|
|
|
|
{
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public class ManualFanartSeasonImageProvider : IRemoteImageProvider
|
2013-11-01 19:54:25 +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;
|
2013-11-01 19:54:25 +00:00
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public ManualFanartSeasonImageProvider(IServerConfigurationManager config, IHttpClient httpClient)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
_httpClient = httpClient;
|
2013-11-01 19:54:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
|
|
|
|
{
|
|
|
|
|
get { return "FanArt"; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
return item is Season;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
|
|
|
|
{
|
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Backdrop,
|
|
|
|
|
ImageType.Thumb
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return images.Where(i => i.Type == imageType);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
var season = (Season)item;
|
|
|
|
|
var series = season.Series;
|
2013-11-01 19:54:25 +00:00
|
|
|
|
|
2013-11-01 20:14:21 +00:00
|
|
|
|
if (series != null)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
2013-11-01 20:14:21 +00:00
|
|
|
|
var id = series.GetProviderId(MetadataProviders.Tvdb);
|
2013-11-01 19:54:25 +00:00
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(id) && season.IndexNumber.HasValue)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
2013-11-01 20:14:21 +00:00
|
|
|
|
var xmlPath = FanArtTvProvider.Current.GetFanartXmlPath(id);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-12-21 18:37:34 +00:00
|
|
|
|
AddImages(list, season.IndexNumber.Value, xmlPath, cancellationToken);
|
2013-11-01 20:14:21 +00:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie. Don't blow up
|
|
|
|
|
}
|
2013-11-01 19:54:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 00:23:58 +00:00
|
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
2013-11-01 19:54:25 +00:00
|
|
|
|
|
|
|
|
|
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
// Sort first by width to prioritize HD versions
|
|
|
|
|
list = 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)
|
2013-11-04 21:50:37 +00:00
|
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return Task.FromResult<IEnumerable<RemoteImageInfo>>(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, int seasonNumber, 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 "series":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImages(list, subReader, seasonNumber, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AddImages(List<RemoteImageInfo> list, XmlReader reader, int seasonNumber, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "seasonthumbs":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Thumb, 500, 281, seasonNumber);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-01 20:14:21 +00:00
|
|
|
|
case "showbackgrounds":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Backdrop, 1920, 1080, seasonNumber);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-01 19:54:25 +00:00
|
|
|
|
default:
|
2013-11-01 20:34:26 +00:00
|
|
|
|
{
|
|
|
|
|
using (reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-01 19:54:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PopulateImageCategory(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken, ImageType type, int width, int height, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "seasonthumb":
|
2013-11-01 20:14:21 +00:00
|
|
|
|
case "showbackground":
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
var url = reader.GetAttribute("url");
|
|
|
|
|
var season = reader.GetAttribute("season");
|
|
|
|
|
|
2013-11-01 20:14:21 +00:00
|
|
|
|
int imageSeasonNumber;
|
|
|
|
|
|
2013-11-01 20:34:26 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(url) &&
|
|
|
|
|
!string.IsNullOrEmpty(season) &&
|
2013-11-01 20:14:21 +00:00
|
|
|
|
int.TryParse(season, NumberStyles.Any, _usCulture, out imageSeasonNumber) &&
|
|
|
|
|
seasonNumber == imageSeasonNumber)
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2013-11-01 20:14:21 +00:00
|
|
|
|
|
2013-11-01 19:54:25 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public int Order
|
2013-11-01 19:54:25 +00:00
|
|
|
|
{
|
2014-01-28 18:37:01 +00:00
|
|
|
|
get { return 1; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = FanartBaseProvider.FanArtResourcePool
|
|
|
|
|
});
|
2013-11-01 19:54:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|