FanArtUpdatesPrescanTask.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Net;
  6. using MediaBrowser.Model.Serialization;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Controller.Providers.Music
  16. {
  17. class FanArtUpdatesPrescanTask : ILibraryPrescanTask
  18. {
  19. private const string UpdatesUrl = "http://api.fanart.tv/webservice/newmusic/{0}/{1}/";
  20. /// <summary>
  21. /// The _HTTP client
  22. /// </summary>
  23. private readonly IHttpClient _httpClient;
  24. /// <summary>
  25. /// The _logger
  26. /// </summary>
  27. private readonly ILogger _logger;
  28. /// <summary>
  29. /// The _config
  30. /// </summary>
  31. private readonly IServerConfigurationManager _config;
  32. private readonly IJsonSerializer _jsonSerializer;
  33. private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  34. public FanArtUpdatesPrescanTask(IJsonSerializer jsonSerializer, IServerConfigurationManager config, ILogger logger, IHttpClient httpClient)
  35. {
  36. _jsonSerializer = jsonSerializer;
  37. _config = config;
  38. _logger = logger;
  39. _httpClient = httpClient;
  40. }
  41. /// <summary>
  42. /// Runs the specified progress.
  43. /// </summary>
  44. /// <param name="progress">The progress.</param>
  45. /// <param name="cancellationToken">The cancellation token.</param>
  46. /// <returns>Task.</returns>
  47. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  48. {
  49. if (!_config.Configuration.EnableInternetProviders)
  50. {
  51. progress.Report(100);
  52. return;
  53. }
  54. var path = FanArtArtistProvider.GetArtistDataPath(_config.CommonApplicationPaths);
  55. var timestampFile = Path.Combine(path, "time.txt");
  56. var timestampFileInfo = new FileInfo(timestampFile);
  57. // Don't check for tvdb updates anymore frequently than 24 hours
  58. if (timestampFileInfo.Exists && (DateTime.UtcNow - timestampFileInfo.LastWriteTimeUtc).TotalDays < 1)
  59. {
  60. return;
  61. }
  62. // Find out the last time we queried for updates
  63. var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty;
  64. var existingDirectories = Directory.EnumerateDirectories(path).Select(Path.GetFileName).ToList();
  65. // If this is our first time, don't do any updates and just record the timestamp
  66. if (!string.IsNullOrEmpty(lastUpdateTime))
  67. {
  68. var artistsToUpdate = await GetArtistIdsToUpdate(existingDirectories, lastUpdateTime, cancellationToken).ConfigureAwait(false);
  69. progress.Report(5);
  70. await UpdateArtists(artistsToUpdate, path, progress, cancellationToken).ConfigureAwait(false);
  71. }
  72. var newUpdateTime = Convert.ToInt64(DateTimeToUnixTimestamp(DateTime.UtcNow)).ToString(UsCulture);
  73. File.WriteAllText(timestampFile, newUpdateTime, Encoding.UTF8);
  74. progress.Report(100);
  75. }
  76. /// <summary>
  77. /// Gets the artist ids to update.
  78. /// </summary>
  79. /// <param name="existingArtistIds">The existing series ids.</param>
  80. /// <param name="lastUpdateTime">The last update time.</param>
  81. /// <param name="cancellationToken">The cancellation token.</param>
  82. /// <returns>Task{IEnumerable{System.String}}.</returns>
  83. private async Task<IEnumerable<string>> GetArtistIdsToUpdate(IEnumerable<string> existingArtistIds, string lastUpdateTime, CancellationToken cancellationToken)
  84. {
  85. // First get last time
  86. using (var stream = await _httpClient.Get(new HttpRequestOptions
  87. {
  88. Url = string.Format(UpdatesUrl, FanartBaseProvider.ApiKey, lastUpdateTime),
  89. CancellationToken = cancellationToken,
  90. EnableHttpCompression = true,
  91. ResourcePool = FanartBaseProvider.FanArtResourcePool
  92. }).ConfigureAwait(false))
  93. {
  94. // If empty fanart will return a string of "null", rather than an empty list
  95. using (var reader = new StreamReader(stream))
  96. {
  97. var json = await reader.ReadToEndAsync().ConfigureAwait(false);
  98. if (string.Equals(json, "null", StringComparison.OrdinalIgnoreCase))
  99. {
  100. return new List<string>();
  101. }
  102. var updates = _jsonSerializer.DeserializeFromString<List<FanArtUpdate>>(json);
  103. return updates.Select(i => i.id).Where(i => existingArtistIds.Contains(i, StringComparer.OrdinalIgnoreCase));
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Updates the artists.
  109. /// </summary>
  110. /// <param name="idList">The id list.</param>
  111. /// <param name="artistsDataPath">The artists data path.</param>
  112. /// <param name="progress">The progress.</param>
  113. /// <param name="cancellationToken">The cancellation token.</param>
  114. /// <returns>Task.</returns>
  115. private async Task UpdateArtists(IEnumerable<string> idList, string artistsDataPath, IProgress<double> progress, CancellationToken cancellationToken)
  116. {
  117. var list = idList.ToList();
  118. var numComplete = 0;
  119. foreach (var id in list)
  120. {
  121. try
  122. {
  123. await UpdateArtist(id, artistsDataPath, cancellationToken).ConfigureAwait(false);
  124. }
  125. catch (HttpException ex)
  126. {
  127. // Already logged at lower levels, but don't fail the whole operation, unless something other than a timeout
  128. if (!ex.IsTimedOut)
  129. {
  130. throw;
  131. }
  132. }
  133. numComplete++;
  134. double percent = numComplete;
  135. percent /= list.Count;
  136. percent *= 95;
  137. progress.Report(percent + 5);
  138. }
  139. }
  140. /// <summary>
  141. /// Updates the artist.
  142. /// </summary>
  143. /// <param name="musicBrainzId">The musicBrainzId.</param>
  144. /// <param name="artistsDataPath">The artists data path.</param>
  145. /// <param name="cancellationToken">The cancellation token.</param>
  146. /// <returns>Task.</returns>
  147. private Task UpdateArtist(string musicBrainzId, string artistsDataPath, CancellationToken cancellationToken)
  148. {
  149. _logger.Info("Updating artist " + musicBrainzId);
  150. artistsDataPath = Path.Combine(artistsDataPath, musicBrainzId);
  151. if (!Directory.Exists(artistsDataPath))
  152. {
  153. Directory.CreateDirectory(artistsDataPath);
  154. }
  155. return FanArtArtistProvider.Current.DownloadArtistXml(artistsDataPath, musicBrainzId, cancellationToken);
  156. }
  157. /// <summary>
  158. /// Dates the time to unix timestamp.
  159. /// </summary>
  160. /// <param name="dateTime">The date time.</param>
  161. /// <returns>System.Double.</returns>
  162. private static double DateTimeToUnixTimestamp(DateTime dateTime)
  163. {
  164. return (dateTime - new DateTime(1970, 1, 1).ToUniversalTime()).TotalSeconds;
  165. }
  166. public class FanArtUpdate
  167. {
  168. public string id { get; set; }
  169. public string name { get; set; }
  170. public string new_images { get; set; }
  171. public string total_images { get; set; }
  172. }
  173. }
  174. }