FanArtTvUpdatesPostScanTask.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Serialization;
  7. using MediaBrowser.Providers.Music;
  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.TV
  17. {
  18. class FanArtTvUpdatesPostScanTask : ILibraryPostScanTask
  19. {
  20. private const string UpdatesUrl = "http://webservice.fanart.tv/v3/tv/latest?api_key={0}&date={1}";
  21. /// <summary>
  22. /// The _HTTP client
  23. /// </summary>
  24. private readonly IHttpClient _httpClient;
  25. /// <summary>
  26. /// The _logger
  27. /// </summary>
  28. private readonly ILogger _logger;
  29. /// <summary>
  30. /// The _config
  31. /// </summary>
  32. private readonly IServerConfigurationManager _config;
  33. private readonly IJsonSerializer _jsonSerializer;
  34. private readonly IFileSystem _fileSystem;
  35. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  36. public FanArtTvUpdatesPostScanTask(IJsonSerializer jsonSerializer, IServerConfigurationManager config, ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
  37. {
  38. _jsonSerializer = jsonSerializer;
  39. _config = config;
  40. _logger = logger;
  41. _httpClient = httpClient;
  42. _fileSystem = fileSystem;
  43. }
  44. /// <summary>
  45. /// Runs the specified progress.
  46. /// </summary>
  47. /// <param name="progress">The progress.</param>
  48. /// <param name="cancellationToken">The cancellation token.</param>
  49. /// <returns>Task.</returns>
  50. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  51. {
  52. if (!_config.Configuration.EnableFanArtUpdates)
  53. {
  54. progress.Report(100);
  55. return;
  56. }
  57. var path = FanartSeriesProvider.GetSeriesDataPath(_config.CommonApplicationPaths);
  58. Directory.CreateDirectory(path);
  59. var timestampFile = Path.Combine(path, "time.txt");
  60. var timestampFileInfo = new FileInfo(timestampFile);
  61. // Don't check for updates every single time
  62. if (timestampFileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(timestampFileInfo)).TotalDays < 3)
  63. {
  64. return;
  65. }
  66. // Find out the last time we queried for updates
  67. var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
  68. var existingDirectories = Directory.EnumerateDirectories(path).Select(Path.GetFileName).ToList();
  69. // If this is our first time, don't do any updates and just record the timestamp
  70. if (!string.IsNullOrEmpty(lastUpdateTime))
  71. {
  72. var seriesToUpdate = await GetSeriesIdsToUpdate(existingDirectories, lastUpdateTime, cancellationToken).ConfigureAwait(false);
  73. progress.Report(5);
  74. await UpdateSeries(seriesToUpdate, progress, cancellationToken).ConfigureAwait(false);
  75. }
  76. var newUpdateTime = Convert.ToInt64(DateTimeToUnixTimestamp(DateTime.UtcNow)).ToString(UsCulture);
  77. File.WriteAllText(timestampFile, newUpdateTime, Encoding.UTF8);
  78. progress.Report(100);
  79. }
  80. /// <summary>
  81. /// Gets the series ids to update.
  82. /// </summary>
  83. /// <param name="existingSeriesIds">The existing series ids.</param>
  84. /// <param name="lastUpdateTime">The last update time.</param>
  85. /// <param name="cancellationToken">The cancellation token.</param>
  86. /// <returns>Task{IEnumerable{System.String}}.</returns>
  87. private async Task<IEnumerable<string>> GetSeriesIdsToUpdate(IEnumerable<string> existingSeriesIds, string lastUpdateTime, CancellationToken cancellationToken)
  88. {
  89. // First get last time
  90. using (var stream = await _httpClient.Get(new HttpRequestOptions
  91. {
  92. Url = string.Format(UpdatesUrl, FanartArtistProvider.ApiKey, lastUpdateTime),
  93. CancellationToken = cancellationToken,
  94. EnableHttpCompression = true,
  95. ResourcePool = FanartArtistProvider.Current.FanArtResourcePool
  96. }).ConfigureAwait(false))
  97. {
  98. // If empty fanart will return a string of "null", rather than an empty list
  99. using (var reader = new StreamReader(stream))
  100. {
  101. var json = await reader.ReadToEndAsync().ConfigureAwait(false);
  102. if (string.Equals(json, "null", StringComparison.OrdinalIgnoreCase))
  103. {
  104. return new List<string>();
  105. }
  106. var existingDictionary = existingSeriesIds.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
  107. var updates = _jsonSerializer.DeserializeFromString<List<FanartUpdatesPostScanTask.FanArtUpdate>>(json);
  108. return updates.Select(i => i.id).Where(existingDictionary.ContainsKey);
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// Updates the series.
  114. /// </summary>
  115. /// <param name="idList">The id list.</param>
  116. /// <param name="progress">The progress.</param>
  117. /// <param name="cancellationToken">The cancellation token.</param>
  118. /// <returns>Task.</returns>
  119. private async Task UpdateSeries(IEnumerable<string> idList, IProgress<double> progress, CancellationToken cancellationToken)
  120. {
  121. var list = idList.ToList();
  122. var numComplete = 0;
  123. foreach (var id in list)
  124. {
  125. _logger.Info("Updating series " + id);
  126. await FanartSeriesProvider.Current.DownloadSeriesJson(id, cancellationToken).ConfigureAwait(false);
  127. numComplete++;
  128. double percent = numComplete;
  129. percent /= list.Count;
  130. percent *= 95;
  131. progress.Report(percent + 5);
  132. }
  133. }
  134. /// <summary>
  135. /// Dates the time to unix timestamp.
  136. /// </summary>
  137. /// <param name="dateTime">The date time.</param>
  138. /// <returns>System.Double.</returns>
  139. private static double DateTimeToUnixTimestamp(DateTime dateTime)
  140. {
  141. return (dateTime - new DateTime(1970, 1, 1).ToUniversalTime()).TotalSeconds;
  142. }
  143. public class FanArtUpdate
  144. {
  145. public string id { get; set; }
  146. public string name { get; set; }
  147. public string new_images { get; set; }
  148. public string total_images { get; set; }
  149. }
  150. }
  151. }