2013-09-15 19:33:23 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2013-10-10 16:55:07 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
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-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
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-05-21 03:16:43 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2013-03-02 17:59:15 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
2013-09-15 19:33:23 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-05-18 18:16:07 +00:00
|
|
|
|
using System.Globalization;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.IO;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
using System.Linq;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
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 RemoteSeriesProvider
|
|
|
|
|
/// </summary>
|
2013-03-08 05:08:27 +00:00
|
|
|
|
class RemoteSeriesProvider : BaseMetadataProvider, IDisposable
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-04 05:43:06 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The tv db
|
|
|
|
|
/// </summary>
|
2013-05-23 15:40:20 +00:00
|
|
|
|
internal readonly SemaphoreSlim TvDbResourcePool = new SemaphoreSlim(2, 2);
|
2013-03-04 05:43:06 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The current.</value>
|
2013-03-04 05:43:06 +00:00
|
|
|
|
internal static RemoteSeriesProvider Current { get; private set; }
|
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _zip client
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IZipClient _zipClient;
|
|
|
|
|
|
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-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
|
2013-03-04 05:43:06 +00: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 03:16:43 +00:00
|
|
|
|
/// <param name="zipClient">The zip client.</param>
|
2013-03-04 05:43:06 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">httpClient</exception>
|
2013-10-31 14:03:23 +00:00
|
|
|
|
public RemoteSeriesProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IZipClient zipClient, IFileSystem fileSystem)
|
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;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
_zipClient = zipClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-03-04 05:43:06 +00: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 05:08:27 +00:00
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2013-03-04 05:43:06 +00:00
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
TvDbResourcePool.Dispose();
|
|
|
|
|
}
|
2013-02-25 00:13:45 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The root URL
|
|
|
|
|
/// </summary>
|
2013-05-19 19:37:52 +00:00
|
|
|
|
private const string RootUrl = "http://www.thetvdb.com/api/";
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The series query
|
|
|
|
|
/// </summary>
|
2013-05-19 19:37:52 +00:00
|
|
|
|
private const string SeriesQuery = "GetSeries.php?seriesname={0}";
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// The series get zip
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-05-21 03:16:43 +00:00
|
|
|
|
private const string SeriesGetZip = "http://www.thetvdb.com/api/{0}/series/{1}/all/{2}.zip";
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The LOCA l_ MET a_ FIL e_ NAME
|
|
|
|
|
/// </summary>
|
2013-07-19 18:37:26 +00:00
|
|
|
|
protected const string LocalMetaFileName = "series.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 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 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 19:33:23 +00:00
|
|
|
|
return "2";
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-23 14:02:56 +00:00
|
|
|
|
public override bool EnforceDontFetchMetadata
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Other providers depend on the xml downloaded here
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 16:55:07 +00:00
|
|
|
|
protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-05-25 15:16:50 +00:00
|
|
|
|
var seriesId = item.GetProviderId(MetadataProviders.Tvdb);
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
// Process images
|
|
|
|
|
var path = GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId);
|
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var files = new DirectoryInfo(path)
|
|
|
|
|
.EnumerateFiles("*.xml", SearchOption.TopDirectoryOnly)
|
2013-10-31 14:03:23 +00:00
|
|
|
|
.Select(i => _fileSystem.GetLastWriteTimeUtc(i))
|
2013-10-01 18:24:27 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (files.Count > 0)
|
|
|
|
|
{
|
2013-10-10 16:55:07 +00:00
|
|
|
|
return files.Max() > providerInfo.LastRefreshed;
|
2013-10-01 18:24:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-10-01 18:24:27 +00:00
|
|
|
|
// Don't blow up
|
2013-10-10 16:55:07 +00:00
|
|
|
|
return true;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-12 01:47:44 +00:00
|
|
|
|
|
2013-10-10 16:55:07 +00:00
|
|
|
|
return base.NeedsRefreshBasedOnCompareDate(item, providerInfo);
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-05-25 15:16:50 +00:00
|
|
|
|
|
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-05-21 03:16:43 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var series = (Series)item;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
|
|
|
|
var seriesId = series.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(seriesId))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-08-28 15:56:56 +00:00
|
|
|
|
seriesId = await FindSeries(series.Name, cancellationToken).ConfigureAwait(false);
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-05-18 18:16:07 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(seriesId))
|
|
|
|
|
{
|
|
|
|
|
var seriesDataPath = GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId);
|
2013-05-18 18:16:07 +00:00
|
|
|
|
|
2013-06-23 15:17:42 +00:00
|
|
|
|
await FetchSeriesData(series, seriesId, seriesDataPath, force, cancellationToken).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 15:29:10 +00:00
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
2013-05-21 03:16:43 +00:00
|
|
|
|
return true;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the series data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="seriesId">The series id.</param>
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <param name="seriesDataPath">The series data path.</param>
|
2013-06-23 15:17:42 +00:00
|
|
|
|
/// <param name="isForcedRefresh">if set to <c>true</c> [is forced refresh].</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2013-06-23 15:17:42 +00:00
|
|
|
|
private async Task FetchSeriesData(Series series, string seriesId, string seriesDataPath, bool isForcedRefresh, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-04 15:22:03 +00:00
|
|
|
|
Directory.CreateDirectory(seriesDataPath);
|
|
|
|
|
|
|
|
|
|
var files = Directory.EnumerateFiles(seriesDataPath, "*.xml", SearchOption.TopDirectoryOnly)
|
|
|
|
|
.Select(Path.GetFileName)
|
|
|
|
|
.ToList();
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2013-10-14 15:08:33 +00:00
|
|
|
|
await DownloadSeriesZip(seriesId, seriesDataPath, null, cancellationToken).ConfigureAwait(false);
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 01:44:23 +00:00
|
|
|
|
// Have to check this here since we prevent the normal enforcement through ProviderManager
|
|
|
|
|
if (!series.DontFetchMeta)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-16 01:44:23 +00:00
|
|
|
|
// Examine if there's no local metadata, or save local is on (to get updates)
|
|
|
|
|
if (isForcedRefresh || ConfigurationManager.Configuration.EnableTvDbUpdates || !HasLocalMeta(series))
|
|
|
|
|
{
|
|
|
|
|
series.SetProviderId(MetadataProviders.Tvdb, seriesId);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-10-16 01:44:23 +00:00
|
|
|
|
var seriesXmlPath = Path.Combine(seriesDataPath, seriesXmlFilename);
|
|
|
|
|
var actorsXmlPath = Path.Combine(seriesDataPath, "actors.xml");
|
2013-06-27 23:01:03 +00:00
|
|
|
|
|
2013-10-16 01:44:23 +00:00
|
|
|
|
FetchSeriesInfo(series, seriesXmlPath, cancellationToken);
|
2013-09-16 17:52:14 +00:00
|
|
|
|
|
2013-10-16 01:44:23 +00:00
|
|
|
|
if (!series.LockedFields.Contains(MetadataFields.Cast))
|
|
|
|
|
{
|
|
|
|
|
series.People.Clear();
|
|
|
|
|
|
|
|
|
|
FetchActors(series, actorsXmlPath, cancellationToken);
|
|
|
|
|
}
|
2013-06-15 22:19:47 +00:00
|
|
|
|
}
|
2013-05-21 03:16:43 +00: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>
|
2013-10-14 15:08:33 +00:00
|
|
|
|
internal async Task DownloadSeriesZip(string seriesId, string seriesDataPath, long? lastTvDbUpdateTime, CancellationToken cancellationToken)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
|
|
|
|
var url = string.Format(SeriesGetZip, TVUtils.TvdbApiKey, seriesId, ConfigurationManager.Configuration.PreferredMetadataLanguage);
|
2013-05-24 15:29:10 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
using (var zipStream = await HttpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
2013-10-24 17:49:24 +00:00
|
|
|
|
// Delete existing files
|
|
|
|
|
DeleteXmlFiles(seriesDataPath);
|
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
// Copy to memory stream because we need a seekable stream
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
await zipStream.CopyToAsync(ms).ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
ms.Position = 0;
|
|
|
|
|
_zipClient.ExtractAll(ms, seriesDataPath, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-10 16:55:07 +00:00
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
// Sanitize all files, except for extracted episode files
|
|
|
|
|
foreach (var file in Directory.EnumerateFiles(seriesDataPath, "*.xml", SearchOption.AllDirectories).ToList()
|
|
|
|
|
.Where(i => !Path.GetFileName(i).StartsWith("episode-", StringComparison.OrdinalIgnoreCase)))
|
2013-10-10 16:55:07 +00:00
|
|
|
|
{
|
|
|
|
|
await SanitizeXmlFile(file).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-10-12 01:47:44 +00:00
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
await ExtractEpisodes(seriesDataPath, Path.Combine(seriesDataPath, ConfigurationManager.Configuration.PreferredMetadataLanguage + ".xml"), lastTvDbUpdateTime).ConfigureAwait(false);
|
2013-10-10 16:55:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 17:49:24 +00:00
|
|
|
|
private void DeleteXmlFiles(string path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
foreach (var file in new DirectoryInfo(path)
|
|
|
|
|
.EnumerateFiles("*.xml", SearchOption.AllDirectories)
|
|
|
|
|
.ToList())
|
|
|
|
|
{
|
|
|
|
|
file.Delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
// No biggie
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-10 16:55:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sanitizes the XML file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task SanitizeXmlFile(string file)
|
|
|
|
|
{
|
|
|
|
|
string validXml;
|
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
2013-10-10 16:55:07 +00:00
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(fileStream))
|
|
|
|
|
{
|
|
|
|
|
var xml = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
validXml = StripInvalidXmlCharacters(xml);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
2013-10-10 16:55:07 +00:00
|
|
|
|
{
|
|
|
|
|
using (var writer = new StreamWriter(fileStream))
|
|
|
|
|
{
|
|
|
|
|
await writer.WriteAsync(validXml).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Strips the invalid XML characters.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inString">The in string.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
public static string StripInvalidXmlCharacters(string inString)
|
|
|
|
|
{
|
|
|
|
|
if (inString == null) return null;
|
|
|
|
|
|
|
|
|
|
var sbOutput = new StringBuilder();
|
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < inString.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
ch = inString[i];
|
|
|
|
|
if ((ch >= 0x0020 && ch <= 0xD7FF) ||
|
|
|
|
|
(ch >= 0xE000 && ch <= 0xFFFD) ||
|
|
|
|
|
ch == 0x0009 ||
|
|
|
|
|
ch == 0x000A ||
|
|
|
|
|
ch == 0x000D)
|
|
|
|
|
{
|
|
|
|
|
sbOutput.Append(ch);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sbOutput.ToString();
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-10-12 01:47:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extracts info for each episode into invididual xml files so that they can be easily accessed without having to step through the entire series xml
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="seriesDataPath">The series data path.</param>
|
|
|
|
|
/// <param name="xmlFile">The XML file.</param>
|
2013-10-14 15:08:33 +00:00
|
|
|
|
/// <param name="lastTvDbUpdateTime">The last tv db update time.</param>
|
2013-10-12 01:47:44 +00:00
|
|
|
|
/// <returns>Task.</returns>
|
2013-10-14 15:08:33 +00:00
|
|
|
|
private async Task ExtractEpisodes(string seriesDataPath, string xmlFile, long? lastTvDbUpdateTime)
|
2013-10-12 01:47:44 +00:00
|
|
|
|
{
|
|
|
|
|
var settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StreamReader(xmlFile, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Episode":
|
|
|
|
|
{
|
|
|
|
|
var outerXml = reader.ReadOuterXml();
|
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
await SaveEpsiodeXml(seriesDataPath, outerXml, lastTvDbUpdateTime).ConfigureAwait(false);
|
2013-10-12 01:47:44 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
private async Task SaveEpsiodeXml(string seriesDataPath, string xml, long? lastTvDbUpdateTime)
|
2013-10-12 01:47:44 +00:00
|
|
|
|
{
|
|
|
|
|
var settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var seasonNumber = -1;
|
|
|
|
|
var episodeNumber = -1;
|
|
|
|
|
var absoluteNumber = -1;
|
2013-10-14 15:08:33 +00:00
|
|
|
|
var lastUpdateString = string.Empty;
|
2013-10-12 01:47:44 +00:00
|
|
|
|
|
|
|
|
|
using (var streamReader = new StringReader(xml))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
2013-10-14 15:08:33 +00:00
|
|
|
|
case "lastupdated":
|
|
|
|
|
{
|
|
|
|
|
lastUpdateString = reader.ReadElementContentAsString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-12 01:47:44 +00:00
|
|
|
|
case "EpisodeNumber":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int num;
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
|
|
|
|
|
{
|
|
|
|
|
episodeNumber = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "absolute_number":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int num;
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
|
|
|
|
|
{
|
|
|
|
|
absoluteNumber = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "SeasonNumber":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int num;
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out num))
|
|
|
|
|
{
|
|
|
|
|
seasonNumber = num;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
var hasEpisodeChanged = true;
|
|
|
|
|
if (!string.IsNullOrEmpty(lastUpdateString) && lastTvDbUpdateTime.HasValue)
|
2013-10-12 01:47:44 +00:00
|
|
|
|
{
|
2013-10-14 15:08:33 +00:00
|
|
|
|
long num;
|
|
|
|
|
if (long.TryParse(lastUpdateString, NumberStyles.Any, UsCulture, out num))
|
|
|
|
|
{
|
|
|
|
|
hasEpisodeChanged = num >= lastTvDbUpdateTime.Value;
|
|
|
|
|
}
|
2013-10-12 01:47:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
var file = Path.Combine(seriesDataPath, string.Format("episode-{0}-{1}.xml", seasonNumber, episodeNumber));
|
2013-10-12 01:47:44 +00:00
|
|
|
|
|
2013-10-14 15:08:33 +00:00
|
|
|
|
// Only save the file if not already there, or if the episode has changed
|
|
|
|
|
if (hasEpisodeChanged || !File.Exists(file))
|
|
|
|
|
{
|
2013-10-12 01:47:44 +00:00
|
|
|
|
using (var writer = XmlWriter.Create(file, new XmlWriterSettings
|
|
|
|
|
{
|
|
|
|
|
Encoding = Encoding.UTF8,
|
|
|
|
|
Async = true
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
await writer.WriteRawAsync(xml).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-14 15:08:33 +00:00
|
|
|
|
|
|
|
|
|
if (absoluteNumber != -1)
|
|
|
|
|
{
|
|
|
|
|
file = Path.Combine(seriesDataPath, string.Format("episode-abs-{0}.xml", absoluteNumber));
|
|
|
|
|
|
|
|
|
|
// Only save the file if not already there, or if the episode has changed
|
|
|
|
|
if (hasEpisodeChanged || !File.Exists(file))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = XmlWriter.Create(file, new XmlWriterSettings
|
|
|
|
|
{
|
|
|
|
|
Encoding = Encoding.UTF8,
|
|
|
|
|
Async = true
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
await writer.WriteRawAsync(xml).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-12 01:47:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 03:16:43 +00: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 05:44:45 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
return seriesDataPath;
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00: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)
|
|
|
|
|
{
|
2013-10-12 01:47:44 +00:00
|
|
|
|
var dataPath = Path.Combine(appPaths.DataPath, "tvdb-v3");
|
2013-03-08 05:08:27 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
return dataPath;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
private void FetchSeriesInfo(Series item, string seriesXmlPath, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
var settings = new XmlReaderSettings
|
2013-06-15 22:19:47 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
2013-09-02 15:39:09 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
var episiodeAirDates = new List<DateTime>();
|
2013-09-02 15:39:09 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
using (var streamReader = new StreamReader(seriesXmlPath, Encoding.UTF8))
|
2013-09-02 15:39:09 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
2013-09-02 15:39:09 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-09-02 15:39:09 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Series":
|
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
FetchDataFromSeriesNode(item, subtree, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-09-02 15:39:09 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
case "Episode":
|
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
var date = GetFirstAiredDateFromEpisodeNode(subtree, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (date.HasValue)
|
|
|
|
|
{
|
|
|
|
|
episiodeAirDates.Add(date.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-16 14:00:16 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
if (item.Status.HasValue && item.Status.Value == SeriesStatus.Ended && episiodeAirDates.Count > 0)
|
2013-09-13 15:26:43 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
item.EndDate = episiodeAirDates.Max();
|
2013-09-13 15:26:43 +00:00
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
private void FetchDataFromSeriesNode(Series item, XmlReader reader, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
switch (reader.Name)
|
2013-06-15 22:19:47 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
case "SeriesName":
|
|
|
|
|
{
|
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Name))
|
|
|
|
|
{
|
|
|
|
|
item.Name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Overview":
|
|
|
|
|
{
|
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Overview))
|
|
|
|
|
{
|
|
|
|
|
item.Overview = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Airs_DayOfWeek":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
item.AirDays = TVUtils.GetAirDays(val);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Airs_Time":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
item.AirTime = val;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "ContentRating":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
2013-09-23 14:02:56 +00:00
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.OfficialRating))
|
|
|
|
|
{
|
|
|
|
|
item.OfficialRating = val;
|
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Rating":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
// Only fill this if it doesn't already have a value, since we get it from imdb which has better data
|
|
|
|
|
if (!item.CommunityRating.HasValue || string.IsNullOrWhiteSpace(item.GetProviderId(MetadataProviders.Imdb)))
|
|
|
|
|
{
|
|
|
|
|
float rval;
|
|
|
|
|
|
|
|
|
|
// float.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (float.TryParse(val, NumberStyles.AllowDecimalPoint, UsCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
item.CommunityRating = rval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-10-12 01:47:44 +00:00
|
|
|
|
case "RatingCount":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
item.VoteCount = rval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
|
|
|
|
|
case "IMDB_ID":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
item.SetProviderId(MetadataProviders.Imdb, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "zap2it_id":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
item.SetProviderId(MetadataProviders.Zap2It, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Status":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
SeriesStatus seriesStatus;
|
|
|
|
|
|
|
|
|
|
if (Enum.TryParse(val, true, out seriesStatus))
|
|
|
|
|
item.Status = seriesStatus;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "FirstAired":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
DateTime date;
|
|
|
|
|
if (DateTime.TryParse(val, out date))
|
|
|
|
|
{
|
|
|
|
|
date = date.ToUniversalTime();
|
|
|
|
|
|
|
|
|
|
item.PremiereDate = date;
|
|
|
|
|
item.ProductionYear = date.Year;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Runtime":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val) && !item.LockedFields.Contains(MetadataFields.Runtime))
|
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
item.RunTimeTicks = TimeSpan.FromMinutes(rval).Ticks;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Genre":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
// Only fill this in if there's no existing genres, because Imdb data from Omdb is preferred
|
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Genres) && (item.Genres.Count == 0 || !string.Equals(ConfigurationManager.Configuration.PreferredMetadataLanguage, "en", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
var vals = val
|
|
|
|
|
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(i => i.Trim())
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2013-09-20 00:53:18 +00:00
|
|
|
|
.ToList();
|
2013-09-15 18:16:28 +00:00
|
|
|
|
|
2013-09-20 00:53:18 +00:00
|
|
|
|
if (vals.Count > 0)
|
2013-09-15 18:16:28 +00:00
|
|
|
|
{
|
|
|
|
|
item.Genres.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var genre in vals)
|
|
|
|
|
{
|
|
|
|
|
item.AddGenre(genre);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Network":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
if (!item.LockedFields.Contains(MetadataFields.Studios))
|
|
|
|
|
{
|
|
|
|
|
var vals = val
|
|
|
|
|
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(i => i.Trim())
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
2013-09-20 00:53:18 +00:00
|
|
|
|
.ToList();
|
2013-09-15 18:16:28 +00:00
|
|
|
|
|
2013-09-20 00:53:18 +00:00
|
|
|
|
if (vals.Count > 0)
|
2013-09-15 18:16:28 +00:00
|
|
|
|
{
|
|
|
|
|
item.Studios.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var genre in vals)
|
|
|
|
|
{
|
|
|
|
|
item.AddStudio(genre);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
2013-06-15 22:19:47 +00:00
|
|
|
|
}
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
private DateTime? GetFirstAiredDateFromEpisodeNode(XmlReader reader, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
DateTime? airDate = null;
|
|
|
|
|
int? seasonNumber = null;
|
2013-09-15 17:34:48 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-06-15 22:19:47 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
switch (reader.Name)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-09-15 18:16:28 +00:00
|
|
|
|
case "FirstAired":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
2013-06-15 22:19:47 +00:00
|
|
|
|
|
2013-09-15 18:16:28 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
DateTime date;
|
|
|
|
|
if (DateTime.TryParse(val, out date))
|
|
|
|
|
{
|
|
|
|
|
airDate = date.ToUniversalTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "SeasonNumber":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, UsCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
seasonNumber = rval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
|
|
|
|
|
if (seasonNumber.HasValue && seasonNumber.Value != 0)
|
|
|
|
|
{
|
|
|
|
|
return airDate;
|
2013-05-26 06:41:37 +00:00
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
2013-05-21 03:16:43 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the actors.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
2013-09-15 17:34:48 +00:00
|
|
|
|
/// <param name="actorsXmlPath">The actors XML path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
private void FetchActors(Series series, string actorsXmlPath, CancellationToken cancellationToken)
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
var settings = new XmlReaderSettings
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
2013-04-28 05:44:45 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
using (var streamReader = new StreamReader(actorsXmlPath, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
2013-05-21 03:16:43 +00:00
|
|
|
|
{
|
2013-09-15 17:34:48 +00:00
|
|
|
|
reader.MoveToContent();
|
2013-07-31 12:09:23 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Actor":
|
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
FetchDataFromActorNode(series, subtree);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-04-28 05:44:45 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the data from actor node.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="reader">The reader.</param>
|
|
|
|
|
private void FetchDataFromActorNode(Series series, XmlReader reader)
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
var personInfo = new PersonInfo();
|
|
|
|
|
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "Name":
|
|
|
|
|
{
|
|
|
|
|
personInfo.Name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Role":
|
|
|
|
|
{
|
|
|
|
|
personInfo.Role = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-09-15 18:16:28 +00:00
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-16 17:52:14 +00:00
|
|
|
|
personInfo.Type = PersonType.Actor;
|
|
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(personInfo.Name))
|
|
|
|
|
{
|
|
|
|
|
series.AddPerson(personInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The us culture
|
|
|
|
|
/// </summary>
|
2013-05-07 03:46:14 +00:00
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
2013-05-21 03:16:43 +00:00
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-05-19 19:37:52 +00:00
|
|
|
|
return item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName);
|
2013-02-21 01:33:05 +00: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 15:56:56 +00:00
|
|
|
|
private async Task<string> FindSeries(string name, CancellationToken cancellationToken)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-08-28 15:56:56 +00:00
|
|
|
|
var url = string.Format(RootUrl + SeriesQuery, WebUtility.UrlEncode(name));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
|
2013-05-19 15:56:03 +00:00
|
|
|
|
using (var results = await HttpClient.Get(new HttpRequestOptions
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-19 15:56:03 +00:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
2013-05-21 04:04:38 +00:00
|
|
|
|
CancellationToken = cancellationToken
|
2013-05-06 19:31:57 +00:00
|
|
|
|
|
2013-05-19 15:56:03 +00:00
|
|
|
|
}).ConfigureAwait(false))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-05-19 15:56:03 +00:00
|
|
|
|
doc.Load(results);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (doc.HasChildNodes)
|
|
|
|
|
{
|
2013-08-28 15:56:56 +00:00
|
|
|
|
var nodes = doc.SelectNodes("//Series");
|
|
|
|
|
var comparableName = GetComparableName(name);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
if (nodes != null)
|
2013-10-28 13:59:13 +00:00
|
|
|
|
{
|
2013-02-21 01:33:05 +00:00
|
|
|
|
foreach (XmlNode node in nodes)
|
|
|
|
|
{
|
2013-10-28 13:59:13 +00:00
|
|
|
|
var titles = new List<string>();
|
|
|
|
|
|
|
|
|
|
var nameNode = node.SelectSingleNode("./SeriesName");
|
|
|
|
|
if (nameNode != null)
|
|
|
|
|
{
|
|
|
|
|
titles.Add(GetComparableName(nameNode.InnerText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aliasNode = node.SelectSingleNode("./AliasNames");
|
|
|
|
|
if (aliasNode != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-28 13:59:13 +00:00
|
|
|
|
var alias = aliasNode.InnerText.Split('|').Select(GetComparableName);
|
|
|
|
|
titles.AddRange(alias);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-10-28 13:59:13 +00:00
|
|
|
|
|
|
|
|
|
if (titles.Any(t => string.Equals(t, comparableName, StringComparison.OrdinalIgnoreCase)))
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-10-28 13:59:13 +00:00
|
|
|
|
var id = node.SelectSingleNode("./seriesid");
|
|
|
|
|
if (id != null)
|
|
|
|
|
return id.InnerText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var title in titles)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("TVDb Provider - " + title + " did not match " + comparableName);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-28 13:59:13 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-28 15:56:56 +00: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 01:33:05 +00: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 03:16:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
2013-03-08 05:08:27 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|