2013-10-31 14:03:23 +00:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-12-27 00:23:58 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-12-27 00:23:58 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Movies
|
|
|
|
|
{
|
2013-12-08 22:09:06 +00:00
|
|
|
|
public class MovieUpdatesPreScanTask : ILibraryPostScanTask
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The updates URL
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const string UpdatesUrl = "http://api.themoviedb.org/3/movie/changes?start_date={0}&api_key={1}&page={2}";
|
|
|
|
|
|
|
|
|
|
/// <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 _json;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-12-27 00:23:58 +00:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="MovieUpdatesPreScanTask"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <param name="httpClient">The HTTP client.</param>
|
|
|
|
|
/// <param name="config">The config.</param>
|
|
|
|
|
/// <param name="json">The json.</param>
|
2013-12-27 00:23:58 +00:00
|
|
|
|
public MovieUpdatesPreScanTask(ILogger logger, IHttpClient httpClient, IServerConfigurationManager config, IJsonSerializer json, IFileSystem fileSystem, ILibraryManager libraryManager)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_config = config;
|
|
|
|
|
_json = json;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
_fileSystem = fileSystem;
|
2013-12-27 00:23:58 +00:00
|
|
|
|
_libraryManager = libraryManager;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2014-01-10 13:52:01 +00:00
|
|
|
|
if (!_config.Configuration.EnableInternetProviders && !_config.Configuration.EnableTmdbUpdates)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
var path = MovieDbProvider.GetMoviesDataPath(_config.CommonApplicationPaths);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
|
|
|
|
var timestampFile = Path.Combine(path, "time.txt");
|
|
|
|
|
|
|
|
|
|
var timestampFileInfo = new FileInfo(timestampFile);
|
|
|
|
|
|
2013-11-22 15:33:14 +00:00
|
|
|
|
// Don't check for updates every single time
|
2013-12-21 20:38:35 +00:00
|
|
|
|
if (timestampFileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(timestampFileInfo)).TotalDays < 7)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2013-10-14 17:35:24 +00:00
|
|
|
|
return;
|
2013-10-14 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out the last time we queried tvdb for updates
|
|
|
|
|
var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
|
|
|
|
|
|
|
|
|
|
var existingDirectories = Directory.EnumerateDirectories(path).Select(Path.GetFileName).ToList();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(lastUpdateTime))
|
|
|
|
|
{
|
|
|
|
|
long lastUpdateTicks;
|
|
|
|
|
|
|
|
|
|
if (long.TryParse(lastUpdateTime, NumberStyles.Any, UsCulture, out lastUpdateTicks))
|
|
|
|
|
{
|
|
|
|
|
var lastUpdateDate = new DateTime(lastUpdateTicks, DateTimeKind.Utc);
|
|
|
|
|
|
|
|
|
|
// They only allow up to 14 days of updates
|
|
|
|
|
if ((DateTime.UtcNow - lastUpdateDate).TotalDays > 13)
|
|
|
|
|
{
|
|
|
|
|
lastUpdateDate = DateTime.UtcNow.AddDays(-13);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var updatedIds = await GetIdsToUpdate(lastUpdateDate, 1, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var existingDictionary = existingDirectories.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
var idsToUpdate = updatedIds.Where(i => !string.IsNullOrWhiteSpace(i) && existingDictionary.ContainsKey(i));
|
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
await UpdateMovies(idsToUpdate, progress, cancellationToken).ConfigureAwait(false);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(timestampFile, DateTime.UtcNow.Ticks.ToString(UsCulture), Encoding.UTF8);
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the ids to update.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="lastUpdateTime">The last update time.</param>
|
|
|
|
|
/// <param name="page">The page.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{IEnumerable{System.String}}.</returns>
|
|
|
|
|
private async Task<IEnumerable<string>> GetIdsToUpdate(DateTime lastUpdateTime, int page, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
bool hasMorePages;
|
|
|
|
|
var list = new List<string>();
|
|
|
|
|
|
|
|
|
|
// First get last time
|
|
|
|
|
using (var stream = await _httpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = string.Format(UpdatesUrl, lastUpdateTime.ToString("yyyy-MM-dd"), MovieDbProvider.ApiKey, page),
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableHttpCompression = true,
|
|
|
|
|
ResourcePool = MovieDbProvider.Current.MovieDbResourcePool,
|
|
|
|
|
AcceptHeader = MovieDbProvider.AcceptHeader
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
var obj = _json.DeserializeFromStream<RootObject>(stream);
|
|
|
|
|
|
|
|
|
|
var data = obj.results.Select(i => i.id.ToString(UsCulture));
|
|
|
|
|
|
|
|
|
|
list.AddRange(data);
|
|
|
|
|
|
|
|
|
|
hasMorePages = page < obj.total_pages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasMorePages)
|
|
|
|
|
{
|
|
|
|
|
var more = await GetIdsToUpdate(lastUpdateTime, page + 1, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
list.AddRange(more);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the movies.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids">The ids.</param>
|
|
|
|
|
/// <param name="progress">The progress.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-01-30 21:23:54 +00:00
|
|
|
|
private async Task UpdateMovies(IEnumerable<string> ids, IProgress<double> progress, CancellationToken cancellationToken)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
|
|
|
|
var list = ids.ToList();
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
2013-12-27 00:23:58 +00:00
|
|
|
|
// Gather all movies into a lookup by tmdb id
|
|
|
|
|
var allMovies = _libraryManager.RootFolder.RecursiveChildren
|
|
|
|
|
.Where(i => i is Movie || i is Trailer)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i.GetProviderId(MetadataProviders.Tmdb)))
|
|
|
|
|
.ToLookup(i => i.GetProviderId(MetadataProviders.Tmdb));
|
|
|
|
|
|
2013-10-14 17:34:54 +00:00
|
|
|
|
foreach (var id in list)
|
|
|
|
|
{
|
2013-12-27 00:23:58 +00:00
|
|
|
|
// Find the preferred language(s) for the movie in the library
|
|
|
|
|
var languages = allMovies[id]
|
|
|
|
|
.Select(i => i.GetPreferredMetadataLanguage())
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var language in languages)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2013-12-27 00:23:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2014-01-30 21:23:54 +00:00
|
|
|
|
await UpdateMovie(id, language, cancellationToken).ConfigureAwait(false);
|
2013-12-27 00:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error updating tmdb movie id {0}, language {1}", ex, id, language);
|
|
|
|
|
}
|
2013-10-14 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= list.Count;
|
|
|
|
|
percent *= 100;
|
|
|
|
|
|
|
|
|
|
progress.Report(percent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates the movie.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">The id.</param>
|
2013-12-27 00:23:58 +00:00
|
|
|
|
/// <param name="preferredMetadataLanguage">The preferred metadata language.</param>
|
2013-10-14 17:34:54 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-01-30 21:23:54 +00:00
|
|
|
|
private Task UpdateMovie(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
|
2013-10-14 17:34:54 +00:00
|
|
|
|
{
|
2013-12-27 00:23:58 +00:00
|
|
|
|
_logger.Info("Updating movie from tmdb " + id + ", language " + preferredMetadataLanguage);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
|
2014-01-30 21:23:54 +00:00
|
|
|
|
return MovieDbProvider.Current.DownloadMovieInfo(id, preferredMetadataLanguage, cancellationToken);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Result
|
|
|
|
|
{
|
|
|
|
|
public int id { get; set; }
|
|
|
|
|
public bool? adult { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RootObject
|
|
|
|
|
{
|
|
|
|
|
public List<Result> results { get; set; }
|
|
|
|
|
public int page { get; set; }
|
|
|
|
|
public int total_pages { get; set; }
|
|
|
|
|
public int total_results { get; set; }
|
|
|
|
|
|
|
|
|
|
public RootObject()
|
|
|
|
|
{
|
|
|
|
|
results = new List<Result>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|