MovieUpdatesPrescanTask.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Common.Progress;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Threading.Tasks;
  16. namespace MediaBrowser.Providers.Movies
  17. {
  18. public class MovieUpdatesPreScanTask : ILibraryPrescanTask
  19. {
  20. /// <summary>
  21. /// The updates URL
  22. /// </summary>
  23. private const string UpdatesUrl = "http://api.themoviedb.org/3/movie/changes?start_date={0}&api_key={1}&page={2}";
  24. /// <summary>
  25. /// The _HTTP client
  26. /// </summary>
  27. private readonly IHttpClient _httpClient;
  28. /// <summary>
  29. /// The _logger
  30. /// </summary>
  31. private readonly ILogger _logger;
  32. /// <summary>
  33. /// The _config
  34. /// </summary>
  35. private readonly IServerConfigurationManager _config;
  36. private readonly IJsonSerializer _json;
  37. private readonly IFileSystem _fileSystem;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="MovieUpdatesPreScanTask"/> class.
  40. /// </summary>
  41. /// <param name="logger">The logger.</param>
  42. /// <param name="httpClient">The HTTP client.</param>
  43. /// <param name="config">The config.</param>
  44. /// <param name="json">The json.</param>
  45. public MovieUpdatesPreScanTask(ILogger logger, IHttpClient httpClient, IServerConfigurationManager config, IJsonSerializer json, IFileSystem fileSystem)
  46. {
  47. _logger = logger;
  48. _httpClient = httpClient;
  49. _config = config;
  50. _json = json;
  51. _fileSystem = fileSystem;
  52. }
  53. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  54. /// <summary>
  55. /// Runs the specified progress.
  56. /// </summary>
  57. /// <param name="progress">The progress.</param>
  58. /// <param name="cancellationToken">The cancellation token.</param>
  59. /// <returns>Task.</returns>
  60. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  61. {
  62. if (!_config.Configuration.EnableInternetProviders)
  63. {
  64. progress.Report(100);
  65. return;
  66. }
  67. var innerProgress = new ActionableProgress<double>();
  68. innerProgress.RegisterAction(pct => progress.Report(pct * .8));
  69. await Run(innerProgress, false, cancellationToken).ConfigureAwait(false);
  70. progress.Report(80);
  71. //innerProgress = new ActionableProgress<double>();
  72. //innerProgress.RegisterAction(pct => progress.Report(80 + pct * .2));
  73. //await Run(innerProgress, true, cancellationToken).ConfigureAwait(false);
  74. progress.Report(100);
  75. }
  76. /// <summary>
  77. /// Runs the specified progress.
  78. /// </summary>
  79. /// <param name="progress">The progress.</param>
  80. /// <param name="runForBoxSets">if set to <c>true</c> [run for box sets].</param>
  81. /// <param name="cancellationToken">The cancellation token.</param>
  82. /// <returns>Task.</returns>
  83. private async Task Run(IProgress<double> progress, bool runForBoxSets, CancellationToken cancellationToken)
  84. {
  85. var path = runForBoxSets ? MovieDbProvider.GetBoxSetsDataPath(_config.CommonApplicationPaths) : MovieDbProvider.GetMoviesDataPath(_config.CommonApplicationPaths);
  86. Directory.CreateDirectory(path);
  87. var timestampFile = Path.Combine(path, "time.txt");
  88. var timestampFileInfo = new FileInfo(timestampFile);
  89. // Don't check for updates every single time
  90. if (timestampFileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(timestampFileInfo)).TotalDays < 3)
  91. {
  92. return;
  93. }
  94. // Find out the last time we queried tvdb for updates
  95. var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
  96. var existingDirectories = Directory.EnumerateDirectories(path).Select(Path.GetFileName).ToList();
  97. if (!string.IsNullOrEmpty(lastUpdateTime))
  98. {
  99. long lastUpdateTicks;
  100. if (long.TryParse(lastUpdateTime, NumberStyles.Any, UsCulture, out lastUpdateTicks))
  101. {
  102. var lastUpdateDate = new DateTime(lastUpdateTicks, DateTimeKind.Utc);
  103. // They only allow up to 14 days of updates
  104. if ((DateTime.UtcNow - lastUpdateDate).TotalDays > 13)
  105. {
  106. lastUpdateDate = DateTime.UtcNow.AddDays(-13);
  107. }
  108. var updatedIds = await GetIdsToUpdate(lastUpdateDate, 1, cancellationToken).ConfigureAwait(false);
  109. var existingDictionary = existingDirectories.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  110. var idsToUpdate = updatedIds.Where(i => !string.IsNullOrWhiteSpace(i) && existingDictionary.ContainsKey(i));
  111. await UpdateMovies(idsToUpdate, runForBoxSets, path, progress, cancellationToken).ConfigureAwait(false);
  112. }
  113. }
  114. File.WriteAllText(timestampFile, DateTime.UtcNow.Ticks.ToString(UsCulture), Encoding.UTF8);
  115. progress.Report(100);
  116. }
  117. /// <summary>
  118. /// Gets the ids to update.
  119. /// </summary>
  120. /// <param name="lastUpdateTime">The last update time.</param>
  121. /// <param name="page">The page.</param>
  122. /// <param name="cancellationToken">The cancellation token.</param>
  123. /// <returns>Task{IEnumerable{System.String}}.</returns>
  124. private async Task<IEnumerable<string>> GetIdsToUpdate(DateTime lastUpdateTime, int page, CancellationToken cancellationToken)
  125. {
  126. bool hasMorePages;
  127. var list = new List<string>();
  128. // First get last time
  129. using (var stream = await _httpClient.Get(new HttpRequestOptions
  130. {
  131. Url = string.Format(UpdatesUrl, lastUpdateTime.ToString("yyyy-MM-dd"), MovieDbProvider.ApiKey, page),
  132. CancellationToken = cancellationToken,
  133. EnableHttpCompression = true,
  134. ResourcePool = MovieDbProvider.Current.MovieDbResourcePool,
  135. AcceptHeader = MovieDbProvider.AcceptHeader
  136. }).ConfigureAwait(false))
  137. {
  138. var obj = _json.DeserializeFromStream<RootObject>(stream);
  139. var data = obj.results.Select(i => i.id.ToString(UsCulture));
  140. list.AddRange(data);
  141. hasMorePages = page < obj.total_pages;
  142. }
  143. if (hasMorePages)
  144. {
  145. var more = await GetIdsToUpdate(lastUpdateTime, page + 1, cancellationToken).ConfigureAwait(false);
  146. list.AddRange(more);
  147. }
  148. return list;
  149. }
  150. /// <summary>
  151. /// Updates the movies.
  152. /// </summary>
  153. /// <param name="ids">The ids.</param>
  154. /// <param name="isBoxSet">if set to <c>true</c> [is box set].</param>
  155. /// <param name="moviesDataPath">The movies data path.</param>
  156. /// <param name="progress">The progress.</param>
  157. /// <param name="cancellationToken">The cancellation token.</param>
  158. /// <returns>Task.</returns>
  159. private async Task UpdateMovies(IEnumerable<string> ids, bool isBoxSet, string moviesDataPath, IProgress<double> progress, CancellationToken cancellationToken)
  160. {
  161. var list = ids.ToList();
  162. var numComplete = 0;
  163. foreach (var id in list)
  164. {
  165. try
  166. {
  167. await UpdateMovie(id, isBoxSet, moviesDataPath, cancellationToken).ConfigureAwait(false);
  168. }
  169. catch (Exception ex)
  170. {
  171. _logger.ErrorException("Error updating tmdb movie id {0}", ex, id);
  172. }
  173. numComplete++;
  174. double percent = numComplete;
  175. percent /= list.Count;
  176. percent *= 100;
  177. progress.Report(percent);
  178. }
  179. }
  180. /// <summary>
  181. /// Updates the movie.
  182. /// </summary>
  183. /// <param name="id">The id.</param>
  184. /// <param name="isBoxSet">if set to <c>true</c> [is box set].</param>
  185. /// <param name="dataPath">The data path.</param>
  186. /// <param name="cancellationToken">The cancellation token.</param>
  187. /// <returns>Task.</returns>
  188. private Task UpdateMovie(string id, bool isBoxSet, string dataPath, CancellationToken cancellationToken)
  189. {
  190. _logger.Info("Updating movie from tmdb " + id);
  191. var itemDataPath = Path.Combine(dataPath, id);
  192. Directory.CreateDirectory(dataPath);
  193. return MovieDbProvider.Current.DownloadMovieInfo(id, isBoxSet, itemDataPath, cancellationToken);
  194. }
  195. class Result
  196. {
  197. public int id { get; set; }
  198. public bool? adult { get; set; }
  199. }
  200. class RootObject
  201. {
  202. public List<Result> results { get; set; }
  203. public int page { get; set; }
  204. public int total_pages { get; set; }
  205. public int total_results { get; set; }
  206. public RootObject()
  207. {
  208. results = new List<Result>();
  209. }
  210. }
  211. }
  212. }