2019-01-13 20:03:10 +00:00
|
|
|
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;
|
2013-05-24 15:29:10 +00:00
|
|
|
using System.IO;
|
2013-10-26 04:12:20 +00:00
|
|
|
using System.Linq;
|
2014-02-03 20:51:28 +00:00
|
|
|
using System.Text;
|
2013-05-24 15:29:10 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2014-02-03 20:51:28 +00:00
|
|
|
using System.Xml;
|
2019-01-13 19:26:31 +00:00
|
|
|
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;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Model.Providers;
|
2016-10-27 07:58:33 +00:00
|
|
|
using MediaBrowser.Model.Xml;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace MediaBrowser.Providers.TV.TheTVDB
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2016-05-25 00:42:12 +00:00
|
|
|
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2013-10-31 14:03:23 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2016-10-27 07:58:33 +00:00
|
|
|
private readonly IXmlReaderSettingsFactory _xmlReaderSettingsFactory;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2016-10-27 07:58:33 +00:00
|
|
|
public TvdbSeriesImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IXmlReaderSettingsFactory xmlReaderSettingsFactory)
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
_config = config;
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
_fileSystem = fileSystem;
|
2016-10-27 07:58:33 +00:00
|
|
|
_xmlReaderSettingsFactory = xmlReaderSettingsFactory;
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => ProviderName;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public static string ProviderName => "TheTVDB";
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return item is Series;
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2013-06-25 01:22:21 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return new List<ImageType>
|
2013-06-25 01:22:21 +00:00
|
|
|
{
|
2019-01-07 23:27:46 +00:00
|
|
|
ImageType.Primary,
|
2014-02-03 20:51:28 +00:00
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Backdrop
|
|
|
|
};
|
2013-06-25 01:22:21 +00:00
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2015-10-21 05:09:50 +00:00
|
|
|
if (TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
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);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (string.IsNullOrEmpty(seriesDataPath))
|
|
|
|
{
|
|
|
|
return new RemoteImageInfo[] { };
|
|
|
|
}
|
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
var path = Path.Combine(seriesDataPath, "banners.xml");
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return GetImages(path, language, cancellationToken);
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException)
|
2013-06-20 14:48:49 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
// No tvdb data yet. Don't blow up
|
2013-06-20 14:48:49 +00:00
|
|
|
}
|
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-05-24 15:29:10 +00:00
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
return new RemoteImageInfo[] { };
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
private IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, CancellationToken cancellationToken)
|
2013-10-22 19:03:21 +00:00
|
|
|
{
|
2016-10-27 07:58:33 +00:00
|
|
|
var settings = _xmlReaderSettingsFactory.Create(false);
|
|
|
|
|
|
|
|
settings.CheckCharacters = false;
|
|
|
|
settings.IgnoreProcessingInstructions = true;
|
|
|
|
settings.IgnoreComments = true;
|
2013-12-15 18:33:07 +00:00
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
var list = new List<RemoteImageInfo>();
|
2013-05-24 15:29:10 +00:00
|
|
|
|
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();
|
2013-05-24 15:29:10 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2014-02-03 20:51:28 +00:00
|
|
|
return list.OrderByDescending(i =>
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
|
2013-11-04 20:49:36 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (!isLanguageEn)
|
|
|
|
{
|
|
|
|
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
2013-12-15 18:33:07 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return 2;
|
2013-12-15 18:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 20:51:28 +00:00
|
|
|
if (string.IsNullOrEmpty(i.Language))
|
2013-11-04 20:49:36 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return isLanguageEn ? 3 : 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
2017-08-09 19:56:38 +00:00
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
2014-02-03 20:51:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void AddImage(XmlReader reader, List<RemoteImageInfo> images)
|
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
2013-12-15 18:33:07 +00:00
|
|
|
|
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)
|
2013-12-15 18:33:07 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
case "Rating":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
if (double.TryParse(val, NumberStyles.Any, _usCulture, out var rval))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
rating = rval;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "RatingCount":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2019-01-13 20:46:33 +00:00
|
|
|
if (int.TryParse(resolutionParts[0], NumberStyles.Integer, _usCulture, out var rval))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
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-12-15 18:33:07 +00:00
|
|
|
}
|
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
|
|
|
|
};
|
2013-10-22 19:03:21 +00:00
|
|
|
|
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);
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 20:51:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 0;
|
2014-02-03 20:51:28 +00:00
|
|
|
|
|
|
|
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
|
2014-02-03 20:51:28 +00:00
|
|
|
});
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
}
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|