2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-25 00:13:45 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-03-03 06:58:04 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-03-02 17:59:15 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2013-09-15 17:34:48 +00:00
|
|
|
|
using System.Text;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.TV
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RemoteSeasonProvider
|
|
|
|
|
/// </summary>
|
|
|
|
|
class RemoteSeasonProvider : BaseMetadataProvider
|
|
|
|
|
{
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _provider manager
|
|
|
|
|
/// </summary>
|
2013-03-08 05:08:27 +00:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RemoteSeasonProvider"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
|
|
|
|
/// <param name="providerManager">The provider manager.</param>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">httpClient</exception>
|
2013-10-31 14:03:23 +00:00
|
|
|
|
public RemoteSeasonProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
|
2013-03-04 05:43:06 +00:00
|
|
|
|
: base(logManager, configurationManager)
|
2013-02-25 00:13:45 +00:00
|
|
|
|
{
|
2013-03-08 05:08:27 +00:00
|
|
|
|
_providerManager = providerManager;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-02-25 00:13:45 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supportses the specified item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
public override bool Supports(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
return item is Season;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
2013-05-12 23:36:42 +00:00
|
|
|
|
// Run after fanart
|
|
|
|
|
get { return MetadataProviderPriority.Fourth; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [requires internet].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
|
|
|
|
|
public override bool RequiresInternet
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-25 01:22:21 +00:00
|
|
|
|
public override ItemUpdateType ItemUpdateType
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ItemUpdateType.ImageUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating whether [refresh on version change].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
|
|
|
|
|
protected override bool RefreshOnVersionChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the provider version.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The provider version.</value>
|
|
|
|
|
protected override string ProviderVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
return "2";
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 23:35:11 +00:00
|
|
|
|
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
|
|
|
|
var season = (Season)item;
|
|
|
|
|
var seriesId = season.Series != null ? season.Series.GetProviderId(MetadataProviders.Tvdb) : null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var imagesXmlPath = Path.Combine(RemoteSeriesProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "banners.xml");
|
|
|
|
|
|
|
|
|
|
var imagesFileInfo = new FileInfo(imagesXmlPath);
|
|
|
|
|
|
2013-06-20 01:41:50 +00:00
|
|
|
|
if (imagesFileInfo.Exists)
|
|
|
|
|
{
|
2013-10-31 14:03:23 +00:00
|
|
|
|
return _fileSystem.GetLastWriteTimeUtc(imagesFileInfo) > providerInfo.LastRefreshed;
|
2013-06-20 01:41:50 +00:00
|
|
|
|
}
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-10-16 23:35:11 +00:00
|
|
|
|
return false;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Banner) && item.BackdropImagePaths.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="force">if set to <c>true</c> [force].</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2013-03-08 05:08:27 +00:00
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-02-25 00:13:45 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var season = (Season)item;
|
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
var seriesId = season.Series != null ? season.Series.GetProviderId(MetadataProviders.Tvdb) : null;
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
var seasonNumber = season.IndexNumber;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId) && seasonNumber.HasValue)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-21 03:16:43 +00:00
|
|
|
|
// Process images
|
|
|
|
|
var imagesXmlPath = Path.Combine(RemoteSeriesProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "banners.xml");
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
try
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
var fanartData = FetchFanartXmlData(imagesXmlPath, seasonNumber.Value, cancellationToken);
|
2013-11-02 19:30:29 +00:00
|
|
|
|
await DownloadImages(item, fanartData, ConfigurationManager.Configuration.MaxBackdrops, cancellationToken).ConfigureAwait(false);
|
2013-09-15 17:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie. Not all series have images
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-05-12 23:16:46 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
|
|
|
|
return true;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
private async Task DownloadImages(BaseItem item, FanartXmlData data, int backdropLimit, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (!item.HasImage(ImageType.Primary))
|
|
|
|
|
{
|
|
|
|
|
var url = data.LanguagePoster ?? data.Poster;
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
url = TVUtils.BannerUrl + url;
|
2013-05-12 23:16:46 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
await _providerManager.SaveImage(item, url, RemoteSeriesProvider.Current.TvDbResourcePool, ImageType.Primary, null, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ConfigurationManager.Configuration.DownloadSeasonImages.Banner && !item.HasImage(ImageType.Banner))
|
2013-05-12 23:16:46 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
var url = data.LanguageBanner ?? data.Banner;
|
|
|
|
|
if (!string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
url = TVUtils.BannerUrl + url;
|
|
|
|
|
|
|
|
|
|
await _providerManager.SaveImage(item, url, RemoteSeriesProvider.Current.TvDbResourcePool, ImageType.Banner, null, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
}
|
2013-05-12 23:16:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 19:03:21 +00:00
|
|
|
|
if (ConfigurationManager.Configuration.DownloadSeasonImages.Backdrops && item.BackdropImagePaths.Count < backdropLimit)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
var bdNo = item.BackdropImagePaths.Count;
|
|
|
|
|
|
|
|
|
|
foreach (var backdrop in data.Backdrops)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-22 19:03:21 +00:00
|
|
|
|
var url = TVUtils.BannerUrl + backdrop.Url;
|
|
|
|
|
|
|
|
|
|
if (item.ContainsImageWithSourceUrl(url))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
await _providerManager.SaveImage(item, url, RemoteSeriesProvider.Current.TvDbResourcePool, ImageType.Backdrop, bdNo, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
bdNo++;
|
|
|
|
|
|
|
|
|
|
if (item.BackdropImagePaths.Count >= backdropLimit) break;
|
2013-05-12 23:16:46 +00:00
|
|
|
|
}
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private FanartXmlData FetchFanartXmlData(string bannersXmlPath, int seasonNumber, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
2013-05-12 23:16:46 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
var data = new FanartXmlData();
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(bannersXmlPath, Encoding.UTF8))
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
2013-05-12 23:16:46 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-05-26 14:51:48 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
switch (reader.Name)
|
2013-05-26 14:51:48 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
case "Banner":
|
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
FetchInfoFromBannerNode(data, subtree, seasonNumber);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
2013-05-26 14:51:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-12 23:16:46 +00:00
|
|
|
|
}
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FetchInfoFromBannerNode(FanartXmlData data, XmlReader reader, int seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
string bannerType = null;
|
|
|
|
|
string bannerType2 = null;
|
|
|
|
|
string url = null;
|
|
|
|
|
int? bannerSeason = null;
|
2013-10-22 19:03:21 +00:00
|
|
|
|
string resolution = null;
|
2013-10-27 22:19:23 +00:00
|
|
|
|
string language = null;
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
switch (reader.Name)
|
2013-05-12 23:16:46 +00:00
|
|
|
|
{
|
2013-10-27 22:19:23 +00:00
|
|
|
|
case "Language":
|
|
|
|
|
{
|
|
|
|
|
language = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
case "BannerType":
|
|
|
|
|
{
|
|
|
|
|
bannerType = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "BannerType2":
|
|
|
|
|
{
|
|
|
|
|
bannerType2 = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "BannerPath":
|
|
|
|
|
{
|
|
|
|
|
url = reader.ReadElementContentAsString() ?? string.Empty;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
case "Season":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
bannerSeason = int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 20:25:58 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(url) && bannerSeason.HasValue && bannerSeason.Value == seasonNumber)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(bannerType, "season", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(bannerType2, "season", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2013-10-26 04:12:20 +00:00
|
|
|
|
// Just grab the first
|
|
|
|
|
if (string.IsNullOrWhiteSpace(data.Poster))
|
|
|
|
|
{
|
|
|
|
|
data.Poster = url;
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(bannerType2, "seasonwide", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2013-10-27 22:19:23 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(language) || string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
|
2013-10-26 04:12:20 +00:00
|
|
|
|
{
|
2013-10-27 22:19:23 +00:00
|
|
|
|
// Just grab the first
|
|
|
|
|
if (string.IsNullOrWhiteSpace(data.Banner))
|
|
|
|
|
{
|
|
|
|
|
data.Banner = url;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(language, ConfigurationManager.Configuration.PreferredMetadataLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
// Just grab the first
|
|
|
|
|
if (string.IsNullOrWhiteSpace(data.LanguageBanner))
|
|
|
|
|
{
|
|
|
|
|
data.LanguageBanner = url;
|
|
|
|
|
}
|
2013-10-26 04:12:20 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
else if (string.Equals(bannerType, "fanart", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2013-10-22 19:03:21 +00:00
|
|
|
|
data.Backdrops.Add(new ImageInfo
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
Resolution = resolution
|
|
|
|
|
});
|
2013-09-15 17:34:48 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|