jellyfin/MediaBrowser.Providers/TV/TvdbSeriesImageProvider.cs

358 lines
12 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
2013-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
2014-02-03 20:51:28 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using System;
2013-09-15 17:34:48 +00:00
using System.Collections.Generic;
2014-02-03 20:51:28 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
2014-02-03 20:51:28 +00:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2014-02-03 20:51:28 +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 TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder, IHasChangeMonitor
{
2014-02-03 20:51:28 +00:00
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IFileSystem _fileSystem;
2014-02-03 20:51:28 +00:00
public TvdbSeriesImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
{
2014-02-03 20:51:28 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
}
2014-02-03 20:51:28 +00:00
public string Name
{
2014-02-03 20:51:28 +00:00
get { return ProviderName; }
}
2014-02-03 20:51:28 +00:00
public static string ProviderName
{
2014-02-03 20:51:28 +00:00
get { return "TheTVDB"; }
}
2014-02-03 20:51:28 +00:00
public bool Supports(IHasImages item)
{
2014-02-03 20:51:28 +00:00
return item is Series;
}
2014-02-03 20:51:28 +00:00
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
2014-02-03 20:51:28 +00:00
return new List<ImageType>
{
2014-02-03 20:51:28 +00:00
ImageType.Primary,
ImageType.Banner,
ImageType.Backdrop
};
}
2013-09-15 17:34:48 +00:00
2014-02-03 20:51:28 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
{
2014-02-03 20:51:28 +00:00
var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false);
2014-02-03 20:51:28 +00:00
return images.Where(i => i.Type == imageType);
}
2014-02-03 20:51:28 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
{
2014-02-03 20:51:28 +00:00
var series = (Series)item;
var seriesId = series.GetProviderId(MetadataProviders.Tvdb);
if (!string.IsNullOrEmpty(seriesId))
{
2014-02-03 20:51:28 +00:00
var language = item.GetPreferredMetadataLanguage();
await TvdbSeriesProvider.Current.EnsureSeriesInfo(seriesId, language, cancellationToken).ConfigureAwait(false);
// Process images
2014-02-03 20:51:28 +00:00
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesId);
2014-02-03 20:51:28 +00:00
var path = Path.Combine(seriesDataPath, "banners.xml");
2014-02-03 20:51:28 +00:00
try
{
return GetImages(path, language, cancellationToken);
}
catch (FileNotFoundException)
{
2014-02-03 20:51:28 +00:00
// No tvdb data yet. Don't blow up
}
}
2013-09-15 17:34:48 +00:00
2014-02-03 20:51:28 +00:00
return new RemoteImageInfo[] { };
}
2014-02-03 20:51:28 +00:00
private IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, CancellationToken cancellationToken)
{
2014-02-03 20:51:28 +00:00
var settings = new XmlReaderSettings
{
2014-02-03 20:51:28 +00:00
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true,
ValidationType = ValidationType.None
};
2014-02-03 20:51:28 +00:00
var list = new List<RemoteImageInfo>();
2014-02-03 20:51:28 +00:00
using (var streamReader = new StreamReader(xmlPath, Encoding.UTF8))
{
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
reader.MoveToContent();
2014-02-03 20:51:28 +00:00
// Loop through each element
while (reader.Read())
{
cancellationToken.ThrowIfCancellationRequested();
2014-02-03 20:51:28 +00:00
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Banner":
{
using (var subtree = reader.ReadSubtree())
{
AddImage(subtree, list);
}
break;
}
default:
reader.Skip();
break;
}
}
}
}
}
2014-02-03 20:51:28 +00:00
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
2014-02-03 20:51:28 +00:00
return list.OrderByDescending(i =>
{
2014-02-03 20:51:28 +00:00
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
{
2014-02-03 20:51:28 +00:00
return 3;
}
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
2014-02-03 20:51:28 +00:00
return 2;
}
}
2014-02-03 20:51:28 +00:00
if (string.IsNullOrEmpty(i.Language))
{
2014-02-03 20:51:28 +00:00
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0)
.ToList();
}
private void AddImage(XmlReader reader, List<RemoteImageInfo> images)
{
reader.MoveToContent();
2014-02-03 20:51:28 +00:00
string bannerType = null;
string url = null;
int? bannerSeason = null;
int? width = null;
int? height = null;
string language = null;
double? rating = null;
int? voteCount = null;
string thumbnailUrl = null;
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
2014-02-03 20:51:28 +00:00
case "Rating":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
double rval;
if (double.TryParse(val, NumberStyles.Any, _usCulture, out rval))
{
rating = rval;
}
break;
}
case "RatingCount":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
int rval;
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
{
voteCount = rval;
}
break;
}
case "Language":
{
language = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "ThumbnailPath":
{
thumbnailUrl = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerType":
{
bannerType = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerPath":
{
url = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "BannerType2":
{
var bannerType2 = reader.ReadElementContentAsString() ?? string.Empty;
// Sometimes the resolution is stuffed in here
var resolutionParts = bannerType2.Split('x');
if (resolutionParts.Length == 2)
{
int rval;
if (int.TryParse(resolutionParts[0], NumberStyles.Integer, _usCulture, out rval))
{
width = rval;
}
if (int.TryParse(resolutionParts[1], NumberStyles.Integer, _usCulture, out rval))
{
height = rval;
}
}
break;
}
case "Season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
bannerSeason = int.Parse(val);
}
break;
}
default:
reader.Skip();
break;
}
2013-09-15 17:34:48 +00:00
}
}
2014-02-03 20:51:28 +00:00
if (!string.IsNullOrEmpty(url) && !bannerSeason.HasValue)
2013-09-15 17:34:48 +00:00
{
2014-02-03 20:51:28 +00:00
var imageInfo = new RemoteImageInfo
2013-09-15 17:34:48 +00:00
{
2014-02-03 20:51:28 +00:00
RatingType = RatingType.Score,
CommunityRating = rating,
VoteCount = voteCount,
Url = TVUtils.BannerUrl + url,
ProviderName = Name,
Language = language,
Width = width,
Height = height
};
2014-02-03 20:51:28 +00:00
if (!string.IsNullOrEmpty(thumbnailUrl))
{
imageInfo.ThumbnailUrl = TVUtils.BannerUrl + thumbnailUrl;
}
2013-09-15 17:34:48 +00:00
2014-02-03 20:51:28 +00:00
if (string.Equals(bannerType, "poster", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Primary;
images.Add(imageInfo);
}
else if (string.Equals(bannerType, "series", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Banner;
images.Add(imageInfo);
}
else if (string.Equals(bannerType, "fanart", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Backdrop;
images.Add(imageInfo);
}
}
2014-02-03 20:51:28 +00:00
}
public int Order
{
get { return 0; }
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = url,
ResourcePool = TvdbSeriesProvider.Current.TvDbResourcePool
});
}
public bool HasChanged(IHasMetadata item, DateTime date)
{
var tvdbId = item.GetProviderId(MetadataProviders.Tvdb);
if (!String.IsNullOrEmpty(tvdbId))
{
// Process images
var imagesXmlPath = Path.Combine(TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, tvdbId), "banners.xml");
var fileInfo = new FileInfo(imagesXmlPath);
return fileInfo.Exists && _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
}
return false;
2013-09-15 17:34:48 +00:00
}
}
}