jellyfin/MediaBrowser.Providers/TV/FanArtSeasonProvider.cs

304 lines
11 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
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.Providers;
2014-02-03 05:35:43 +00:00
using MediaBrowser.Providers.Music;
using System;
using System.Collections.Generic;
2014-02-03 05:35:43 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
2014-02-03 05:35:43 +00:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2014-02-03 05:35:43 +00:00
using System.Xml;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.TV
{
2014-02-03 20:51:28 +00:00
public class FanartSeasonProvider : IRemoteImageProvider, IHasOrder, IHasChangeMonitor
{
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-02-03 20:51:28 +00:00
public FanartSeasonProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
{
2014-02-03 05:35:43 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
}
2014-02-03 05:35:43 +00:00
public string Name
{
2014-02-03 05:35:43 +00:00
get { return ProviderName; }
}
2014-02-03 05:35:43 +00:00
public static string ProviderName
{
get { return "FanArt"; }
}
public bool Supports(IHasImages item)
{
return item is Season;
}
2014-02-03 05:35:43 +00:00
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
2014-02-03 05:35:43 +00:00
return new List<ImageType>
{
ImageType.Backdrop,
ImageType.Thumb
};
}
2014-02-03 05:35:43 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
{
2014-02-03 05:35:43 +00:00
var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false);
return images.Where(i => i.Type == imageType);
}
public async Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
{
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-02-03 20:51:28 +00:00
await FanartSeriesProvider.Current.EnsureSeriesXml(id, cancellationToken).ConfigureAwait(false);
2014-02-03 05:35:43 +00:00
2014-02-03 20:51:28 +00:00
var xmlPath = FanartSeriesProvider.Current.GetFanartXmlPath(id);
2014-02-03 05:35:43 +00:00
try
{
AddImages(list, season.IndexNumber.Value, xmlPath, cancellationToken);
}
catch (FileNotFoundException)
{
// No biggie. Don't blow up
}
}
}
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-02-03 05:35:43 +00:00
private void AddImages(List<RemoteImageInfo> list, int seasonNumber, string xmlPath, CancellationToken cancellationToken)
{
2014-02-03 05:35:43 +00:00
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();
2014-02-03 05:35:43 +00:00
// Loop through each element
while (reader.Read())
{
cancellationToken.ThrowIfCancellationRequested();
2014-02-03 05:35:43 +00:00
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "series":
{
using (var subReader = reader.ReadSubtree())
{
AddImages(list, subReader, seasonNumber, cancellationToken);
}
break;
}
2014-02-03 05:35:43 +00:00
default:
reader.Skip();
break;
}
}
}
}
}
}
2014-02-03 05:35:43 +00:00
private void AddImages(List<RemoteImageInfo> list, XmlReader reader, int seasonNumber, CancellationToken cancellationToken)
{
2014-02-03 05:35:43 +00:00
reader.MoveToContent();
2014-02-02 13:36:31 +00:00
2014-02-03 05:35:43 +00:00
while (reader.Read())
{
2014-02-03 05:35:43 +00:00
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;
}
case "showbackgrounds":
{
using (var subReader = reader.ReadSubtree())
{
PopulateImageCategory(list, subReader, cancellationToken, ImageType.Backdrop, 1920, 1080, seasonNumber);
}
break;
}
default:
{
using (reader.ReadSubtree())
{
}
break;
}
}
}
}
}
2014-02-03 05:35:43 +00:00
private void PopulateImageCategory(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken, ImageType type, int width, int height, int seasonNumber)
{
2014-02-03 05:35:43 +00:00
reader.MoveToContent();
while (reader.Read())
{
2014-02-03 05:35:43 +00:00
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
2014-02-03 05:35:43 +00:00
switch (reader.Name)
{
2014-02-03 05:35:43 +00:00
case "seasonthumb":
case "showbackground":
{
var url = reader.GetAttribute("url");
var season = reader.GetAttribute("season");
int imageSeasonNumber;
if (!string.IsNullOrEmpty(url) &&
!string.IsNullOrEmpty(season) &&
int.TryParse(season, NumberStyles.Any, _usCulture, out imageSeasonNumber) &&
seasonNumber == imageSeasonNumber)
{
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);
}
break;
}
default:
reader.Skip();
break;
}
}
}
}
2014-02-03 05:35:43 +00:00
public int Order
{
2014-02-03 05:35:43 +00:00
get { return 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,
Url = url,
ResourcePool = FanartArtistProvider.FanArtResourcePool
});
}
2014-02-03 05:35:43 +00:00
public bool HasChanged(IHasMetadata item, DateTime date)
{
2014-02-03 05:35:43 +00:00
var season = (Season)item;
var series = season.Series;
if (series == null)
{
2014-02-03 05:35:43 +00:00
return false;
}
2014-02-03 05:35:43 +00:00
var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
if (!String.IsNullOrEmpty(tvdbId))
{
// Process images
2014-02-03 20:51:28 +00:00
var imagesXmlPath = FanartSeriesProvider.Current.GetFanartXmlPath(tvdbId);
2014-02-03 05:35:43 +00:00
var fileInfo = new FileInfo(imagesXmlPath);
return fileInfo.Exists && _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
}
return false;
}
}
}