2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-06-11 14:39:25 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Providers.Music;
|
|
|
|
|
using System;
|
2013-05-27 18:33:43 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.TV
|
2013-05-27 18:33:43 +00:00
|
|
|
|
{
|
|
|
|
|
class FanArtTvUpdatesPrescanTask : ILibraryPrescanTask
|
|
|
|
|
{
|
|
|
|
|
private const string UpdatesUrl = "http://api.fanart.tv/webservice/newtv/{0}/{1}/";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _HTTP client
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _config
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-27 18:33:43 +00:00
|
|
|
|
|
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2013-10-31 14:03:23 +00:00
|
|
|
|
public FanArtTvUpdatesPrescanTask(IJsonSerializer jsonSerializer, IServerConfigurationManager config, ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-05-27 18:33:43 +00:00
|
|
|
|
{
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
_config = config;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-05-27 18:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs the specified progress.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!_config.Configuration.EnableInternetProviders)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var path = FanArtTvProvider.GetSeriesDataPath(_config.CommonApplicationPaths);
|
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
2013-05-27 18:33:43 +00:00
|
|
|
|
var timestampFile = Path.Combine(path, "time.txt");
|
|
|
|
|
|
|
|
|
|
var timestampFileInfo = new FileInfo(timestampFile);
|
|
|
|
|
|
2013-05-29 01:45:39 +00:00
|
|
|
|
// Don't check for tvdb updates anymore frequently than 24 hours
|
2013-10-31 14:03:23 +00:00
|
|
|
|
if (timestampFileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(timestampFileInfo)).TotalDays < 1)
|
2013-05-27 18:33:43 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out the last time we queried for updates
|
|
|
|
|
var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
|
|
|
|
|
|
|
|
|
|
var existingDirectories = Directory.EnumerateDirectories(path).Select(Path.GetFileName).ToList();
|
|
|
|
|
|
|
|
|
|
// If this is our first time, don't do any updates and just record the timestamp
|
|
|
|
|
if (!string.IsNullOrEmpty(lastUpdateTime))
|
|
|
|
|
{
|
|
|
|
|
var seriesToUpdate = await GetSeriesIdsToUpdate(existingDirectories, lastUpdateTime, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
progress.Report(5);
|
|
|
|
|
|
|
|
|
|
await UpdateSeries(seriesToUpdate, path, progress, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newUpdateTime = Convert.ToInt64(DateTimeToUnixTimestamp(DateTime.UtcNow)).ToString(UsCulture);
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(timestampFile, newUpdateTime, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the series ids to update.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="existingSeriesIds">The existing series ids.</param>
|
|
|
|
|
/// <param name="lastUpdateTime">The last update time.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{IEnumerable{System.String}}.</returns>
|
|
|
|
|
private async Task<IEnumerable<string>> GetSeriesIdsToUpdate(IEnumerable<string> existingSeriesIds, string lastUpdateTime, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
// First get last time
|
|
|
|
|
using (var stream = await _httpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = string.Format(UpdatesUrl, FanartBaseProvider.ApiKey, lastUpdateTime),
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableHttpCompression = true,
|
|
|
|
|
ResourcePool = FanartBaseProvider.FanArtResourcePool
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
// If empty fanart will return a string of "null", rather than an empty list
|
|
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
|
{
|
|
|
|
|
var json = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (string.Equals(json, "null", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new List<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 16:00:20 +00:00
|
|
|
|
var existingDictionary = existingSeriesIds.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
2013-05-27 18:33:43 +00:00
|
|
|
|
var updates = _jsonSerializer.DeserializeFromString<List<FanArtUpdatesPrescanTask.FanArtUpdate>>(json);
|
|
|
|
|
|
2013-08-07 16:00:20 +00:00
|
|
|
|
return updates.Select(i => i.id).Where(existingDictionary.ContainsKey);
|
2013-05-27 18:33:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the series.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idList">The id list.</param>
|
|
|
|
|
/// <param name="seriesDataPath">The artists data path.</param>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task UpdateSeries(IEnumerable<string> idList, string seriesDataPath, IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var list = idList.ToList();
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var id in list)
|
|
|
|
|
{
|
2013-06-11 14:06:08 +00:00
|
|
|
|
await UpdateSeries(id, seriesDataPath, cancellationToken).ConfigureAwait(false);
|
2013-05-27 18:33:43 +00:00
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= list.Count;
|
|
|
|
|
percent *= 95;
|
|
|
|
|
|
|
|
|
|
progress.Report(percent + 5);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task UpdateSeries(string tvdbId, string seriesDataPath, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Updating series " + tvdbId);
|
|
|
|
|
|
|
|
|
|
seriesDataPath = Path.Combine(seriesDataPath, tvdbId);
|
|
|
|
|
|
2013-10-01 18:24:27 +00:00
|
|
|
|
Directory.CreateDirectory(seriesDataPath);
|
2013-05-27 18:33:43 +00:00
|
|
|
|
|
|
|
|
|
return FanArtTvProvider.Current.DownloadSeriesXml(seriesDataPath, tvdbId, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dates the time to unix timestamp.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dateTime">The date time.</param>
|
|
|
|
|
/// <returns>System.Double.</returns>
|
|
|
|
|
private static double DateTimeToUnixTimestamp(DateTime dateTime)
|
|
|
|
|
{
|
|
|
|
|
return (dateTime - new DateTime(1970, 1, 1).ToUniversalTime()).TotalSeconds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class FanArtUpdate
|
|
|
|
|
{
|
|
|
|
|
public string id { get; set; }
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string new_images { get; set; }
|
|
|
|
|
public string total_images { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|