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;
|
2013-02-21 17:22:22 +00:00
|
|
|
|
using MediaBrowser.Controller.Extensions;
|
2013-03-03 06:58:04 +00:00
|
|
|
|
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.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers.TV
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RemoteEpisodeProvider
|
|
|
|
|
/// </summary>
|
|
|
|
|
class RemoteEpisodeProvider : BaseMetadataProvider
|
|
|
|
|
{
|
2013-03-08 05:08:27 +00:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2013-03-04 05:43:06 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RemoteEpisodeProvider" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpClient">The HTTP client.</param>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
2013-03-08 05:08:27 +00:00
|
|
|
|
public RemoteEpisodeProvider(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
|
|
|
|
{
|
|
|
|
|
HttpClient = httpClient;
|
2013-03-08 05:08:27 +00:00
|
|
|
|
_providerManager = providerManager;
|
2013-02-25 00:13:45 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The episode query
|
|
|
|
|
/// </summary>
|
2013-05-18 18:16:07 +00:00
|
|
|
|
private const string EpisodeQuery = "http://www.thetvdb.com/api/{0}/series/{1}/default/{2}/{3}/{4}.xml";
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The abs episode query
|
|
|
|
|
/// </summary>
|
2013-05-18 18:16:07 +00:00
|
|
|
|
private const string AbsEpisodeQuery = "http://www.thetvdb.com/api/{0}/series/{1}/absolute/{2}/{3}.xml";
|
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 Episode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the priority.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
|
public override MetadataProviderPriority Priority
|
|
|
|
|
{
|
|
|
|
|
get { return MetadataProviderPriority.Second; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool RefreshOnFileSystemStampChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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>
|
|
|
|
|
/// <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();
|
|
|
|
|
|
|
|
|
|
var episode = (Episode)item;
|
2013-05-19 20:19:30 +00:00
|
|
|
|
if (!HasLocalMeta(episode))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
var seriesId = episode.Series != null ? episode.Series.GetProviderId(MetadataProviders.Tvdb) : null;
|
|
|
|
|
|
|
|
|
|
if (seriesId != null)
|
|
|
|
|
{
|
2013-05-12 23:36:42 +00:00
|
|
|
|
var status = await FetchEpisodeData(episode, seriesId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow, status);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-05-19 15:56:03 +00:00
|
|
|
|
Logger.Info("Episode provider not fetching because series does not have a tvdb id: " + item.Path);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Logger.Info("Episode provider not fetching because local meta exists or requested to ignore: " + item.Name);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the episode data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="episode">The episode.</param>
|
|
|
|
|
/// <param name="seriesId">The series id.</param>
|
2013-04-10 15:56:36 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2013-05-12 23:36:42 +00:00
|
|
|
|
private async Task<ProviderRefreshStatus> FetchEpisodeData(Episode episode, string seriesId, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
string location = episode.Path;
|
|
|
|
|
|
2013-05-19 17:05:33 +00:00
|
|
|
|
var episodeNumber = episode.IndexNumber ?? TVUtils.GetEpisodeNumberFromFile(location, episode.Season != null);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-12 23:36:42 +00:00
|
|
|
|
var status = ProviderRefreshStatus.Success;
|
|
|
|
|
|
2013-05-19 17:05:33 +00:00
|
|
|
|
if (episodeNumber == null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Warn("TvDbProvider: Could not determine episode number for: " + episode.Path);
|
2013-05-12 23:36:42 +00:00
|
|
|
|
return status;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
episode.IndexNumber = episodeNumber;
|
|
|
|
|
var usingAbsoluteData = false;
|
|
|
|
|
|
2013-05-12 23:36:42 +00:00
|
|
|
|
if (string.IsNullOrEmpty(seriesId)) return status;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
var seasonNumber = "";
|
|
|
|
|
if (episode.Parent is Season)
|
|
|
|
|
{
|
|
|
|
|
seasonNumber = episode.Parent.IndexNumber.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(seasonNumber))
|
|
|
|
|
seasonNumber = TVUtils.SeasonNumberFromEpisodeFile(location); // try and extract the season number from the file name for S1E1, 1x04 etc.
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seasonNumber))
|
|
|
|
|
{
|
|
|
|
|
seasonNumber = seasonNumber.TrimStart('0');
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(seasonNumber))
|
|
|
|
|
{
|
|
|
|
|
seasonNumber = "0"; // Specials
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-18 18:16:07 +00:00
|
|
|
|
var url = string.Format(EpisodeQuery, TVUtils.TvdbApiKey, seriesId, seasonNumber, episodeNumber, ConfigurationManager.Configuration.PreferredMetadataLanguage);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
|
2013-05-18 18:16:07 +00:00
|
|
|
|
using (var result = await HttpClient.Get(new HttpRequestOptions
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-18 18:16:07 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = RemoteSeriesProvider.Current.TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableResponseCache = true
|
2013-05-06 19:31:57 +00:00
|
|
|
|
|
2013-05-18 18:16:07 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-18 18:16:07 +00:00
|
|
|
|
doc.Load(result);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//episode does not exist under this season, try absolute numbering.
|
|
|
|
|
//still assuming it's numbered as 1x01
|
|
|
|
|
//this is basicly just for anime.
|
|
|
|
|
if (!doc.HasChildNodes && Int32.Parse(seasonNumber) == 1)
|
|
|
|
|
{
|
2013-05-18 18:16:07 +00:00
|
|
|
|
url = string.Format(AbsEpisodeQuery, TVUtils.TvdbApiKey, seriesId, episodeNumber, ConfigurationManager.Configuration.PreferredMetadataLanguage);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-18 18:16:07 +00:00
|
|
|
|
using (var result = await HttpClient.Get(new HttpRequestOptions
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-18 18:16:07 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = RemoteSeriesProvider.Current.TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableResponseCache = true
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-18 18:16:07 +00:00
|
|
|
|
if (result != null) doc.Load(result);
|
|
|
|
|
usingAbsoluteData = true;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.HasChildNodes)
|
|
|
|
|
{
|
2013-05-20 17:14:15 +00:00
|
|
|
|
if (!episode.HasImage(ImageType.Primary))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-20 17:14:15 +00:00
|
|
|
|
var p = doc.SafeGetString("//filename");
|
|
|
|
|
if (p != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-20 17:14:15 +00:00
|
|
|
|
if (!Directory.Exists(episode.MetaLocation)) Directory.CreateDirectory(episode.MetaLocation);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
episode.PrimaryImagePath = await _providerManager.DownloadAndSaveImage(episode, TVUtils.BannerUrl + p, Path.GetFileName(p), ConfigurationManager.Configuration.SaveLocalMeta, RemoteSeriesProvider.Current.TvDbResourcePool, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException)
|
|
|
|
|
{
|
|
|
|
|
status = ProviderRefreshStatus.CompletedWithErrors;
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
episode.Overview = doc.SafeGetString("//Overview");
|
|
|
|
|
if (usingAbsoluteData)
|
|
|
|
|
episode.IndexNumber = doc.SafeGetInt32("//absolute_number", -1);
|
|
|
|
|
if (episode.IndexNumber < 0)
|
|
|
|
|
episode.IndexNumber = doc.SafeGetInt32("//EpisodeNumber");
|
|
|
|
|
|
2013-05-16 19:00:54 +00:00
|
|
|
|
episode.Name = doc.SafeGetString("//EpisodeName");
|
2013-02-21 01:33:05 +00:00
|
|
|
|
episode.CommunityRating = doc.SafeGetSingle("//Rating", -1, 10);
|
|
|
|
|
var firstAired = doc.SafeGetString("//FirstAired");
|
|
|
|
|
DateTime airDate;
|
|
|
|
|
if (DateTime.TryParse(firstAired, out airDate) && airDate.Year > 1850)
|
|
|
|
|
{
|
|
|
|
|
episode.PremiereDate = airDate.ToUniversalTime();
|
|
|
|
|
episode.ProductionYear = airDate.Year;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 05:44:45 +00:00
|
|
|
|
episode.People.Clear();
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var actors = doc.SafeGetString("//GuestStars");
|
|
|
|
|
if (actors != null)
|
|
|
|
|
{
|
2013-04-28 05:44:45 +00:00
|
|
|
|
foreach (var person in actors.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(str => new PersonInfo { Type = PersonType.GuestStar, Name = str }))
|
|
|
|
|
{
|
|
|
|
|
episode.AddPerson(person);
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var directors = doc.SafeGetString("//Director");
|
|
|
|
|
if (directors != null)
|
|
|
|
|
{
|
2013-05-01 13:45:36 +00:00
|
|
|
|
foreach (var person in directors.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(str => new PersonInfo { Type = PersonType.Director, Name = str }))
|
2013-04-28 05:44:45 +00:00
|
|
|
|
{
|
|
|
|
|
episode.AddPerson(person);
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var writers = doc.SafeGetString("//Writer");
|
|
|
|
|
if (writers != null)
|
|
|
|
|
{
|
2013-05-01 13:45:36 +00:00
|
|
|
|
foreach (var person in writers.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries).Select(str => new PersonInfo { Type = PersonType.Writer, Name = str }))
|
2013-04-28 05:44:45 +00:00
|
|
|
|
{
|
|
|
|
|
episode.AddPerson(person);
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-04 05:43:06 +00:00
|
|
|
|
if (ConfigurationManager.Configuration.SaveLocalMeta)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(episode.MetaLocation)) Directory.CreateDirectory(episode.MetaLocation);
|
|
|
|
|
var ms = new MemoryStream();
|
|
|
|
|
doc.Save(ms);
|
|
|
|
|
|
2013-03-08 05:08:27 +00:00
|
|
|
|
await _providerManager.SaveToLibraryFilesystem(episode, Path.Combine(episode.MetaLocation, Path.GetFileNameWithoutExtension(episode.Path) + ".xml"), ms, cancellationToken).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-12 23:36:42 +00:00
|
|
|
|
return status;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-12 23:36:42 +00:00
|
|
|
|
return status;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether [has local meta] [the specified episode].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="episode">The episode.</param>
|
|
|
|
|
/// <returns><c>true</c> if [has local meta] [the specified episode]; otherwise, <c>false</c>.</returns>
|
2013-04-28 23:39:17 +00:00
|
|
|
|
private bool HasLocalMeta(BaseItem episode)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
return (episode.Parent.ResolveArgs.ContainsMetaFileByName(Path.GetFileNameWithoutExtension(episode.Path) + ".xml"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|