2014-02-04 20:19:29 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
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;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-02-04 20:19:29 +00:00
|
|
|
|
using System;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-11-04 21:50:37 +00:00
|
|
|
|
using System.Globalization;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.TV
|
|
|
|
|
{
|
2014-02-04 20:19:29 +00:00
|
|
|
|
public class TvdbEpisodeImageProvider : IRemoteImageProvider, IHasChangeMonitor
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
2013-11-04 21:50:37 +00:00
|
|
|
|
get { return "TheTVDB"; }
|
2013-11-01 15:55:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-11-01 15:55:25 +00:00
|
|
|
|
{
|
|
|
|
|
return item is Episode;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
|
|
|
|
{
|
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages 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
|
|
|
|
|
2014-02-06 15:58:49 +00:00
|
|
|
|
var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tvdb) : null;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesId);
|
2014-03-27 02:14:06 +00:00
|
|
|
|
var indexOffset = TvdbSeriesProvider.GetSeriesOffset(series.ProviderIds) ?? 0;
|
2013-11-01 15:55:25 +00:00
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
var files = TvdbEpisodeProvider.Current.GetEpisodeXmlFiles(episode.ParentIndexNumber + indexOffset, episode.IndexNumber, episode.IndexNumberEnd, seriesDataPath);
|
2013-11-01 15:55:25 +00:00
|
|
|
|
|
2013-11-04 21:50:37 +00:00
|
|
|
|
var result = files.Select(i => GetImageInfo(i, cancellationToken))
|
|
|
|
|
.Where(i => i != null);
|
2013-11-01 15:55:25 +00:00
|
|
|
|
|
|
|
|
|
return Task.FromResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult<IEnumerable<RemoteImageInfo>>(new RemoteImageInfo[] { });
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 03:38:46 +00:00
|
|
|
|
private RemoteImageInfo GetImageInfo(FileSystemMetadata xmlFile, CancellationToken cancellationToken)
|
2013-11-01 15:55:25 +00:00
|
|
|
|
{
|
|
|
|
|
var height = 225;
|
|
|
|
|
var width = 400;
|
|
|
|
|
var url = string.Empty;
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(xmlFile.FullName, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "thumb_width":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
width = rval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "thumb_height":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
height = rval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "filename":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
url = TVUtils.BannerUrl + val;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
Width = width,
|
|
|
|
|
Height = height,
|
|
|
|
|
ProviderName = Name,
|
|
|
|
|
Url = url,
|
|
|
|
|
Type = ImageType.Primary
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public int Order
|
2013-11-01 15:55:25 +00:00
|
|
|
|
{
|
|
|
|
|
get { return 0; }
|
|
|
|
|
}
|
2014-01-28 18:37:01 +00:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvdbSeriesProvider.Current.TvDbResourcePool
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-02-04 20:19:29 +00:00
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
2014-02-04 20:19:29 +00:00
|
|
|
|
{
|
2014-08-14 13:24:30 +00:00
|
|
|
|
var episode = (Episode)item;
|
|
|
|
|
|
|
|
|
|
if (!episode.IsVirtualUnaired)
|
2014-02-08 20:02:35 +00:00
|
|
|
|
{
|
2014-08-14 13:24:30 +00:00
|
|
|
|
// For non-unaired items, only enable if configured
|
2015-01-18 05:45:10 +00:00
|
|
|
|
if (!TvdbSeriesProvider.Current.GetTvDbOptions().EnableAutomaticUpdates)
|
2014-02-08 22:38:02 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-08 20:02:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
if (!item.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
var series = episode.Series;
|
|
|
|
|
|
|
|
|
|
var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tvdb) : null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesId);
|
|
|
|
|
|
|
|
|
|
var files = TvdbEpisodeProvider.Current.GetEpisodeXmlFiles(episode.ParentIndexNumber, episode.IndexNumber, episode.IndexNumberEnd, seriesDataPath);
|
|
|
|
|
|
|
|
|
|
return files.Any(i => _fileSystem.GetLastWriteTimeUtc(i) > date);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-08 20:02:35 +00:00
|
|
|
|
|
2014-02-04 20:19:29 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-01 15:55:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|