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

348 lines
13 KiB
C#
Raw Normal View History

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;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Providers;
2016-10-27 07:58:33 +00:00
using MediaBrowser.Model.Xml;
namespace MediaBrowser.Providers.TV.TheTVDB
{
2016-05-25 00:42:12 +00:00
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
{
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
2014-02-03 05:35:43 +00:00
private readonly IFileSystem _fileSystem;
2016-10-27 07:58:33 +00:00
private readonly IXmlReaderSettingsFactory _xmlSettings;
2016-10-27 07:58:33 +00:00
public TvdbSeasonImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlSettings)
{
_config = config;
_httpClient = httpClient;
2014-02-03 05:35:43 +00:00
_fileSystem = fileSystem;
2016-10-27 07:58:33 +00:00
_xmlSettings = xmlSettings;
}
public string Name => ProviderName;
public static string ProviderName => "TheTVDB";
2018-09-12 17:26:21 +00:00
public bool Supports(BaseItem item)
{
return item is Season;
}
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType>
{
2019-01-07 23:27:46 +00:00
ImageType.Primary,
ImageType.Banner,
ImageType.Backdrop
};
}
2018-09-12 17:26:21 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var season = (Season)item;
2014-02-03 05:35:43 +00:00
var series = season.Series;
2015-10-21 05:09:50 +00:00
if (series != null && season.IndexNumber.HasValue && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
2015-09-19 21:25:19 +00:00
{
2015-10-21 05:09:50 +00:00
var seriesProviderIds = series.ProviderIds;
var seasonNumber = season.IndexNumber.Value;
2017-03-12 19:27:45 +00:00
var seriesDataPath = await TvdbSeriesProvider.Current.EnsureSeriesInfo(seriesProviderIds, series.Name, series.ProductionYear, series.GetPreferredMetadataLanguage(), cancellationToken).ConfigureAwait(false);
2018-09-12 17:26:21 +00:00
if (!string.IsNullOrEmpty(seriesDataPath))
2014-12-23 03:58:14 +00:00
{
2016-03-26 05:44:01 +00:00
var path = Path.Combine(seriesDataPath, "banners.xml");
try
{
2016-10-27 07:58:33 +00:00
return GetImages(path, item.GetPreferredMetadataLanguage(), seasonNumber, _xmlSettings, _fileSystem, cancellationToken);
2016-03-26 05:44:01 +00:00
}
catch (FileNotFoundException)
{
// No tvdb data yet. Don't blow up
}
2016-10-27 07:58:33 +00:00
catch (IOException)
2016-03-26 05:44:01 +00:00
{
// No tvdb data yet. Don't blow up
}
2014-12-23 03:58:14 +00:00
}
}
2014-02-03 05:35:43 +00:00
return new RemoteImageInfo[] { };
}
2016-10-27 07:58:33 +00:00
internal static IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, int seasonNumber, IXmlReaderSettingsFactory xmlReaderSettingsFactory, IFileSystem fileSystem, CancellationToken cancellationToken)
{
2016-10-27 07:58:33 +00:00
var settings = xmlReaderSettingsFactory.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
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))
{
2016-10-27 07:58:33 +00:00
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
2016-10-27 07:58:33 +00:00
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, settings))
{
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)
{
2016-10-27 07:58:33 +00:00
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
2016-10-27 07:58:33 +00:00
switch (reader.Name)
{
case "Banner":
{
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, seasonNumber);
}
break;
}
2016-10-27 07:58:33 +00:00
default:
reader.Skip();
break;
2016-10-27 07:58:33 +00:00
}
}
2016-12-02 20:10:35 +00:00
else
{
reader.Read();
}
}
}
}
}
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
return list.OrderByDescending(i =>
{
if (string.Equals(preferredLanguage, 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);
}
private static void AddImage(XmlReader reader, List<RemoteImageInfo> images, int seasonNumber)
{
reader.MoveToContent();
string bannerType = null;
string bannerType2 = 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)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Rating":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
if (double.TryParse(val, NumberStyles.Any, UsCulture, out var rval))
{
rating = rval;
}
break;
}
case "RatingCount":
{
var val = reader.ReadElementContentAsString() ?? string.Empty;
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out var 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 "BannerType2":
{
bannerType2 = reader.ReadElementContentAsString() ?? string.Empty;
// Sometimes the resolution is stuffed in here
var resolutionParts = bannerType2.Split('x');
if (resolutionParts.Length == 2)
{
if (int.TryParse(resolutionParts[0], NumberStyles.Integer, UsCulture, out var rval))
{
width = rval;
}
if (int.TryParse(resolutionParts[1], NumberStyles.Integer, UsCulture, out rval))
{
height = rval;
}
}
break;
}
case "BannerPath":
{
url = reader.ReadElementContentAsString() ?? string.Empty;
break;
}
case "Season":
{
var val = reader.ReadElementContentAsString();
if (!string.IsNullOrWhiteSpace(val))
{
bannerSeason = int.Parse(val);
}
break;
}
default:
reader.Skip();
break;
}
}
2016-12-02 20:10:35 +00:00
else
{
reader.Read();
}
}
if (!string.IsNullOrEmpty(url) && bannerSeason.HasValue && bannerSeason.Value == seasonNumber)
{
var imageInfo = new RemoteImageInfo
{
RatingType = RatingType.Score,
CommunityRating = rating,
VoteCount = voteCount,
Url = TVUtils.BannerUrl + url,
ProviderName = ProviderName,
Language = language,
Width = width,
Height = height
};
if (!string.IsNullOrEmpty(thumbnailUrl))
{
imageInfo.ThumbnailUrl = TVUtils.BannerUrl + thumbnailUrl;
}
if (string.Equals(bannerType, "season", StringComparison.OrdinalIgnoreCase))
{
if (string.Equals(bannerType2, "season", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Primary;
images.Add(imageInfo);
}
else if (string.Equals(bannerType2, "seasonwide", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Banner;
images.Add(imageInfo);
}
}
else if (string.Equals(bannerType, "fanart", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Backdrop;
images.Add(imageInfo);
}
}
}
public int Order => 0;
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
2017-04-30 02:37:51 +00:00
Url = url
});
}
}
}