2013-10-31 07:03:23 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-05-27 15:25:04 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2015-01-17 22:45:10 -07:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
2013-05-27 15:25:04 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-06-23 11:55:30 -07:00
|
|
|
|
using MediaBrowser.Providers.Music;
|
2015-04-14 08:26:02 -07:00
|
|
|
|
using MediaBrowser.Providers.TV;
|
2013-05-27 15:25:04 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-10-03 21:23:11 -07:00
|
|
|
|
using CommonIO;
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
2013-06-09 09:47:28 -07:00
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
2014-02-09 16:08:01 -07:00
|
|
|
|
class FanartMovieUpdatesPostScanTask : ILibraryPostScanTask
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
2014-10-22 21:26:01 -07:00
|
|
|
|
private const string UpdatesUrl = "http://webservice.fanart.tv/v3/movies/latest?api_key={0}&date={1}";
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
/// <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 07:03:23 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
2014-02-09 16:08:01 -07:00
|
|
|
|
public FanartMovieUpdatesPostScanTask(IJsonSerializer jsonSerializer, IServerConfigurationManager config, ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
|
|
|
|
_config = config;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_httpClient = httpClient;
|
2013-10-31 07:03:23 -07:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-05-27 15:25:04 -07: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)
|
|
|
|
|
{
|
2015-01-17 22:45:10 -07:00
|
|
|
|
var options = FanartSeriesProvider.Current.GetFanartOptions();
|
|
|
|
|
|
|
|
|
|
if (!options.EnableAutomaticUpdates)
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-05 21:39:16 -07:00
|
|
|
|
var path = FanartMovieImageProvider.GetMoviesDataPath(_config.CommonApplicationPaths);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
2015-09-13 14:32:02 -07:00
|
|
|
|
_fileSystem.CreateDirectory(path);
|
2013-10-01 11:24:27 -07:00
|
|
|
|
|
2013-05-27 15:25:04 -07:00
|
|
|
|
var timestampFile = Path.Combine(path, "time.txt");
|
|
|
|
|
|
2015-10-03 20:38:46 -07:00
|
|
|
|
var timestampFileInfo = _fileSystem.GetFileInfo(timestampFile);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
2013-11-22 08:33:14 -07:00
|
|
|
|
// Don't check for updates every single time
|
|
|
|
|
if (timestampFileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(timestampFileInfo)).TotalDays < 3)
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out the last time we queried for updates
|
2015-09-13 14:32:02 -07:00
|
|
|
|
var lastUpdateTime = timestampFileInfo.Exists ? _fileSystem.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2015-01-17 22:45:10 -07:00
|
|
|
|
var moviesToUpdate = await GetMovieIdsToUpdate(existingDirectories, lastUpdateTime, options, cancellationToken).ConfigureAwait(false);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
progress.Report(5);
|
|
|
|
|
|
2013-11-01 08:55:25 -07:00
|
|
|
|
await UpdateMovies(moviesToUpdate, progress, cancellationToken).ConfigureAwait(false);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newUpdateTime = Convert.ToInt64(DateTimeToUnixTimestamp(DateTime.UtcNow)).ToString(UsCulture);
|
|
|
|
|
|
2015-09-13 14:32:02 -07:00
|
|
|
|
_fileSystem.WriteAllText(timestampFile, newUpdateTime, Encoding.UTF8);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-17 22:45:10 -07:00
|
|
|
|
private async Task<IEnumerable<string>> GetMovieIdsToUpdate(IEnumerable<string> existingIds, string lastUpdateTime, FanartOptions options, CancellationToken cancellationToken)
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
2015-01-12 00:01:19 -07:00
|
|
|
|
var url = string.Format(UpdatesUrl, FanartArtistProvider.ApiKey, lastUpdateTime);
|
|
|
|
|
|
2015-01-17 22:45:10 -07:00
|
|
|
|
var clientKey = options.UserApiKey;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(clientKey))
|
2015-01-12 00:01:19 -07:00
|
|
|
|
{
|
2015-01-17 22:45:10 -07:00
|
|
|
|
url += "&client_key=" + clientKey;
|
2015-01-12 00:01:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-27 15:25:04 -07:00
|
|
|
|
// First get last time
|
|
|
|
|
using (var stream = await _httpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
2015-01-12 00:01:19 -07:00
|
|
|
|
Url = url,
|
2013-05-27 15:25:04 -07:00
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableHttpCompression = true,
|
2014-02-25 09:04:08 -07:00
|
|
|
|
ResourcePool = FanartArtistProvider.Current.FanArtResourcePool
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
|
{
|
|
|
|
|
var json = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
2014-01-30 21:50:09 -07:00
|
|
|
|
// If empty fanart will return a string of "null", rather than an empty list
|
2013-05-27 15:25:04 -07:00
|
|
|
|
if (string.Equals(json, "null", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new List<string>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-22 21:26:01 -07:00
|
|
|
|
var updates = _jsonSerializer.DeserializeFromString<List<RootObject>>(json);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
2013-08-07 09:00:20 -07:00
|
|
|
|
var existingDictionary = existingIds.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
2014-10-22 21:26:01 -07:00
|
|
|
|
return updates.SelectMany(i =>
|
|
|
|
|
{
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(i.imdb_id))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.imdb_id);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(i.tmdb_id))
|
|
|
|
|
{
|
|
|
|
|
list.Add(i.tmdb_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
|
|
}).Where(existingDictionary.ContainsKey);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-01 08:55:25 -07:00
|
|
|
|
private async Task UpdateMovies(IEnumerable<string> idList, IProgress<double> progress, CancellationToken cancellationToken)
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
|
|
|
|
var list = idList.ToList();
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var id in list)
|
|
|
|
|
{
|
2013-11-01 08:55:25 -07:00
|
|
|
|
_logger.Info("Updating movie " + id);
|
|
|
|
|
|
2014-10-22 21:26:01 -07:00
|
|
|
|
await FanartMovieImageProvider.Current.DownloadMovieJson(id, cancellationToken).ConfigureAwait(false);
|
2013-05-27 15:25:04 -07:00
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= list.Count;
|
|
|
|
|
percent *= 95;
|
|
|
|
|
|
|
|
|
|
progress.Report(percent + 5);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-22 21:26:01 -07:00
|
|
|
|
public class RootObject
|
2013-05-27 15:25:04 -07:00
|
|
|
|
{
|
2014-10-22 21:26:01 -07:00
|
|
|
|
public string tmdb_id { get; set; }
|
|
|
|
|
public string imdb_id { get; set; }
|
2013-05-27 15:25:04 -07:00
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public string new_images { get; set; }
|
|
|
|
|
public string total_images { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|