2019-01-13 20:03:10 +00:00
|
|
|
using System.Collections.Generic;
|
2019-01-13 19:26:31 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml;
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-28 18:37:01 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-01 15:55:25 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
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;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace MediaBrowser.Providers.TV.TheTVDB
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
2016-05-25 00:42:12 +00:00
|
|
|
public class TvdbEpisodeImageProvider : IRemoteImageProvider
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2014-01-28 18:37:01 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
2014-02-04 20:19:29 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
public TvdbEpisodeImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
_config = config;
|
2014-01-28 18:37:01 +00:00
|
|
|
_httpClient = httpClient;
|
2014-02-04 20:19:29 +00:00
|
|
|
_fileSystem = fileSystem;
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => "TheTVDB";
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
return item is Episode;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
|
|
|
ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
var episode = (Episode)item;
|
2014-02-06 15:58:49 +00:00
|
|
|
var series = episode.Series;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2015-11-24 18:45:52 +00:00
|
|
|
if (series != null && TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
// Process images
|
2015-11-24 18:45:52 +00:00
|
|
|
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, series.ProviderIds);
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
var nodes = TvdbEpisodeProvider.Current.GetEpisodeXmlNodes(seriesDataPath, episode.GetLookupInfo());
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2015-12-25 21:18:27 +00:00
|
|
|
var result = nodes.Select(i => GetImageInfo(i, cancellationToken))
|
|
|
|
.Where(i => i != null)
|
2019-01-07 23:24:34 +00:00
|
|
|
.ToList();
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
return Task.FromResult<IEnumerable<RemoteImageInfo>>(result);
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Task.FromResult<IEnumerable<RemoteImageInfo>>(new RemoteImageInfo[] { });
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
private RemoteImageInfo GetImageInfo(XmlReader reader, CancellationToken cancellationToken)
|
2013-11-01 15:55:25 +00:00
|
|
|
{
|
|
|
|
var height = 225;
|
|
|
|
var width = 400;
|
|
|
|
var url = string.Empty;
|
|
|
|
|
2019-01-07 23:24:34 +00:00
|
|
|
// Use XmlReader for best performance
|
|
|
|
using (reader)
|
|
|
|
{
|
2016-11-02 17:29:06 +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)
|
2016-11-02 17:29:06 +00:00
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "thumb_width":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
{
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
2019-01-13 20:46:33 +00:00
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
|
2016-11-02 17:29:06 +00:00
|
|
|
{
|
|
|
|
width = rval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "thumb_height":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
{
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
2019-01-13 20:46:33 +00:00
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out var rval))
|
2016-11-02 17:29:06 +00:00
|
|
|
{
|
|
|
|
height = rval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "filename":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
{
|
|
|
|
url = TVUtils.BannerUrl + val;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 23:24:34 +00:00
|
|
|
}
|
2013-11-01 15:55:25 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
{
|
|
|
|
Width = width,
|
|
|
|
Height = height,
|
|
|
|
ProviderName = Name,
|
|
|
|
Url = url,
|
|
|
|
Type = ImageType.Primary
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 0;
|
2014-01-28 18:37:01 +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-01-28 18:37:01 +00:00
|
|
|
});
|
|
|
|
}
|
2013-11-01 15:55:25 +00:00
|
|
|
}
|
|
|
|
}
|