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

359 lines
13 KiB
C#
Raw Normal View History

2016-03-27 14:11:27 -07: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 09:47:28 -07:00
using MediaBrowser.Controller.Providers;
2014-02-03 13:51:28 -07:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using System;
2013-09-15 10:34:48 -07:00
using System.Collections.Generic;
2014-02-03 13:51:28 -07:00
using System.Globalization;
using System.IO;
using System.Linq;
2014-02-03 13:51:28 -07:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
2014-02-03 13:51:28 -07:00
using System.Xml;
2017-05-25 23:48:54 -07:00
2016-10-25 12:02:04 -07:00
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2016-10-27 00:58:33 -07:00
using MediaBrowser.Model.Xml;
2013-06-09 09:47:28 -07:00
namespace MediaBrowser.Providers.TV
{
2016-05-24 17:42:12 -07:00
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
{
2014-02-03 13:51:28 -07:00
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IFileSystem _fileSystem;
2016-10-27 00:58:33 -07:00
private readonly IXmlReaderSettingsFactory _xmlReaderSettingsFactory;
2016-10-27 00:58:33 -07:00
public TvdbSeriesImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
{
2014-02-03 13:51:28 -07:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
2016-10-27 00:58:33 -07:00
_xmlReaderSettingsFactory = xmlReaderSettingsFactory;
}
2014-02-03 13:51:28 -07:00
public string Name
{
2014-02-03 13:51:28 -07:00
get { return ProviderName; }
}
2014-02-03 13:51:28 -07:00
public static string ProviderName
{
2014-02-03 13:51:28 -07:00
get { return "TheTVDB"; }
}
2018-09-12 10:26:21 -07:00
public bool Supports(BaseItem item)
{
2014-02-03 13:51:28 -07:00
return item is Series;
}
2018-09-12 10:26:21 -07:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
2014-02-03 13:51:28 -07:00
return new List<ImageType>
{
2014-02-03 13:51:28 -07:00
ImageType.Primary,
ImageType.Banner,
ImageType.Backdrop
};
}
2013-09-15 10:34:48 -07:00
2018-09-12 10:26:21 -07:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
2015-10-20 22:09:50 -07:00
if (TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
{
2014-02-03 13:51:28 -07:00
var language = item.GetPreferredMetadataLanguage();
2017-03-12 12:27:45 -07:00
var seriesDataPath = await TvdbSeriesProvider.Current.EnsureSeriesInfo(item.ProviderIds, item.Name, item.ProductionYear, language, cancellationToken).ConfigureAwait(false);
2018-09-12 10:26:21 -07:00
if (string.IsNullOrEmpty(seriesDataPath))
{
return new RemoteImageInfo[] { };
}
2014-02-03 13:51:28 -07:00
var path = Path.Combine(seriesDataPath, "banners.xml");
2014-02-03 13:51:28 -07:00
try
{
return GetImages(path, language, cancellationToken);
}
catch (FileNotFoundException)
{
2014-02-03 13:51:28 -07:00
// No tvdb data yet. Don't blow up
}
2016-10-27 00:58:33 -07:00
catch (IOException)
2014-12-22 20:58:14 -07:00
{
// No tvdb data yet. Don't blow up
}
}
2013-09-15 10:34:48 -07:00
2014-02-03 13:51:28 -07:00
return new RemoteImageInfo[] { };
}
2014-02-03 13:51:28 -07:00
private IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, CancellationToken cancellationToken)
{
2016-10-27 00:58:33 -07:00
var settings = _xmlReaderSettingsFactory.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
2014-02-03 13:51:28 -07:00
var list = new List<RemoteImageInfo>();
2016-10-27 00:58:33 -07:00
using (var fileStream = _fileSystem.GetFileStream(xmlPath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
2014-02-03 13:51:28 -07:00
{
2016-10-27 00:58:33 -07:00
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
2014-02-03 13:51:28 -07:00
{
2016-10-27 00:58:33 -07:00
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
2014-02-03 13:51:28 -07:00
{
2016-10-27 00:58:33 -07:00
reader.MoveToContent();
2016-12-02 13:10:35 -07:00
reader.Read();
2016-10-27 00:58:33 -07:00
// Loop through each element
2016-12-03 14:46:06 -07:00
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
2014-02-03 13:51:28 -07:00
{
2016-10-27 00:58:33 -07:00
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
2014-02-03 13:51:28 -07:00
{
2016-10-27 00:58:33 -07:00
switch (reader.Name)
{
case "Banner":
2014-02-03 13:51:28 -07:00
{
2016-12-03 16:57:34 -07:00
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
2016-10-27 00:58:33 -07:00
using (var subtree = reader.ReadSubtree())
{
AddImage(subtree, list);
}
break;
2014-02-03 13:51:28 -07:00
}
2016-10-27 00:58:33 -07:00
default:
reader.Skip();
2014-02-03 13:51:28 -07:00
break;
2016-10-27 00:58:33 -07:00
}
2014-02-03 13:51:28 -07:00
}
2016-12-02 13:10:35 -07:00
else
{
reader.Read();
}
2014-02-03 13:51:28 -07:00
}
}
}
}
2014-02-03 13:51:28 -07:00
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
2014-02-03 13:51:28 -07:00
return list.OrderByDescending(i =>
{
2014-02-03 13:51:28 -07:00
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
{
2014-02-03 13:51:28 -07:00
return 3;
}
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
2014-02-03 13:51:28 -07:00
return 2;
}
}
2014-02-03 13:51:28 -07:00
if (string.IsNullOrEmpty(i.Language))
{
2014-02-03 13:51:28 -07:00
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
2014-02-03 13:51:28 -07:00
}
private void AddImage(XmlReader reader, List<RemoteImageInfo> images)
{
reader.MoveToContent();
2014-02-03 13:51:28 -07: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 13:10:35 -07:00
reader.MoveToContent();
reader.Read();
// Loop through each element
2016-12-03 14:46:06 -07:00
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
2014-02-03 13:51:28 -07:00
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
2014-02-03 13:51:28 -07: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 10:34:48 -07:00
}
2016-12-02 13:10:35 -07:00
else
{
reader.Read();
}
2013-09-15 10:34:48 -07:00
}
2014-02-03 13:51:28 -07:00
if (!string.IsNullOrEmpty(url) && !bannerSeason.HasValue)
2013-09-15 10:34:48 -07:00
{
2014-02-03 13:51:28 -07:00
var imageInfo = new RemoteImageInfo
2013-09-15 10:34:48 -07:00
{
2014-02-03 13:51:28 -07:00
RatingType = RatingType.Score,
CommunityRating = rating,
VoteCount = voteCount,
Url = TVUtils.BannerUrl + url,
ProviderName = Name,
Language = language,
Width = width,
Height = height
};
2014-02-03 13:51:28 -07:00
if (!string.IsNullOrEmpty(thumbnailUrl))
{
imageInfo.ThumbnailUrl = TVUtils.BannerUrl + thumbnailUrl;
}
2013-09-15 10:34:48 -07:00
2014-02-03 13:51:28 -07: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 13:51:28 -07:00
}
public int Order
{
get { return 0; }
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
2017-04-29 19:37:51 -07:00
Url = url
2014-02-03 13:51:28 -07:00
});
}
2013-09-15 10:34:48 -07:00
}
}