AudioDbAlbumProvider.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Providers.Music
  16. {
  17. public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum,AlbumInfo>, IHasOrder
  18. {
  19. private readonly IServerConfigurationManager _config;
  20. private readonly IFileSystem _fileSystem;
  21. private readonly IHttpClient _httpClient;
  22. private readonly IJsonSerializer _json;
  23. public static AudioDbAlbumProvider Current;
  24. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  25. public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)
  26. {
  27. _config = config;
  28. _fileSystem = fileSystem;
  29. _httpClient = httpClient;
  30. _json = json;
  31. Current = this;
  32. }
  33. public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
  34. {
  35. var result = new MetadataResult<MusicAlbum>();
  36. var id = info.GetReleaseGroupId();
  37. if (!string.IsNullOrWhiteSpace(id))
  38. {
  39. await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
  40. var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
  41. var obj = _json.DeserializeFromFile<RootObject>(path);
  42. if (obj != null && obj.album != null && obj.album.Count > 0)
  43. {
  44. result.Item = new MusicAlbum();
  45. result.HasMetadata = true;
  46. ProcessResult(result.Item, obj.album[0]);
  47. }
  48. }
  49. return result;
  50. }
  51. private void ProcessResult(MusicAlbum item, Album result)
  52. {
  53. item.AlbumArtist = result.strArtist;
  54. if (!string.IsNullOrEmpty(result.intYearReleased))
  55. {
  56. item.ProductionYear = int.Parse(result.intYearReleased, _usCulture);
  57. }
  58. if (!string.IsNullOrEmpty(result.strGenre))
  59. {
  60. item.Genres = new List<string> { result.strGenre };
  61. }
  62. item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
  63. item.SetProviderId(MetadataProviders.AudioDbAlbum, result.idAlbum);
  64. item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
  65. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, result.strMusicBrainzID);
  66. }
  67. public string Name
  68. {
  69. get { return "TheAudioDB"; }
  70. }
  71. private readonly Task _cachedTask = Task.FromResult(true);
  72. internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  73. {
  74. var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  75. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  76. if (fileInfo.Exists)
  77. {
  78. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
  79. {
  80. return _cachedTask;
  81. }
  82. }
  83. return DownloadInfo(musicBrainzReleaseGroupId, cancellationToken);
  84. }
  85. internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  86. {
  87. cancellationToken.ThrowIfCancellationRequested();
  88. var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
  89. var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  90. Directory.CreateDirectory(Path.GetDirectoryName(path));
  91. using (var response = await _httpClient.Get(new HttpRequestOptions
  92. {
  93. Url = url,
  94. ResourcePool = AudioDbArtistProvider.Current.AudioDbResourcePool,
  95. CancellationToken = cancellationToken
  96. }).ConfigureAwait(false))
  97. {
  98. using (var xmlFileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  99. {
  100. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  101. }
  102. }
  103. }
  104. private static string GetAlbumDataPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
  105. {
  106. var dataPath = Path.Combine(GetAlbumDataPath(appPaths), musicBrainzReleaseGroupId);
  107. return dataPath;
  108. }
  109. private static string GetAlbumDataPath(IApplicationPaths appPaths)
  110. {
  111. var dataPath = Path.Combine(appPaths.CachePath, "audiodb-album");
  112. return dataPath;
  113. }
  114. internal static string GetAlbumInfoPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
  115. {
  116. var dataPath = GetAlbumDataPath(appPaths, musicBrainzReleaseGroupId);
  117. return Path.Combine(dataPath, "album.json");
  118. }
  119. public int Order
  120. {
  121. get
  122. {
  123. // After music brainz
  124. return 1;
  125. }
  126. }
  127. public class Album
  128. {
  129. public string idAlbum { get; set; }
  130. public string idArtist { get; set; }
  131. public string strAlbum { get; set; }
  132. public string strArtist { get; set; }
  133. public string intYearReleased { get; set; }
  134. public string strGenre { get; set; }
  135. public string strSubGenre { get; set; }
  136. public string strReleaseFormat { get; set; }
  137. public string intSales { get; set; }
  138. public string strAlbumThumb { get; set; }
  139. public string strAlbumCDart { get; set; }
  140. public string strDescriptionEN { get; set; }
  141. public object strDescriptionDE { get; set; }
  142. public object strDescriptionFR { get; set; }
  143. public object strDescriptionCN { get; set; }
  144. public object strDescriptionIT { get; set; }
  145. public object strDescriptionJP { get; set; }
  146. public object strDescriptionRU { get; set; }
  147. public object strDescriptionES { get; set; }
  148. public object strDescriptionPT { get; set; }
  149. public object strDescriptionSE { get; set; }
  150. public object strDescriptionNL { get; set; }
  151. public object strDescriptionHU { get; set; }
  152. public object strDescriptionNO { get; set; }
  153. public object strDescriptionIL { get; set; }
  154. public object strDescriptionPL { get; set; }
  155. public object intLoved { get; set; }
  156. public object intScore { get; set; }
  157. public string strReview { get; set; }
  158. public object strMood { get; set; }
  159. public object strTheme { get; set; }
  160. public object strSpeed { get; set; }
  161. public object strLocation { get; set; }
  162. public string strMusicBrainzID { get; set; }
  163. public string strMusicBrainzArtistID { get; set; }
  164. public object strItunesID { get; set; }
  165. public object strAmazonID { get; set; }
  166. public string strLocked { get; set; }
  167. }
  168. public class RootObject
  169. {
  170. public List<Album> album { get; set; }
  171. }
  172. }
  173. }