jellyfin-server/MediaBrowser.Controller/Providers/TV/RemoteSeasonProvider.cs

298 lines
14 KiB
C#
Raw Normal View History

2013-02-25 00:13:45 +00:00
using MediaBrowser.Common.Net;
2013-03-04 05:43:06 +00:00
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;
using MediaBrowser.Controller.Library;
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 MediaBrowser.Model.Net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace MediaBrowser.Controller.Providers.TV
{
/// <summary>
/// Class RemoteSeasonProvider
/// </summary>
class RemoteSeasonProvider : BaseMetadataProvider
{
2013-02-25 00:13:45 +00:00
/// <summary>
/// Gets the HTTP client.
/// </summary>
/// <value>The HTTP client.</value>
protected IHttpClient HttpClient { get; private set; }
private readonly IProviderManager _providerManager;
2013-03-04 05:43:06 +00:00
public RemoteSeasonProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
2013-03-04 05:43:06 +00:00
: base(logManager, configurationManager)
2013-02-25 00:13:45 +00:00
{
if (httpClient == null)
{
throw new ArgumentNullException("httpClient");
}
HttpClient = httpClient;
_providerManager = providerManager;
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
{
// 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-05-14 16:16:43 +00:00
protected override bool RefreshOnFileSystemStampChange
{
get
{
return ConfigurationManager.Configuration.SaveLocalMeta;
}
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
2013-04-28 23:39:17 +00:00
if (HasLocalMeta(item))
2013-02-21 01:33:05 +00:00
{
2013-04-28 23:39:17 +00:00
return false;
2013-02-21 01:33:05 +00:00
}
2013-04-28 23:39:17 +00:00
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>
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;
if (!HasLocalMeta(item))
{
var seriesId = season.Series != null ? season.Series.GetProviderId(MetadataProviders.Tvdb) : null;
if (seriesId != null)
{
2013-05-12 23:16:46 +00:00
var status = await FetchSeasonData(season, seriesId, cancellationToken).ConfigureAwait(false);
SetLastRefreshed(item, DateTime.UtcNow, status);
2013-02-21 01:33:05 +00:00
return true;
}
Logger.Info("Season provider unable to obtain series id for {0}", item.Path);
}
else
{
Logger.Info("Season provider not fetching because local meta exists: " + season.Name);
}
return false;
}
/// <summary>
/// Fetches the season data.
/// </summary>
/// <param name="season">The season.</param>
/// <param name="seriesId">The series id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
2013-05-12 23:16:46 +00:00
private async Task<ProviderRefreshStatus> FetchSeasonData(Season season, string seriesId, CancellationToken cancellationToken)
2013-02-21 01:33:05 +00:00
{
var seasonNumber = TVUtils.GetSeasonNumberFromPath(season.Path) ?? -1;
season.IndexNumber = seasonNumber;
if (seasonNumber == 0)
{
season.Name = "Specials";
}
2013-05-12 23:16:46 +00:00
var status = ProviderRefreshStatus.Success;
if (string.IsNullOrEmpty(seriesId))
{
return status;
}
if ((season.PrimaryImagePath == null) || (!season.HasImage(ImageType.Banner)) || (season.BackdropImagePaths == null))
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
var images = new XmlDocument();
var url = string.Format("http://www.thetvdb.com/api/" + TVUtils.TVDBApiKey + "/series/{0}/banners.xml", seriesId);
using (var imgs = await HttpClient.Get(new HttpRequestOptions
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
Url = url,
ResourcePool = RemoteSeriesProvider.Current.TvDbResourcePool,
CancellationToken = cancellationToken,
EnableResponseCache = true
2013-02-21 01:33:05 +00:00
2013-05-12 23:16:46 +00:00
}).ConfigureAwait(false))
{
images.Load(imgs);
}
if (images.HasChildNodes)
{
if (ConfigurationManager.Configuration.RefreshItemImages || !season.HasLocalImage("folder"))
2013-02-21 01:33:05 +00:00
{
2013-05-14 16:16:43 +00:00
var n = images.SelectSingleNode("//Banner[BannerType='season'][BannerType2='season'][Season='" + seasonNumber + "'][Language='" + ConfigurationManager.Configuration.PreferredMetadataLanguage + "']") ??
images.SelectSingleNode("//Banner[BannerType='season'][BannerType2='season'][Season='" + seasonNumber + "'][Language='en']");
2013-05-12 23:16:46 +00:00
if (n != null)
{
2013-05-12 23:16:46 +00:00
n = n.SelectSingleNode("./BannerPath");
2013-05-12 23:16:46 +00:00
try
{
if (n != null)
season.PrimaryImagePath = await _providerManager.DownloadAndSaveImage(season, TVUtils.BannerUrl + n.InnerText, "folder" + Path.GetExtension(n.InnerText), ConfigurationManager.Configuration.SaveLocalMeta, RemoteSeriesProvider.Current.TvDbResourcePool, cancellationToken).ConfigureAwait(false);
}
catch (HttpException)
{
status = ProviderRefreshStatus.CompletedWithErrors;
}
2013-02-21 01:33:05 +00:00
}
}
2013-05-12 23:16:46 +00:00
if (ConfigurationManager.Configuration.DownloadSeasonImages.Banner && (ConfigurationManager.Configuration.RefreshItemImages || !season.HasLocalImage("banner")))
2013-02-21 01:33:05 +00:00
{
2013-05-14 16:16:43 +00:00
var n = images.SelectSingleNode("//Banner[BannerType='season'][BannerType2='seasonwide'][Season='" + seasonNumber + "'][Language='" + ConfigurationManager.Configuration.PreferredMetadataLanguage + "']") ??
images.SelectSingleNode("//Banner[BannerType='season'][BannerType2='seasonwide'][Season='" + seasonNumber + "'][Language='en']");
2013-05-12 23:16:46 +00:00
if (n != null)
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
n = n.SelectSingleNode("./BannerPath");
2013-02-21 01:33:05 +00:00
if (n != null)
{
try
{
2013-05-12 23:16:46 +00:00
var bannerImagePath =
await _providerManager.DownloadAndSaveImage(season,
TVUtils.BannerUrl + n.InnerText,
"banner" +
Path.GetExtension(n.InnerText),
ConfigurationManager.Configuration.SaveLocalMeta, RemoteSeriesProvider.Current.TvDbResourcePool, cancellationToken).
ConfigureAwait(false);
season.SetImage(ImageType.Banner, bannerImagePath);
2013-02-21 01:33:05 +00:00
}
catch (HttpException)
{
2013-05-12 23:16:46 +00:00
status = ProviderRefreshStatus.CompletedWithErrors;
2013-02-21 01:33:05 +00:00
}
}
}
2013-05-12 23:16:46 +00:00
}
2013-02-21 01:33:05 +00:00
2013-05-12 23:16:46 +00:00
if (ConfigurationManager.Configuration.DownloadSeasonImages.Backdrops && (ConfigurationManager.Configuration.RefreshItemImages || !season.HasLocalImage("backdrop")))
{
var n = images.SelectSingleNode("//Banner[BannerType='fanart'][Season='" + seasonNumber + "']");
if (n != null)
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
n = n.SelectSingleNode("./BannerPath");
2013-02-21 01:33:05 +00:00
if (n != null)
{
2013-05-12 23:16:46 +00:00
try
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
if (season.BackdropImagePaths == null) season.BackdropImagePaths = new List<string>();
season.BackdropImagePaths.Add(await _providerManager.DownloadAndSaveImage(season, TVUtils.BannerUrl + n.InnerText, "backdrop" + Path.GetExtension(n.InnerText), ConfigurationManager.Configuration.SaveLocalMeta, RemoteSeriesProvider.Current.TvDbResourcePool, cancellationToken).ConfigureAwait(false));
}
catch (HttpException)
{
status = ProviderRefreshStatus.CompletedWithErrors;
2013-02-21 01:33:05 +00:00
}
}
}
2013-05-12 23:16:46 +00:00
else if (!ConfigurationManager.Configuration.SaveLocalMeta) //if saving local - season will inherit from series
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
// not necessarily accurate but will give a different bit of art to each season
var lst = images.SelectNodes("//Banner[BannerType='fanart']");
if (lst != null && lst.Count > 0)
2013-02-21 01:33:05 +00:00
{
2013-05-12 23:16:46 +00:00
var num = seasonNumber % lst.Count;
n = lst[num];
2013-02-21 01:33:05 +00:00
n = n.SelectSingleNode("./BannerPath");
if (n != null)
{
2013-05-12 23:16:46 +00:00
if (season.BackdropImagePaths == null)
season.BackdropImagePaths = new List<string>();
2013-02-21 01:33:05 +00:00
try
{
2013-05-12 23:16:46 +00:00
season.BackdropImagePaths.Add(
await _providerManager.DownloadAndSaveImage(season,
TVUtils.BannerUrl +
n.InnerText,
"backdrop" +
Path.GetExtension(
n.InnerText),
ConfigurationManager.Configuration.SaveLocalMeta, RemoteSeriesProvider.Current.TvDbResourcePool, cancellationToken)
.ConfigureAwait(false));
2013-02-21 01:33:05 +00:00
}
catch (HttpException)
{
2013-05-12 23:16:46 +00:00
status = ProviderRefreshStatus.CompletedWithErrors;
2013-02-21 01:33:05 +00:00
}
}
}
}
}
}
}
2013-05-12 23:16:46 +00:00
return status;
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Determines whether [has local meta] [the specified item].
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if [has local meta] [the specified item]; otherwise, <c>false</c>.</returns>
private bool HasLocalMeta(BaseItem item)
{
//just folder.jpg/png
return (item.ResolveArgs.ContainsMetaFileByName("folder.jpg") ||
item.ResolveArgs.ContainsMetaFileByName("folder.png"));
}
}
}