AudioDbAlbumProvider.cs 8.1 KB

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