jellyfin-server/MediaBrowser.Providers/TV/TheTVDB/TvdbSeriesImageProvider.cs

356 lines
12 KiB
C#
Raw Normal View History

2016-03-27 21:11:27 +00:00
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;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2016-10-27 07:58:33 +00:00
using MediaBrowser.Model.Xml;
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.TV
{
2016-05-25 00:42:12 +00:00
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
{
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;
2016-10-27 07:58:33 +00:00
private readonly IXmlReaderSettingsFactory _xmlReaderSettingsFactory;
2016-10-27 07:58:33 +00:00
public TvdbSeriesImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
{
2014-02-03 20:51:28 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
2016-10-27 07:58:33 +00:00
_xmlReaderSettingsFactory = xmlReaderSettingsFactory;
}
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-13 05:11:54 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
{
2015-10-21 05:09:50 +00:00
if (TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
{
2014-02-03 20:51:28 +00:00
var language = item.GetPreferredMetadataLanguage();
2017-03-12 19:27:45 +00:00
var seriesDataPath = await TvdbSeriesProvider.Current.EnsureSeriesInfo(item.ProviderIds, item.Name, item.ProductionYear, language, cancellationToken).ConfigureAwait(false);
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
}
2016-10-27 07:58:33 +00:00
catch (IOException)
2014-12-23 03:58:14 +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)
{
2016-10-27 07:58:33 +00:00
var settings = _xmlReaderSettingsFactory.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
2014-02-03 20:51:28 +00:00
var list = new List<RemoteImageInfo>();
2016-10-27 07:58:33 +00:00
using (var fileStream = _fileSystem.GetFileStream(xmlPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
2014-02-03 20:51:28 +00:00
{
2016-10-27 07:58:33 +00:00
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
2014-02-03 20:51:28 +00:00
{
2016-10-27 07:58:33 +00:00
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
2014-02-03 20:51:28 +00:00
{
2016-10-27 07:58:33 +00:00
reader.MoveToContent();
2016-12-02 20:10:35 +00:00
reader.Read();
2016-10-27 07:58:33 +00:00
// Loop through each element
2016-12-03 21:46:06 +00:00
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
2014-02-03 20:51:28 +00:00
{
2016-10-27 07:58:33 +00:00
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
2014-02-03 20:51:28 +00:00
{
2016-10-27 07:58:33 +00:00
switch (reader.Name)
{
case "Banner":
2014-02-03 20:51:28 +00:00
{
2016-12-03 23:57:34 +00:00
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
2016-10-27 07:58:33 +00:00
using (var subtree = reader.ReadSubtree())
{
AddImage(subtree, list);
}
break;
2014-02-03 20:51:28 +00:00
}
2016-10-27 07:58:33 +00:00
default:
reader.Skip();
2014-02-03 20:51:28 +00:00
break;
2016-10-27 07:58:33 +00:00
}
2014-02-03 20:51:28 +00:00
}
2016-12-02 20:10:35 +00:00
else
{
reader.Read();
}
2014-02-03 20:51:28 +00:00
}
}
}
}
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;
2016-12-02 20:10:35 +00:00
reader.MoveToContent();
reader.Read();
// Loop through each element
2016-12-03 21:46:06 +00:00
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
2014-02-03 20:51:28 +00:00
{
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
}
2016-12-02 20:10:35 +00:00
else
{
reader.Read();
}
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
});
}
2013-09-15 17:34:48 +00:00
}
}