AudioDbAlbumProvider.cs 8.3 KB

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