2013-06-27 20:25:07 +02:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-02-25 01:13:45 +01:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-04 06:43:06 +01:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-03-03 07:58:04 +01:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 18:47:28 +02:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-03-02 18:59:15 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-06-27 20:25:07 +02:00
|
|
|
|
using MediaBrowser.Providers.Extensions;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
2013-05-18 20:16:07 +02:00
|
|
|
|
using System.Globalization;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System.IO;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
using System.Linq;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
2013-06-27 20:25:07 +02:00
|
|
|
|
using System.Xml.Linq;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-06-09 18:47:28 +02:00
|
|
|
|
namespace MediaBrowser.Providers.TV
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RemoteSeriesProvider
|
|
|
|
|
/// </summary>
|
2013-03-08 06:08:27 +01:00
|
|
|
|
class RemoteSeriesProvider : BaseMetadataProvider, IDisposable
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-03-04 06:43:06 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The tv db
|
|
|
|
|
/// </summary>
|
2013-05-23 17:40:20 +02:00
|
|
|
|
internal readonly SemaphoreSlim TvDbResourcePool = new SemaphoreSlim(2, 2);
|
2013-03-04 06:43:06 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The current.</value>
|
2013-03-04 06:43:06 +01:00
|
|
|
|
internal static RemoteSeriesProvider Current { get; private set; }
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _zip client
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IZipClient _zipClient;
|
|
|
|
|
|
2013-02-25 01:13:45 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the HTTP client.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The HTTP client.</value>
|
|
|
|
|
protected IHttpClient HttpClient { get; private set; }
|
|
|
|
|
|
2013-03-04 06:43:06 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RemoteSeriesProvider" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpClient">The HTTP client.</param>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <param name="zipClient">The zip client.</param>
|
2013-03-04 06:43:06 +01:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">httpClient</exception>
|
2013-07-05 19:40:51 +02:00
|
|
|
|
public RemoteSeriesProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IZipClient zipClient)
|
2013-03-04 06:43:06 +01:00
|
|
|
|
: base(logManager, configurationManager)
|
2013-02-25 01:13:45 +01:00
|
|
|
|
{
|
|
|
|
|
if (httpClient == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("httpClient");
|
|
|
|
|
}
|
|
|
|
|
HttpClient = httpClient;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
_zipClient = zipClient;
|
2013-03-04 06:43:06 +01:00
|
|
|
|
Current = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
2013-03-08 06:08:27 +01:00
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2013-03-04 06:43:06 +01:00
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
TvDbResourcePool.Dispose();
|
|
|
|
|
}
|
2013-02-25 01:13:45 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The root URL
|
|
|
|
|
/// </summary>
|
2013-05-19 21:37:52 +02:00
|
|
|
|
private const string RootUrl = "http://www.thetvdb.com/api/";
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The series query
|
|
|
|
|
/// </summary>
|
2013-05-19 21:37:52 +02:00
|
|
|
|
private const string SeriesQuery = "GetSeries.php?seriesname={0}";
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// The series get zip
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
private const string SeriesGetZip = "http://www.thetvdb.com/api/{0}/series/{1}/all/{2}.zip";
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The LOCA l_ MET a_ FIL e_ NAME
|
|
|
|
|
/// </summary>
|
2013-07-19 20:37:26 +02:00
|
|
|
|
protected const string LocalMetaFileName = "series.xml";
|
2013-02-21 02:33:05 +01: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 Series;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 05:16:43 +02: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
|
|
|
|
|
{
|
|
|
|
|
return "1";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-21 15:43:34 +02:00
|
|
|
|
protected override DateTime CompareDate(BaseItem item)
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2013-05-25 17:16:50 +02:00
|
|
|
|
var seriesId = item.GetProviderId(MetadataProviders.Tvdb);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var path = GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId);
|
|
|
|
|
|
|
|
|
|
var files = new DirectoryInfo(path)
|
|
|
|
|
.EnumerateFiles("*.xml", SearchOption.TopDirectoryOnly)
|
2013-06-21 15:43:34 +02:00
|
|
|
|
.Select(i => i.LastWriteTimeUtc)
|
2013-05-21 05:16:43 +02:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
if (files.Length > 0)
|
|
|
|
|
{
|
2013-06-21 15:43:34 +02:00
|
|
|
|
return files.Max();
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-21 15:43:34 +02:00
|
|
|
|
return base.CompareDate(item);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-05-25 17:16:50 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01: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 06:08:27 +01:00
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
var series = (Series)item;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
|
|
|
|
var seriesId = series.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(seriesId))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-08-28 17:56:56 +02:00
|
|
|
|
seriesId = await FindSeries(series.Name, cancellationToken).ConfigureAwait(false);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-05-18 20:16:07 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
series.SetProviderId(MetadataProviders.Tvdb, seriesId);
|
2013-05-18 20:16:07 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
var seriesDataPath = GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId);
|
2013-05-18 20:16:07 +02:00
|
|
|
|
|
2013-06-23 17:17:42 +02:00
|
|
|
|
await FetchSeriesData(series, seriesId, seriesDataPath, force, cancellationToken).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 17:29:10 +02:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
return true;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the series data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="seriesId">The series id.</param>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <param name="seriesDataPath">The series data path.</param>
|
2013-06-23 17:17:42 +02:00
|
|
|
|
/// <param name="isForcedRefresh">if set to <c>true</c> [is forced refresh].</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2013-06-23 17:17:42 +02:00
|
|
|
|
private async Task FetchSeriesData(Series series, string seriesId, string seriesDataPath, bool isForcedRefresh, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-05-21 05:16:43 +02:00
|
|
|
|
var files = Directory.EnumerateFiles(seriesDataPath, "*.xml", SearchOption.TopDirectoryOnly).Select(Path.GetFileName).ToArray();
|
|
|
|
|
|
|
|
|
|
var seriesXmlFilename = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower() + ".xml";
|
|
|
|
|
|
|
|
|
|
// Only download if not already there
|
|
|
|
|
// The prescan task will take care of updates so we don't need to re-download here
|
|
|
|
|
if (!files.Contains("banners.xml", StringComparer.OrdinalIgnoreCase) || !files.Contains("actors.xml", StringComparer.OrdinalIgnoreCase) || !files.Contains(seriesXmlFilename, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
await DownloadSeriesZip(seriesId, seriesDataPath, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-10 03:40:28 +02:00
|
|
|
|
// Examine if there's no local metadata, or save local is on (to get updates)
|
2013-07-25 22:59:28 +02:00
|
|
|
|
if (isForcedRefresh || ConfigurationManager.Configuration.EnableTvDbUpdates || !HasLocalMeta(series))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-05-21 05:16:43 +02:00
|
|
|
|
var seriesXmlPath = Path.Combine(seriesDataPath, seriesXmlFilename);
|
|
|
|
|
var actorsXmlPath = Path.Combine(seriesDataPath, "actors.xml");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
var seriesDoc = new XmlDocument();
|
|
|
|
|
seriesDoc.Load(seriesXmlPath);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
FetchMainInfo(series, seriesDoc);
|
2013-06-28 01:01:03 +02:00
|
|
|
|
|
2013-06-16 00:19:47 +02:00
|
|
|
|
if (!series.LockedFields.Contains(MetadataFields.Cast))
|
|
|
|
|
{
|
|
|
|
|
var actorsDoc = new XmlDocument();
|
|
|
|
|
actorsDoc.Load(actorsXmlPath);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2013-06-16 00:19:47 +02:00
|
|
|
|
FetchActors(series, actorsDoc, seriesDoc);
|
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads the series zip.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="seriesId">The series id.</param>
|
|
|
|
|
/// <param name="seriesDataPath">The series data path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
internal async Task DownloadSeriesZip(string seriesId, string seriesDataPath, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var url = string.Format(SeriesGetZip, TVUtils.TvdbApiKey, seriesId, ConfigurationManager.Configuration.PreferredMetadataLanguage);
|
2013-05-24 17:29:10 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
using (var zipStream = await HttpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
// Copy to memory stream because we need a seekable stream
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await zipStream.CopyToAsync(ms).ConfigureAwait(false);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
ms.Position = 0;
|
|
|
|
|
_zipClient.ExtractAll(ms, seriesDataPath, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the series data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="seriesId">The series id.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetSeriesDataPath(IApplicationPaths appPaths, string seriesId)
|
|
|
|
|
{
|
|
|
|
|
var seriesDataPath = Path.Combine(GetSeriesDataPath(appPaths), seriesId);
|
2013-04-28 07:44:45 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
if (!Directory.Exists(seriesDataPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(seriesDataPath);
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
return seriesDataPath;
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the series data path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetSeriesDataPath(IApplicationPaths appPaths)
|
|
|
|
|
{
|
|
|
|
|
var dataPath = Path.Combine(appPaths.DataPath, "tvdb");
|
2013-03-08 06:08:27 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
if (!Directory.Exists(dataPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(dataPath);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
return dataPath;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// Fetches the main info.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="doc">The doc.</param>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
private void FetchMainInfo(Series series, XmlDocument doc)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-16 00:19:47 +02:00
|
|
|
|
if (!series.LockedFields.Contains(MetadataFields.Name))
|
|
|
|
|
{
|
|
|
|
|
series.Name = doc.SafeGetString("//SeriesName");
|
|
|
|
|
}
|
|
|
|
|
if (!series.LockedFields.Contains(MetadataFields.Overview))
|
|
|
|
|
{
|
|
|
|
|
series.Overview = doc.SafeGetString("//Overview");
|
|
|
|
|
}
|
2013-09-02 17:39:09 +02:00
|
|
|
|
|
|
|
|
|
var imdbId = doc.SafeGetString("//IMDB_ID");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(imdbId))
|
|
|
|
|
{
|
|
|
|
|
series.SetProviderId(MetadataProviders.Imdb, imdbId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var zap2ItId = doc.SafeGetString("//zap2it_id");
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(zap2ItId))
|
|
|
|
|
{
|
|
|
|
|
series.SetProviderId(MetadataProviders.Zap2It, zap2ItId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only fill this if it doesn't already have a value, since we get it from imdb which has better data
|
|
|
|
|
if (!series.CommunityRating.HasValue || string.IsNullOrWhiteSpace(series.GetProviderId(MetadataProviders.Imdb)))
|
|
|
|
|
{
|
|
|
|
|
series.CommunityRating = doc.SafeGetSingle("//Rating", 0, 10);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
series.AirDays = TVUtils.GetAirDays(doc.SafeGetString("//Airs_DayOfWeek"));
|
|
|
|
|
series.AirTime = doc.SafeGetString("//Airs_Time");
|
2013-05-26 08:41:37 +02:00
|
|
|
|
SeriesStatus seriesStatus;
|
2013-05-26 16:52:11 +02:00
|
|
|
|
if(Enum.TryParse(doc.SafeGetString("//Status"), true, out seriesStatus))
|
2013-05-26 08:41:37 +02:00
|
|
|
|
series.Status = seriesStatus;
|
|
|
|
|
series.PremiereDate = doc.SafeGetDateTime("//FirstAired");
|
|
|
|
|
if (series.PremiereDate.HasValue)
|
|
|
|
|
series.ProductionYear = series.PremiereDate.Value.Year;
|
2013-06-16 16:00:16 +02:00
|
|
|
|
|
|
|
|
|
series.RunTimeTicks = TimeSpan.FromMinutes(doc.SafeGetInt32("//Runtime")).Ticks;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-06-16 00:19:47 +02:00
|
|
|
|
if (!series.LockedFields.Contains(MetadataFields.Studios))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-16 00:19:47 +02:00
|
|
|
|
string s = doc.SafeGetString("//Network");
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2013-06-16 00:19:47 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(s))
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2013-06-16 00:19:47 +02:00
|
|
|
|
series.Studios.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var studio in s.Trim().Split('|'))
|
|
|
|
|
{
|
|
|
|
|
series.AddStudio(studio);
|
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
series.OfficialRating = doc.SafeGetString("//ContentRating");
|
2013-06-16 00:19:47 +02:00
|
|
|
|
if (!series.LockedFields.Contains(MetadataFields.Genres))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-16 00:19:47 +02:00
|
|
|
|
string g = doc.SafeGetString("//Genre");
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2013-06-16 00:19:47 +02:00
|
|
|
|
if (g != null)
|
|
|
|
|
{
|
|
|
|
|
string[] genres = g.Trim('|').Split('|');
|
|
|
|
|
if (g.Length > 0)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-16 00:19:47 +02:00
|
|
|
|
series.Genres.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var genre in genres)
|
|
|
|
|
{
|
|
|
|
|
series.AddGenre(genre);
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-05-26 08:41:37 +02:00
|
|
|
|
if (series.Status == SeriesStatus.Ended) {
|
|
|
|
|
|
|
|
|
|
var document = XDocument.Load(new XmlNodeReader(doc));
|
|
|
|
|
var dates = document.Descendants("Episode").Where(x => {
|
|
|
|
|
var seasonNumber = x.Element("SeasonNumber");
|
|
|
|
|
var firstAired = x.Element("FirstAired");
|
|
|
|
|
return firstAired != null && seasonNumber != null && (!string.IsNullOrEmpty(seasonNumber.Value) && seasonNumber.Value != "0") && !string.IsNullOrEmpty(firstAired.Value);
|
|
|
|
|
}).Select(x => {
|
|
|
|
|
DateTime? date = null;
|
|
|
|
|
DateTime tempDate;
|
|
|
|
|
var firstAired = x.Element("FirstAired");
|
|
|
|
|
if (firstAired != null && DateTime.TryParse(firstAired.Value, out tempDate))
|
|
|
|
|
{
|
|
|
|
|
date = tempDate;
|
|
|
|
|
}
|
|
|
|
|
return date;
|
|
|
|
|
}).ToList();
|
|
|
|
|
if(dates.Any(x=>x.HasValue))
|
|
|
|
|
series.EndDate = dates.Where(x => x.HasValue).Max();
|
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the actors.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="actorsDoc">The actors doc.</param>
|
|
|
|
|
/// <param name="seriesDoc">The seriesDoc.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private void FetchActors(Series series, XmlDocument actorsDoc, XmlDocument seriesDoc)
|
|
|
|
|
{
|
|
|
|
|
var xmlNodeList = actorsDoc.SelectNodes("Actors/Actor");
|
|
|
|
|
|
|
|
|
|
if (xmlNodeList != null)
|
|
|
|
|
{
|
|
|
|
|
series.People.Clear();
|
2013-04-28 07:44:45 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
foreach (XmlNode p in xmlNodeList)
|
|
|
|
|
{
|
|
|
|
|
string actorName = p.SafeGetString("Name");
|
|
|
|
|
string actorRole = p.SafeGetString("Role");
|
2013-07-31 14:09:23 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(actorName))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-06-11 14:18:43 +02:00
|
|
|
|
// Sometimes tvdb actors have leading spaces
|
2013-06-10 02:09:32 +02:00
|
|
|
|
series.AddPerson(new PersonInfo { Type = PersonType.Actor, Name = actorName.Trim(), Role = actorRole });
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-04-28 07:44:45 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The us culture
|
|
|
|
|
/// </summary>
|
2013-05-07 05:46:14 +02:00
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2013-02-21 02:33:05 +01: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)
|
|
|
|
|
{
|
2013-05-19 21:37:52 +02:00
|
|
|
|
return item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the series.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2013-08-28 17:56:56 +02:00
|
|
|
|
private async Task<string> FindSeries(string name, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-08-28 17:56:56 +02:00
|
|
|
|
var url = string.Format(RootUrl + SeriesQuery, WebUtility.UrlEncode(name));
|
2013-02-21 02:33:05 +01:00
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
|
2013-05-19 17:56:03 +02:00
|
|
|
|
using (var results = await HttpClient.Get(new HttpRequestOptions
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-05-19 17:56:03 +02:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
2013-05-21 06:04:38 +02:00
|
|
|
|
CancellationToken = cancellationToken
|
2013-05-06 21:31:57 +02:00
|
|
|
|
|
2013-05-19 17:56:03 +02:00
|
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-05-19 17:56:03 +02:00
|
|
|
|
doc.Load(results);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.HasChildNodes)
|
|
|
|
|
{
|
2013-08-28 17:56:56 +02:00
|
|
|
|
var nodes = doc.SelectNodes("//Series");
|
|
|
|
|
var comparableName = GetComparableName(name);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
if (nodes != null)
|
|
|
|
|
foreach (XmlNode node in nodes)
|
|
|
|
|
{
|
|
|
|
|
var n = node.SelectSingleNode("./SeriesName");
|
2013-08-08 14:19:11 +02:00
|
|
|
|
if (n != null && string.Equals(GetComparableName(n.InnerText), comparableName, StringComparison.OrdinalIgnoreCase))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
|
|
|
|
n = node.SelectSingleNode("./seriesid");
|
|
|
|
|
if (n != null)
|
|
|
|
|
return n.InnerText;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (n != null)
|
|
|
|
|
Logger.Info("TVDb Provider - " + n.InnerText + " did not match " + comparableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 17:56:56 +02:00
|
|
|
|
// Try stripping off the year if it was supplied
|
|
|
|
|
var parenthIndex = name.LastIndexOf('(');
|
|
|
|
|
|
|
|
|
|
if (parenthIndex != -1)
|
|
|
|
|
{
|
|
|
|
|
var newName = name.Substring(0, parenthIndex);
|
|
|
|
|
|
|
|
|
|
return await FindSeries(newName, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
Logger.Info("TVDb Provider - Could not find " + name + ". Check name on Thetvdb.org.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The remove
|
|
|
|
|
/// </summary>
|
|
|
|
|
const string remove = "\"'!`?";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The spacers
|
|
|
|
|
/// </summary>
|
|
|
|
|
const string spacers = "/,.:;\\(){}[]+-_=–*"; // (there are not actually two - in the they are different char codes)
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name of the comparable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetComparableName(string name)
|
|
|
|
|
{
|
|
|
|
|
name = name.ToLower();
|
|
|
|
|
name = name.Normalize(NormalizationForm.FormKD);
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var c in name)
|
|
|
|
|
{
|
|
|
|
|
if ((int)c >= 0x2B0 && (int)c <= 0x0333)
|
|
|
|
|
{
|
|
|
|
|
// skip char modifier and diacritics
|
|
|
|
|
}
|
|
|
|
|
else if (remove.IndexOf(c) > -1)
|
|
|
|
|
{
|
|
|
|
|
// skip chars we are removing
|
|
|
|
|
}
|
|
|
|
|
else if (spacers.IndexOf(c) > -1)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(" ");
|
|
|
|
|
}
|
|
|
|
|
else if (c == '&')
|
|
|
|
|
{
|
|
|
|
|
sb.Append(" and ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sb.Append(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
name = sb.ToString();
|
|
|
|
|
name = name.Replace(", the", "");
|
|
|
|
|
name = name.Replace("the ", " ");
|
|
|
|
|
name = name.Replace(" the ", " ");
|
|
|
|
|
|
|
|
|
|
string prevName;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
prevName = name;
|
|
|
|
|
name = name.Replace(" ", " ");
|
|
|
|
|
} while (name.Length != prevName.Length);
|
|
|
|
|
|
|
|
|
|
return name.Trim();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
2013-03-08 06:08:27 +01:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|