2014-02-03 05:35:43 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
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;
|
|
|
|
|
using MediaBrowser.Model.Providers;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.TV
|
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder, IHasChangeMonitor
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
2014-03-27 02:14:06 +00:00
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2013-11-04 19:04:23 +00:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
public TvdbSeasonImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
_config = config;
|
2014-01-28 18:37:01 +00:00
|
|
|
|
_httpClient = httpClient;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
|
|
|
|
{
|
2013-11-04 21:50:37 +00:00
|
|
|
|
get { return "TheTVDB"; }
|
2013-11-04 19:04:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-21 18:37:34 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
return item is Season;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
|
|
|
|
{
|
|
|
|
|
return new List<ImageType>
|
|
|
|
|
{
|
|
|
|
|
ImageType.Primary,
|
|
|
|
|
ImageType.Banner,
|
|
|
|
|
ImageType.Backdrop
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 05:11:54 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
var season = (Season)item;
|
2014-02-03 05:35:43 +00:00
|
|
|
|
var series = season.Series;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
var seriesId = series != null ? series.GetProviderId(MetadataProviders.Tvdb) : null;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId) && season.IndexNumber.HasValue)
|
|
|
|
|
{
|
2014-02-03 05:35:43 +00:00
|
|
|
|
await TvdbSeriesProvider.Current.EnsureSeriesInfo(seriesId, series.GetPreferredMetadataLanguage(), cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2013-11-04 19:04:23 +00:00
|
|
|
|
// Process images
|
2013-11-04 20:49:36 +00:00
|
|
|
|
var seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, seriesId);
|
2013-11-04 19:04:23 +00:00
|
|
|
|
|
|
|
|
|
var path = Path.Combine(seriesDataPath, "banners.xml");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-03-27 02:14:06 +00:00
|
|
|
|
int seasonNumber = AdjustForSeriesOffset(series, season.IndexNumber.Value);
|
|
|
|
|
return GetImages(path, item.GetPreferredMetadataLanguage(), seasonNumber, cancellationToken);
|
2013-11-04 19:04:23 +00:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No tvdb data yet. Don't blow up
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
return new RemoteImageInfo[] { };
|
2013-11-04 19:04:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
private int AdjustForSeriesOffset(Series series, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
var offset = TvdbSeriesProvider.GetSeriesOffset(series.ProviderIds);
|
|
|
|
|
if (offset != null)
|
|
|
|
|
return (int) (seasonNumber + offset);
|
|
|
|
|
|
|
|
|
|
return seasonNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static IEnumerable<RemoteImageInfo> GetImages(string xmlPath, string preferredLanguage, int seasonNumber, CancellationToken cancellationToken)
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
var settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(xmlPath, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Banner":
|
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
AddImage(subtree, list, seasonNumber);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-27 00:23:58 +00:00
|
|
|
|
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
|
2013-11-04 19:04:23 +00:00
|
|
|
|
|
|
|
|
|
return list.OrderByDescending(i =>
|
|
|
|
|
{
|
2013-12-27 00:23:58 +00:00
|
|
|
|
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
2013-11-04 21:50:37 +00:00
|
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0)
|
2013-11-04 19:04:23 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
private static void AddImage(XmlReader reader, List<RemoteImageInfo> images, int seasonNumber)
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Rating":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
double rval;
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
if (double.TryParse(val, NumberStyles.Any, UsCulture, out rval))
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
rating = rval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "RatingCount":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
int rval;
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
|
2013-11-04 19:04:23 +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 "BannerType2":
|
|
|
|
|
{
|
|
|
|
|
bannerType2 = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
|
|
|
|
|
// Sometimes the resolution is stuffed in here
|
|
|
|
|
var resolutionParts = bannerType2.Split('x');
|
|
|
|
|
|
|
|
|
|
if (resolutionParts.Length == 2)
|
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
if (int.TryParse(resolutionParts[0], NumberStyles.Integer, UsCulture, out rval))
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
width = rval;
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 02:14:06 +00:00
|
|
|
|
if (int.TryParse(resolutionParts[1], NumberStyles.Integer, UsCulture, out rval))
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(url) && bannerSeason.HasValue && bannerSeason.Value == seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
var imageInfo = new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
RatingType = RatingType.Score,
|
|
|
|
|
CommunityRating = rating,
|
|
|
|
|
VoteCount = voteCount,
|
|
|
|
|
Url = TVUtils.BannerUrl + url,
|
2014-03-27 02:14:06 +00:00
|
|
|
|
ProviderName = ProviderName,
|
2013-11-04 19:04:23 +00:00
|
|
|
|
Language = language,
|
|
|
|
|
Width = width,
|
2013-11-04 20:49:36 +00:00
|
|
|
|
Height = height
|
2013-11-04 19:04:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-11-04 20:49:36 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(thumbnailUrl))
|
|
|
|
|
{
|
|
|
|
|
imageInfo.ThumbnailUrl = TVUtils.BannerUrl + thumbnailUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-04 19:04:23 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
|
public int Order
|
2013-11-04 19:04:23 +00:00
|
|
|
|
{
|
2014-01-28 18:37:01 +00:00
|
|
|
|
get { return 0; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvdbSeriesProvider.Current.TvDbResourcePool
|
|
|
|
|
});
|
2013-11-04 19:04:23 +00:00
|
|
|
|
}
|
2014-02-03 05:35:43 +00:00
|
|
|
|
|
2014-02-10 18:39:41 +00:00
|
|
|
|
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
|
2014-02-03 05:35:43 +00:00
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
if (item.LocationType != LocationType.Virtual)
|
2014-02-08 20:02:35 +00:00
|
|
|
|
{
|
2014-02-08 22:38:02 +00:00
|
|
|
|
// For non-virtual items, only enable if configured
|
|
|
|
|
if (!_config.Configuration.EnableTvDbUpdates)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-08 20:02:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 05:35:43 +00:00
|
|
|
|
var season = (Season)item;
|
|
|
|
|
var series = season.Series;
|
|
|
|
|
|
|
|
|
|
if (series == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tvdbId = series.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrEmpty(tvdbId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var imagesXmlPath = Path.Combine(TvdbSeriesProvider.GetSeriesDataPath(_config.ApplicationPaths, tvdbId), "banners.xml");
|
|
|
|
|
|
|
|
|
|
var fileInfo = new FileInfo(imagesXmlPath);
|
|
|
|
|
|
|
|
|
|
return fileInfo.Exists && _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2013-11-04 19:04:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|