2013-09-15 21:33:23 +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;
|
2015-04-16 16:59:39 +02:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2014-02-20 05:53:15 +01:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
using System;
|
2013-09-15 21:33:23 +02:00
|
|
|
|
using System.Collections.Generic;
|
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;
|
2015-10-04 06:23:11 +02:00
|
|
|
|
using CommonIO;
|
2016-10-06 20:55:01 +02:00
|
|
|
|
using MediaBrowser.Common.IO;
|
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
|
|
|
|
{
|
2016-05-26 19:18:58 +02:00
|
|
|
|
public class TvdbSeriesProvider : IRemoteMetadataProvider<Series, SeriesInfo>, IHasOrder
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
|
private const string TvdbSeriesOffset = "TvdbSeriesOffset";
|
|
|
|
|
private const string TvdbSeriesOffsetFormat = "{0}-{1}";
|
2014-03-27 03:14:06 +01:00
|
|
|
|
|
2013-05-23 17:40:20 +02:00
|
|
|
|
internal readonly SemaphoreSlim TvDbResourcePool = new SemaphoreSlim(2, 2);
|
2013-11-04 21:49:36 +01:00
|
|
|
|
internal static TvdbSeriesProvider Current { get; private set; }
|
2013-05-21 05:16:43 +02:00
|
|
|
|
private readonly IZipClient _zipClient;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2013-10-31 15:03:23 +01:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
private readonly ILogger _logger;
|
2014-11-16 23:46:01 +01:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2016-10-06 20:55:01 +02:00
|
|
|
|
private readonly IMemoryStreamProvider _memoryStreamProvider;
|
2013-10-31 15:03:23 +01:00
|
|
|
|
|
2016-10-07 17:08:13 +02:00
|
|
|
|
public TvdbSeriesProvider(IZipClient zipClient, IHttpClient httpClient, IFileSystem fileSystem, IServerConfigurationManager config, ILogger logger, ILibraryManager libraryManager, IMemoryStreamProvider memoryStreamProvider)
|
2013-02-25 01:13:45 +01:00
|
|
|
|
{
|
2013-05-21 05:16:43 +02:00
|
|
|
|
_zipClient = zipClient;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 15:03:23 +01:00
|
|
|
|
_fileSystem = fileSystem;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
_config = config;
|
|
|
|
|
_logger = logger;
|
2014-11-16 23:46:01 +01:00
|
|
|
|
_libraryManager = libraryManager;
|
2016-10-07 17:08:13 +02:00
|
|
|
|
_memoryStreamProvider = memoryStreamProvider;
|
2013-03-04 06:43:06 +01:00
|
|
|
|
Current = this;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-21 07:13:33 +02:00
|
|
|
|
private const string SeriesSearchUrl = "https://www.thetvdb.com/api/GetSeries.php?seriesname={0}&language={1}";
|
|
|
|
|
private const string SeriesGetZip = "https://www.thetvdb.com/api/{0}/series/{1}/all/{2}.zip";
|
|
|
|
|
private const string GetSeriesByImdbId = "https://www.thetvdb.com/api/GetSeriesByRemoteID.php?imdbid={0}&language={1}";
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2016-01-25 05:12:47 +01:00
|
|
|
|
private string NormalizeLanguage(string language)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(language))
|
|
|
|
|
{
|
|
|
|
|
return language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pt-br is just pt to tvdb
|
|
|
|
|
return language.Split('-')[0].ToLower();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (IsValidSeries(searchInfo.ProviderIds))
|
2014-03-02 16:42:21 +01:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
var metadata = await GetMetadata(searchInfo, cancellationToken).ConfigureAwait(false);
|
2014-08-19 03:42:53 +02:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (metadata.HasMetadata)
|
2014-08-19 03:42:53 +02:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
return new List<RemoteSearchResult>
|
|
|
|
|
{
|
|
|
|
|
new RemoteSearchResult
|
|
|
|
|
{
|
|
|
|
|
Name = metadata.Item.Name,
|
|
|
|
|
PremiereDate = metadata.Item.PremiereDate,
|
|
|
|
|
ProductionYear = metadata.Item.ProductionYear,
|
|
|
|
|
ProviderIds = metadata.Item.ProviderIds,
|
|
|
|
|
SearchProviderName = Name
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-08-19 03:42:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
return await FindSeries(searchInfo.Name, searchInfo.Year, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2014-02-20 05:53:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 04:10:13 +01:00
|
|
|
|
public async Task<MetadataResult<Series>> GetMetadata(SeriesInfo itemId, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var result = new MetadataResult<Series>();
|
2016-09-18 06:27:17 +02:00
|
|
|
|
result.QueriedById = true;
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (!IsValidSeries(itemId.ProviderIds))
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2016-09-18 06:27:17 +02:00
|
|
|
|
result.QueriedById = false;
|
2015-09-19 23:25:19 +02:00
|
|
|
|
await Identify(itemId).ConfigureAwait(false);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2015-10-21 07:09:50 +02:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-05-18 20:16:07 +02:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (IsValidSeries(itemId.ProviderIds))
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
await EnsureSeriesInfo(itemId.ProviderIds, itemId.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2014-02-03 21:51:28 +01:00
|
|
|
|
|
|
|
|
|
result.Item = new Series();
|
|
|
|
|
result.HasMetadata = true;
|
2013-05-18 20:16:07 +02:00
|
|
|
|
|
2015-12-02 04:10:04 +01:00
|
|
|
|
FetchSeriesData(result, itemId.MetadataLanguage, itemId.ProviderIds, cancellationToken);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
return result;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 03:14:06 +01:00
|
|
|
|
internal static int? GetSeriesOffset(Dictionary<string, string> seriesProviderIds)
|
|
|
|
|
{
|
2014-03-30 04:44:03 +02:00
|
|
|
|
string idString;
|
|
|
|
|
if (!seriesProviderIds.TryGetValue(TvdbSeriesOffset, out idString))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var parts = idString.Split('-');
|
|
|
|
|
if (parts.Length < 2)
|
2014-03-27 03:14:06 +01:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
int offset;
|
2014-03-30 04:44:03 +02:00
|
|
|
|
if (int.TryParse(parts[1], out offset))
|
2014-03-27 03:14:06 +01:00
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the series data.
|
|
|
|
|
/// </summary>
|
2015-06-29 03:10:45 +02:00
|
|
|
|
/// <param name="result">The result.</param>
|
2015-12-02 04:10:04 +01:00
|
|
|
|
/// <param name="metadataLanguage">The metadata language.</param>
|
2015-11-29 21:01:14 +01:00
|
|
|
|
/// <param name="seriesProviderIds">The series provider ids.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
2015-12-02 04:10:04 +01:00
|
|
|
|
private void FetchSeriesData(MetadataResult<Series> result, string metadataLanguage, Dictionary<string, string> seriesProviderIds, CancellationToken cancellationToken)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-06-29 03:10:45 +02:00
|
|
|
|
var series = result.Item;
|
|
|
|
|
|
2015-11-29 21:01:14 +01:00
|
|
|
|
string id;
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out id) && !string.IsNullOrEmpty(id))
|
2015-11-29 21:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
series.SetProviderId(MetadataProviders.Tvdb, id);
|
|
|
|
|
}
|
2013-10-04 17:22:03 +02:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out id) && !string.IsNullOrEmpty(id))
|
2015-11-29 21:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
series.SetProviderId(MetadataProviders.Imdb, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var seriesDataPath = GetSeriesDataPath(_config.ApplicationPaths, seriesProviderIds);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
var seriesXmlPath = GetSeriesXmlPath(seriesProviderIds, metadataLanguage);
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var actorsXmlPath = Path.Combine(seriesDataPath, "actors.xml");
|
2013-06-28 01:01:03 +02:00
|
|
|
|
|
2016-05-14 03:55:46 +02:00
|
|
|
|
FetchSeriesInfo(result, seriesXmlPath, cancellationToken);
|
2013-09-16 19:52:14 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-10-16 03:44:23 +02:00
|
|
|
|
|
2015-07-24 04:48:10 +02:00
|
|
|
|
result.ResetPeople();
|
2015-10-21 07:09:50 +02:00
|
|
|
|
|
2015-06-29 03:10:45 +02:00
|
|
|
|
FetchActors(result, actorsXmlPath);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads the series zip.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="seriesId">The series id.</param>
|
2015-11-29 21:01:14 +01:00
|
|
|
|
/// <param name="idType">Type of the identifier.</param>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <param name="seriesDataPath">The series data path.</param>
|
2013-12-27 01:23:58 +01:00
|
|
|
|
/// <param name="lastTvDbUpdateTime">The last tv database update time.</param>
|
|
|
|
|
/// <param name="preferredMetadataLanguage">The preferred metadata language.</param>
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2015-11-29 21:01:14 +01:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">seriesId</exception>
|
|
|
|
|
internal async Task DownloadSeriesZip(string seriesId, string idType, string seriesDataPath, long? lastTvDbUpdateTime, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
2015-04-16 16:59:39 +02:00
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(seriesId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("seriesId");
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 16:59:39 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
await DownloadSeriesZip(seriesId, idType, seriesDataPath, lastTvDbUpdateTime, preferredMetadataLanguage, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2015-04-16 16:59:39 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException ex)
|
|
|
|
|
{
|
|
|
|
|
if (!ex.StatusCode.HasValue || ex.StatusCode.Value != HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.Equals(preferredMetadataLanguage, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
await DownloadSeriesZip(seriesId, idType, seriesDataPath, lastTvDbUpdateTime, "en", preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2015-04-16 16:59:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-29 21:01:14 +01:00
|
|
|
|
private async Task DownloadSeriesZip(string seriesId, string idType, string seriesDataPath, long? lastTvDbUpdateTime, string preferredMetadataLanguage, string saveAsMetadataLanguage, CancellationToken cancellationToken)
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(seriesId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("seriesId");
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 17:43:42 +01:00
|
|
|
|
if (!string.Equals(idType, "tvdb", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
seriesId = await GetSeriesByRemoteId(seriesId, idType, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-16 20:02:25 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(seriesId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("seriesId");
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-26 20:06:02 +01:00
|
|
|
|
var url = string.Format(SeriesGetZip, TVUtils.TvdbApiKey, seriesId, NormalizeLanguage(preferredMetadataLanguage));
|
2013-05-24 17:29:10 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var zipStream = await _httpClient.Get(new HttpRequestOptions
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
2013-10-24 19:49:24 +02:00
|
|
|
|
// Delete existing files
|
|
|
|
|
DeleteXmlFiles(seriesDataPath);
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
// Copy to memory stream because we need a seekable stream
|
2016-10-06 20:55:01 +02:00
|
|
|
|
using (var ms = _memoryStreamProvider.CreateNew())
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
|
|
|
|
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;
|
2014-12-29 03:50:02 +01:00
|
|
|
|
_zipClient.ExtractAllFromZip(ms, seriesDataPath, true);
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-10 18:55:07 +02:00
|
|
|
|
|
2013-10-14 17:08:33 +02: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 18:55:07 +02:00
|
|
|
|
{
|
|
|
|
|
await SanitizeXmlFile(file).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2016-01-26 20:06:02 +01:00
|
|
|
|
var downloadLangaugeXmlFile = Path.Combine(seriesDataPath, NormalizeLanguage(preferredMetadataLanguage) + ".xml");
|
2015-04-19 05:35:17 +02:00
|
|
|
|
var saveAsLanguageXmlFile = Path.Combine(seriesDataPath, saveAsMetadataLanguage + ".xml");
|
|
|
|
|
|
|
|
|
|
if (!string.Equals(downloadLangaugeXmlFile, saveAsLanguageXmlFile, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
_fileSystem.CopyFile(downloadLangaugeXmlFile, saveAsLanguageXmlFile, true);
|
2015-04-19 05:35:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await ExtractEpisodes(seriesDataPath, downloadLangaugeXmlFile, lastTvDbUpdateTime).ConfigureAwait(false);
|
2013-10-10 18:55:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 17:43:42 +01:00
|
|
|
|
private async Task<string> GetSeriesByRemoteId(string id, string idType, string language, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-01-25 05:12:47 +01:00
|
|
|
|
var url = string.Format(GetSeriesByImdbId, id, NormalizeLanguage(language));
|
2015-12-18 17:43:42 +01:00
|
|
|
|
|
|
|
|
|
using (var result = await _httpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
doc.Load(result);
|
|
|
|
|
|
|
|
|
|
if (doc.HasChildNodes)
|
|
|
|
|
{
|
|
|
|
|
var node = doc.SelectSingleNode("//Series/seriesid");
|
|
|
|
|
|
|
|
|
|
if (node != null)
|
|
|
|
|
{
|
|
|
|
|
var idResult = node.InnerText;
|
|
|
|
|
|
|
|
|
|
_logger.Info("Tvdb GetSeriesByRemoteId produced id of {0}", idResult ?? string.Empty);
|
|
|
|
|
|
|
|
|
|
return idResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
internal static bool IsValidSeries(Dictionary<string, string> seriesProviderIds)
|
2014-02-03 06:35:43 +01:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
string id;
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out id) && !string.IsNullOrEmpty(id))
|
2015-10-21 07:09:50 +02:00
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
// This check should ideally never be necessary but we're seeing some cases of this and haven't tracked them down yet.
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out id) && !string.IsNullOrEmpty(id))
|
2015-11-29 21:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
// This check should ideally never be necessary but we're seeing some cases of this and haven't tracked them down yet.
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-10-21 07:09:50 +02:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-03 06:35:43 +01:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
private SemaphoreSlim _ensureSemaphore = new SemaphoreSlim(1, 1);
|
2015-11-29 21:01:14 +01:00
|
|
|
|
internal async Task<string> EnsureSeriesInfo(Dictionary<string, string> seriesProviderIds, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
2015-10-21 07:09:50 +02:00
|
|
|
|
{
|
2016-01-17 04:24:12 +01:00
|
|
|
|
await _ensureSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
2015-11-29 21:01:14 +01:00
|
|
|
|
|
2016-01-17 04:24:12 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string seriesId;
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out seriesId) && !string.IsNullOrEmpty(seriesId))
|
2015-11-29 21:01:14 +01:00
|
|
|
|
{
|
2016-01-17 04:24:12 +01:00
|
|
|
|
var seriesDataPath = GetSeriesDataPath(_config.ApplicationPaths, seriesProviderIds);
|
2015-11-29 21:01:14 +01:00
|
|
|
|
|
2016-01-17 04:24:12 +01:00
|
|
|
|
// Only download if not already there
|
|
|
|
|
// The post-scan task will take care of updates so we don't need to re-download here
|
|
|
|
|
if (!IsCacheValid(seriesDataPath, preferredMetadataLanguage))
|
|
|
|
|
{
|
|
|
|
|
await DownloadSeriesZip(seriesId, MetadataProviders.Tvdb.ToString(), seriesDataPath, null, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2015-11-29 21:01:14 +01:00
|
|
|
|
|
2016-01-17 04:24:12 +01:00
|
|
|
|
return seriesDataPath;
|
|
|
|
|
}
|
2014-02-03 06:35:43 +01:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out seriesId) && !string.IsNullOrEmpty(seriesId))
|
2015-10-21 07:09:50 +02:00
|
|
|
|
{
|
2016-01-17 04:24:12 +01:00
|
|
|
|
var seriesDataPath = GetSeriesDataPath(_config.ApplicationPaths, seriesProviderIds);
|
|
|
|
|
|
|
|
|
|
// Only download if not already there
|
|
|
|
|
// The post-scan task will take care of updates so we don't need to re-download here
|
|
|
|
|
if (!IsCacheValid(seriesDataPath, preferredMetadataLanguage))
|
|
|
|
|
{
|
|
|
|
|
await DownloadSeriesZip(seriesId, MetadataProviders.Imdb.ToString(), seriesDataPath, null, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return seriesDataPath;
|
2015-10-21 07:09:50 +02:00
|
|
|
|
}
|
2014-02-03 06:35:43 +01:00
|
|
|
|
|
2016-01-17 04:24:12 +01:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_ensureSemaphore.Release();
|
2015-10-21 07:09:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-25 16:40:16 +01:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
private bool IsCacheValid(string seriesDataPath, string preferredMetadataLanguage)
|
|
|
|
|
{
|
|
|
|
|
try
|
2014-02-04 05:04:19 +01:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
var files = _fileSystem.GetFiles(seriesDataPath)
|
|
|
|
|
.ToList();
|
2014-02-04 05:04:19 +01:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
var seriesXmlFilename = preferredMetadataLanguage + ".xml";
|
|
|
|
|
|
2015-11-29 21:01:14 +01:00
|
|
|
|
const int cacheDays = 1;
|
2015-10-21 07:09:50 +02:00
|
|
|
|
|
|
|
|
|
var seriesFile = files.FirstOrDefault(i => string.Equals(seriesXmlFilename, i.Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
// No need to check age if automatic updates are enabled
|
2016-05-25 02:42:12 +02:00
|
|
|
|
if (seriesFile == null || !seriesFile.Exists || (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(seriesFile)).TotalDays > cacheDays)
|
2015-10-21 07:09:50 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-02-04 05:04:19 +01:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
var actorsXml = files.FirstOrDefault(i => string.Equals("actors.xml", i.Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
// No need to check age if automatic updates are enabled
|
2016-05-25 02:42:12 +02:00
|
|
|
|
if (actorsXml == null || !actorsXml.Exists || (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(actorsXml)).TotalDays > cacheDays)
|
2015-10-21 07:09:50 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bannersXml = files.FirstOrDefault(i => string.Equals("banners.xml", i.Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
// No need to check age if automatic updates are enabled
|
2016-05-25 02:42:12 +02:00
|
|
|
|
if (bannersXml == null || !bannersXml.Exists || (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(bannersXml)).TotalDays > cacheDays)
|
2015-10-21 07:09:50 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
2014-02-04 05:04:19 +01:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
return false;
|
2014-02-04 05:04:19 +01:00
|
|
|
|
}
|
2015-10-21 07:09:50 +02:00
|
|
|
|
catch (FileNotFoundException)
|
2014-02-03 06:35:43 +01:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
return false;
|
2014-02-03 06:35:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the series.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The name.</param>
|
2015-10-21 07:09:50 +02:00
|
|
|
|
/// <param name="year">The year.</param>
|
2015-02-20 19:27:01 +01:00
|
|
|
|
/// <param name="language">The language.</param>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2015-10-21 07:09:50 +02:00
|
|
|
|
private async Task<IEnumerable<RemoteSearchResult>> FindSeries(string name, int? year, string language, CancellationToken cancellationToken)
|
2014-08-19 03:42:53 +02:00
|
|
|
|
{
|
2015-02-20 19:27:01 +01:00
|
|
|
|
var results = (await FindSeriesInternal(name, language, cancellationToken).ConfigureAwait(false)).ToList();
|
2014-08-19 03:42:53 +02:00
|
|
|
|
|
|
|
|
|
if (results.Count == 0)
|
|
|
|
|
{
|
2014-11-16 23:46:01 +01:00
|
|
|
|
var parsedName = _libraryManager.ParseName(name);
|
|
|
|
|
var nameWithoutYear = parsedName.Name;
|
2014-08-19 03:42:53 +02:00
|
|
|
|
|
2015-02-20 19:27:01 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(nameWithoutYear) && !string.Equals(nameWithoutYear, name, StringComparison.OrdinalIgnoreCase))
|
2014-08-19 03:42:53 +02:00
|
|
|
|
{
|
2015-02-20 19:27:01 +01:00
|
|
|
|
results = (await FindSeriesInternal(nameWithoutYear, language, cancellationToken).ConfigureAwait(false)).ToList();
|
2014-08-19 03:42:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
return results.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
if (year.HasValue && i.ProductionYear.HasValue)
|
|
|
|
|
{
|
|
|
|
|
// Allow one year tolerance
|
|
|
|
|
return Math.Abs(year.Value - i.ProductionYear.Value) <= 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2014-08-19 03:42:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-20 19:27:01 +01:00
|
|
|
|
private async Task<IEnumerable<RemoteSearchResult>> FindSeriesInternal(string name, string language, CancellationToken cancellationToken)
|
2013-10-24 19:49:24 +02:00
|
|
|
|
{
|
2016-01-25 05:12:47 +01:00
|
|
|
|
var url = string.Format(SeriesSearchUrl, WebUtility.UrlEncode(name), NormalizeLanguage(language));
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
|
|
|
|
|
using (var results = await _httpClient.Get(new HttpRequestOptions
|
2013-10-24 19:49:24 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
2013-10-24 19:49:24 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
doc.Load(results);
|
2013-10-24 19:49:24 +02:00
|
|
|
|
}
|
2013-10-10 18:55:07 +02:00
|
|
|
|
|
2014-08-19 03:42:53 +02:00
|
|
|
|
var searchResults = new List<RemoteSearchResult>();
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (doc.HasChildNodes)
|
2013-10-10 18:55:07 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var nodes = doc.SelectNodes("//Series");
|
|
|
|
|
var comparableName = GetComparableName(name);
|
|
|
|
|
if (nodes != null)
|
2013-10-10 18:55:07 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
foreach (XmlNode node in nodes)
|
|
|
|
|
{
|
2015-01-27 07:50:40 +01:00
|
|
|
|
var searchResult = new RemoteSearchResult
|
|
|
|
|
{
|
|
|
|
|
SearchProviderName = Name
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var titles = new List<string>();
|
2013-10-10 18:55:07 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var nameNode = node.SelectSingleNode("./SeriesName");
|
|
|
|
|
if (nameNode != null)
|
|
|
|
|
{
|
|
|
|
|
titles.Add(GetComparableName(nameNode.InnerText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var aliasNode = node.SelectSingleNode("./AliasNames");
|
|
|
|
|
if (aliasNode != null)
|
|
|
|
|
{
|
|
|
|
|
var alias = aliasNode.InnerText.Split('|').Select(GetComparableName);
|
|
|
|
|
titles.AddRange(alias);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 07:50:40 +01:00
|
|
|
|
var imdbIdNode = node.SelectSingleNode("./IMDB_ID");
|
|
|
|
|
if (imdbIdNode != null)
|
|
|
|
|
{
|
|
|
|
|
var val = imdbIdNode.InnerText;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
searchResult.SetProviderId(MetadataProviders.Imdb, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bannerNode = node.SelectSingleNode("./banner");
|
|
|
|
|
if (bannerNode != null)
|
|
|
|
|
{
|
|
|
|
|
var val = bannerNode.InnerText;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
searchResult.ImageUrl = TVUtils.BannerUrl + val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-06 00:13:50 +02:00
|
|
|
|
var airDateNode = node.SelectSingleNode("./FirstAired");
|
|
|
|
|
if (airDateNode != null)
|
|
|
|
|
{
|
|
|
|
|
var val = airDateNode.InnerText;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
|
|
|
|
DateTime date;
|
|
|
|
|
if (DateTime.TryParse(val, out date))
|
|
|
|
|
{
|
|
|
|
|
searchResult.ProductionYear = date.Year;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-11 23:22:44 +02:00
|
|
|
|
foreach (var title in titles)
|
2014-02-03 21:51:28 +01:00
|
|
|
|
{
|
2015-06-11 23:22:44 +02:00
|
|
|
|
if (string.Equals(title, comparableName, StringComparison.OrdinalIgnoreCase))
|
2014-08-19 03:42:53 +02:00
|
|
|
|
{
|
2015-06-11 23:22:44 +02:00
|
|
|
|
var id = node.SelectSingleNode("./seriesid") ??
|
|
|
|
|
node.SelectSingleNode("./id");
|
|
|
|
|
|
|
|
|
|
if (id != null)
|
|
|
|
|
{
|
|
|
|
|
searchResult.Name = title;
|
|
|
|
|
searchResult.SetProviderId(MetadataProviders.Tvdb, id.InnerText);
|
|
|
|
|
searchResults.Add(searchResult);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2014-08-19 03:42:53 +02:00
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
_logger.Info("TVDb Provider - " + title + " did not match " + comparableName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-10 18:55:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-09 07:56:46 +02:00
|
|
|
|
if (searchResults.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("TVDb Provider - Could not find " + name + ". Check name on Thetvdb.org.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 03:42:53 +02:00
|
|
|
|
return searchResults;
|
2013-10-10 18:55:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// The remove
|
2013-10-10 18:55:07 +02:00
|
|
|
|
/// </summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
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>
|
2013-10-10 18:55:07 +02:00
|
|
|
|
/// <returns>System.String.</returns>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
internal static string GetComparableName(string name)
|
2013-10-10 18:55:07 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
name = name.ToLower();
|
|
|
|
|
name = name.Normalize(NormalizationForm.FormKD);
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var c in name)
|
2013-10-10 18:55:07 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if ((int)c >= 0x2B0 && (int)c <= 0x0333)
|
2013-10-10 18:55:07 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
// 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);
|
2013-10-10 18:55:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
name = sb.ToString();
|
|
|
|
|
name = name.Replace(", the", "");
|
|
|
|
|
name = name.Replace("the ", " ");
|
|
|
|
|
name = name.Replace(" the ", " ");
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
string prevName;
|
|
|
|
|
do
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
prevName = name;
|
|
|
|
|
name = name.Replace(" ", " ");
|
|
|
|
|
} while (name.Length != prevName.Length);
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
return name.Trim();
|
2013-10-12 03:47:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-14 03:55:46 +02:00
|
|
|
|
private void FetchSeriesInfo(MetadataResult<Series> result, string seriesXmlPath, CancellationToken cancellationToken)
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
|
|
|
|
var settings = new XmlReaderSettings
|
|
|
|
|
{
|
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var episiodeAirDates = new List<DateTime>();
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var streamReader = new StreamReader(seriesXmlPath, Encoding.UTF8))
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2013-10-12 03:47:44 +02:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "Series":
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2016-05-14 03:55:46 +02:00
|
|
|
|
FetchDataFromSeriesNode(result, subtree, cancellationToken);
|
2013-10-12 03:47:44 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "Episode":
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var date = GetFirstAiredDateFromEpisodeNode(subtree, cancellationToken);
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (date.HasValue)
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
episiodeAirDates.Add(date.Value);
|
2013-10-12 03:47:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-14 03:55:46 +02:00
|
|
|
|
if (result.Item.Status.HasValue && result.Item.Status.Value == SeriesStatus.Ended && episiodeAirDates.Count > 0)
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
2016-05-14 03:55:46 +02:00
|
|
|
|
result.Item.EndDate = episiodeAirDates.Max();
|
2013-10-12 03:47:44 +02:00
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
}
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
private DateTime? GetFirstAiredDateFromEpisodeNode(XmlReader reader, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
DateTime? airDate = null;
|
|
|
|
|
int? seasonNumber = null;
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
reader.MoveToContent();
|
2013-10-14 17:08:33 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2013-10-14 17:08:33 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2013-10-14 17:08:33 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-10-14 17:08:33 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
switch (reader.Name)
|
2013-10-14 17:08:33 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "FirstAired":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
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-10-14 17:08:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-12 03:47:44 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (seasonNumber.HasValue && seasonNumber.Value != 0)
|
|
|
|
|
{
|
|
|
|
|
return airDate;
|
|
|
|
|
}
|
2013-04-28 07:44:45 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
return null;
|
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>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// Fetches the actors.
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// </summary>
|
2015-06-29 03:10:45 +02:00
|
|
|
|
/// <param name="result">The result.</param>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <param name="actorsXmlPath">The actors XML path.</param>
|
2015-06-29 03:10:45 +02:00
|
|
|
|
private void FetchActors(MetadataResult<Series> result, string actorsXmlPath)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-09-15 20:16:28 +02:00
|
|
|
|
var settings = new XmlReaderSettings
|
2013-06-16 00:19:47 +02:00
|
|
|
|
{
|
2013-09-15 20:16:28 +02:00
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
2013-09-02 17:39:09 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var streamReader = new StreamReader(actorsXmlPath, Encoding.UTF8))
|
2013-09-02 17:39:09 +02:00
|
|
|
|
{
|
2013-09-15 20:16:28 +02:00
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
2013-09-02 17:39:09 +02:00
|
|
|
|
|
2013-09-15 20:16:28 +02:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "Actor":
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
|
|
|
|
using (var subtree = reader.ReadSubtree())
|
|
|
|
|
{
|
2015-06-29 03:10:45 +02:00
|
|
|
|
FetchDataFromActorNode(result, subtree);
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the data from actor node.
|
|
|
|
|
/// </summary>
|
2015-06-29 03:10:45 +02:00
|
|
|
|
/// <param name="result">The result.</param>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <param name="reader">The reader.</param>
|
2015-06-29 03:10:45 +02:00
|
|
|
|
private void FetchDataFromActorNode(MetadataResult<Series> result, XmlReader reader)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var personInfo = new PersonInfo();
|
|
|
|
|
|
2013-09-15 20:16:28 +02:00
|
|
|
|
while (reader.Read())
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-09-15 20:16:28 +02:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2013-09-15 20:16:28 +02:00
|
|
|
|
switch (reader.Name)
|
2013-06-16 00:19:47 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "Name":
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
personInfo.Name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
2013-09-15 20:16:28 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "Role":
|
|
|
|
|
{
|
|
|
|
|
personInfo.Role = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
break;
|
2016-01-21 19:50:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "id":
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Image":
|
|
|
|
|
{
|
|
|
|
|
var url = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
|
|
|
{
|
|
|
|
|
personInfo.ImageUrl = TVUtils.BannerUrl + url;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2014-02-03 21:51:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "SortOrder":
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
|
|
|
|
|
{
|
|
|
|
|
personInfo.SortOrder = rval;
|
|
|
|
|
}
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
personInfo.Type = PersonType.Actor;
|
|
|
|
|
|
2015-02-20 19:27:01 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(personInfo.Name))
|
2014-02-03 21:51:28 +01:00
|
|
|
|
{
|
2015-07-24 04:48:10 +02:00
|
|
|
|
result.AddPerson(personInfo);
|
2014-02-03 21:51:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-14 03:55:46 +02:00
|
|
|
|
private void FetchDataFromSeriesNode(MetadataResult<Series> result, XmlReader reader, CancellationToken cancellationToken)
|
2014-02-03 21:51:28 +01:00
|
|
|
|
{
|
2016-05-14 03:55:46 +02:00
|
|
|
|
Series item = result.Item;
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "SeriesName":
|
|
|
|
|
{
|
|
|
|
|
item.Name = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Overview":
|
|
|
|
|
{
|
|
|
|
|
item.Overview = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-14 03:55:46 +02:00
|
|
|
|
case "Language":
|
|
|
|
|
{
|
|
|
|
|
result.ResultLanguage = (reader.ReadElementContentAsString() ?? string.Empty).Trim();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 20:16:28 +02:00
|
|
|
|
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))
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
item.OfficialRating = val;
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Rating":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
float rval;
|
2013-09-15 20:16:28 +02:00
|
|
|
|
|
2015-11-29 21:01:14 +01:00
|
|
|
|
// 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;
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-10-12 03:47:44 +02: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
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
|
2013-10-12 03:47:44 +02:00
|
|
|
|
{
|
|
|
|
|
item.VoteCount = rval;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-09-15 20:16:28 +02: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();
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
|
|
|
|
int rval;
|
|
|
|
|
|
|
|
|
|
// int.TryParse is local aware, so it can be probamatic, force us culture
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (int.TryParse(val, NumberStyles.Integer, _usCulture, out rval))
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
|
|
|
|
item.RunTimeTicks = TimeSpan.FromMinutes(rval).Ticks;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Genre":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var vals = val
|
|
|
|
|
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(i => i.Trim())
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (vals.Count > 0)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
item.Genres.Clear();
|
2013-09-15 20:16:28 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
foreach (var genre in vals)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
item.AddGenre(genre);
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "Network":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var vals = val
|
|
|
|
|
.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
|
.Select(i => i.Trim())
|
|
|
|
|
.Where(i => !string.IsNullOrWhiteSpace(i))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (vals.Count > 0)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
item.Studios.Clear();
|
2013-09-15 20:16:28 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
foreach (var genre in vals)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
item.AddStudio(genre);
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
2013-06-16 00:19:47 +02:00
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
2013-09-15 19:34:48 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01: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>
|
|
|
|
|
/// <param name="lastTvDbUpdateTime">The last tv db update time.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task ExtractEpisodes(string seriesDataPath, string xmlFile, long? lastTvDbUpdateTime)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var settings = new XmlReaderSettings
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
2013-05-21 05:16:43 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var streamReader = new StreamReader(xmlFile, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
2013-06-16 00:19:47 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
reader.MoveToContent();
|
|
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "Episode":
|
2013-09-15 20:16:28 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var outerXml = reader.ReadOuterXml();
|
2013-09-15 20:16:28 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
await SaveEpsiodeXml(seriesDataPath, outerXml, lastTvDbUpdateTime).ConfigureAwait(false);
|
|
|
|
|
break;
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
2013-09-15 20:16:28 +02:00
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-21 05:16:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
private async Task SaveEpsiodeXml(string seriesDataPath, string xml, long? lastTvDbUpdateTime)
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2013-09-15 19:34:48 +02:00
|
|
|
|
var settings = new XmlReaderSettings
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2013-09-15 19:34:48 +02:00
|
|
|
|
CheckCharacters = false,
|
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
|
IgnoreComments = true,
|
|
|
|
|
ValidationType = ValidationType.None
|
|
|
|
|
};
|
2013-04-28 07:44:45 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var seasonNumber = -1;
|
|
|
|
|
var episodeNumber = -1;
|
|
|
|
|
var absoluteNumber = -1;
|
|
|
|
|
var lastUpdateString = string.Empty;
|
|
|
|
|
|
|
|
|
|
using (var streamReader = new StringReader(xml))
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
|
|
|
|
// Use XmlReader for best performance
|
|
|
|
|
using (var reader = XmlReader.Create(streamReader, settings))
|
2013-05-21 05:16:43 +02:00
|
|
|
|
{
|
2013-09-15 19:34:48 +02:00
|
|
|
|
reader.MoveToContent();
|
2013-07-31 14:09:23 +02:00
|
|
|
|
|
2013-09-15 19:34:48 +02:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2013-09-15 19:34:48 +02:00
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
case "lastupdated":
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
lastUpdateString = reader.ReadElementContentAsString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "EpisodeNumber":
|
|
|
|
|
{
|
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(val))
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2013-09-15 19:34:48 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
|
2013-09-15 19:34:48 +02:00
|
|
|
|
default:
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
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-09-15 19:34:48 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var hasEpisodeChanged = true;
|
2015-02-20 19:27:01 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(lastUpdateString) && lastTvDbUpdateTime.HasValue)
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
long num;
|
|
|
|
|
if (long.TryParse(lastUpdateString, NumberStyles.Any, _usCulture, out num))
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
hasEpisodeChanged = num >= lastTvDbUpdateTime.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-20 04:15:48 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
var file = Path.Combine(seriesDataPath, string.Format("episode-{0}-{1}.xml", seasonNumber, episodeNumber));
|
2013-11-20 04:15:48 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
// Only save the file if not already there, or if the episode has changed
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (hasEpisodeChanged || !_fileSystem.FileExists(file))
|
2014-02-03 21:51:28 +01:00
|
|
|
|
{
|
|
|
|
|
using (var writer = XmlWriter.Create(file, new XmlWriterSettings
|
|
|
|
|
{
|
|
|
|
|
Encoding = Encoding.UTF8,
|
|
|
|
|
Async = true
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
await writer.WriteRawAsync(xml).ConfigureAwait(false);
|
2013-09-15 19:34:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (absoluteNumber != -1)
|
2013-09-15 19:34:48 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
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
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (hasEpisodeChanged || !_fileSystem.FileExists(file))
|
2014-02-03 21:51:28 +01:00
|
|
|
|
{
|
|
|
|
|
using (var writer = XmlWriter.Create(file, new XmlWriterSettings
|
|
|
|
|
{
|
|
|
|
|
Encoding = Encoding.UTF8,
|
|
|
|
|
Async = true
|
|
|
|
|
}))
|
|
|
|
|
{
|
|
|
|
|
await writer.WriteRawAsync(xml).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-15 19:34:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-21 05:16:43 +02:00
|
|
|
|
/// <summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// Gets the series data path.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
2015-11-29 21:01:14 +01:00
|
|
|
|
/// <param name="seriesProviderIds">The series provider ids.</param>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <returns>System.String.</returns>
|
2015-11-29 21:01:14 +01:00
|
|
|
|
internal static string GetSeriesDataPath(IApplicationPaths appPaths, Dictionary<string, string> seriesProviderIds)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-11-29 21:01:14 +01:00
|
|
|
|
string seriesId;
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out seriesId) && !string.IsNullOrEmpty(seriesId))
|
2015-11-29 21:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
var seriesDataPath = Path.Combine(GetSeriesDataPath(appPaths), seriesId);
|
|
|
|
|
|
|
|
|
|
return seriesDataPath;
|
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
if (seriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out seriesId) && !string.IsNullOrEmpty(seriesId))
|
2015-11-29 21:01:14 +01:00
|
|
|
|
{
|
|
|
|
|
var seriesDataPath = Path.Combine(GetSeriesDataPath(appPaths), seriesId);
|
|
|
|
|
|
|
|
|
|
return seriesDataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
public string GetSeriesXmlPath(Dictionary<string, string> seriesProviderIds, string language)
|
|
|
|
|
{
|
|
|
|
|
var seriesDataPath = GetSeriesDataPath(_config.ApplicationPaths, seriesProviderIds);
|
2015-12-25 22:18:27 +01:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
var seriesXmlFilename = language.ToLower() + ".xml";
|
2015-12-25 22:18:27 +01:00
|
|
|
|
|
2016-03-04 18:07:14 +01:00
|
|
|
|
return Path.Combine(seriesDataPath, seriesXmlFilename);
|
|
|
|
|
}
|
2015-12-25 22:18:27 +01:00
|
|
|
|
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// Gets the series data path.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
internal static string GetSeriesDataPath(IApplicationPaths appPaths)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-03-05 03:59:59 +01:00
|
|
|
|
var dataPath = Path.Combine(appPaths.CachePath, "tvdb");
|
2013-02-21 02:33:05 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
return dataPath;
|
|
|
|
|
}
|
2013-05-06 21:31:57 +02:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
private void DeleteXmlFiles(string path)
|
|
|
|
|
{
|
|
|
|
|
try
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2015-09-18 19:50:24 +02:00
|
|
|
|
foreach (var file in _fileSystem.GetFilePaths(path, true)
|
2014-02-03 21:51:28 +01:00
|
|
|
|
.ToList())
|
|
|
|
|
{
|
2015-09-18 19:50:24 +02:00
|
|
|
|
_fileSystem.DeleteFile(file);
|
2014-02-03 21:51:28 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
catch (DirectoryNotFoundException)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
// No biggie
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-28 14:59:13 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01: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-28 14:59:13 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(fileStream))
|
|
|
|
|
{
|
|
|
|
|
var xml = await reader.ReadToEndAsync().ConfigureAwait(false);
|
2013-10-28 14:59:13 +01:00
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
validXml = StripInvalidXmlCharacters(xml);
|
2013-10-28 14:59:13 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read, true))
|
2013-08-28 17:56:56 +02:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
using (var writer = new StreamWriter(fileStream))
|
|
|
|
|
{
|
|
|
|
|
await writer.WriteAsync(validXml).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-08-28 17:56:56 +02:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// Strips the invalid XML characters.
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// </summary>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
/// <param name="inString">The in string.</param>
|
2013-02-21 02:33:05 +01:00
|
|
|
|
/// <returns>System.String.</returns>
|
2014-02-03 21:51:28 +01:00
|
|
|
|
public static string StripInvalidXmlCharacters(string inString)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
if (inString == null) return null;
|
|
|
|
|
|
|
|
|
|
var sbOutput = new StringBuilder();
|
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < inString.Length; i++)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
ch = inString[i];
|
|
|
|
|
if ((ch >= 0x0020 && ch <= 0xD7FF) ||
|
|
|
|
|
(ch >= 0xE000 && ch <= 0xFFFD) ||
|
|
|
|
|
ch == 0x0009 ||
|
|
|
|
|
ch == 0x000A ||
|
|
|
|
|
ch == 0x000D)
|
2013-02-21 02:33:05 +01:00
|
|
|
|
{
|
2014-02-03 21:51:28 +01:00
|
|
|
|
sbOutput.Append(ch);
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2014-02-03 21:51:28 +01:00
|
|
|
|
}
|
|
|
|
|
return sbOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "TheTVDB"; }
|
|
|
|
|
}
|
2014-02-15 17:36:09 +01:00
|
|
|
|
|
2015-09-19 23:25:19 +02:00
|
|
|
|
public async Task Identify(SeriesInfo info)
|
2014-05-06 04:00:16 +02:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(info.GetProviderId(MetadataProviders.Tvdb)))
|
2014-08-19 03:42:53 +02:00
|
|
|
|
{
|
2015-10-21 07:09:50 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2014-08-19 03:42:53 +02:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
var srch = await FindSeries(info.Name, info.Year, info.MetadataLanguage, CancellationToken.None).ConfigureAwait(false);
|
2014-08-19 03:42:53 +02:00
|
|
|
|
|
2015-10-21 07:09:50 +02:00
|
|
|
|
var entry = srch.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (entry != null)
|
|
|
|
|
{
|
|
|
|
|
var id = entry.GetProviderId(MetadataProviders.Tvdb);
|
|
|
|
|
info.SetProviderId(MetadataProviders.Tvdb, id);
|
2014-08-19 03:42:53 +02:00
|
|
|
|
}
|
2014-05-06 04:00:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-15 17:36:09 +01:00
|
|
|
|
public int Order
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// After Omdb
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-20 05:53:15 +01:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-03-02 16:42:21 +01:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = TvDbResourcePool
|
|
|
|
|
});
|
2014-02-20 05:53:15 +01:00
|
|
|
|
}
|
2013-02-21 02:33:05 +01:00
|
|
|
|
}
|
2016-04-21 07:13:33 +02:00
|
|
|
|
}
|