AudioDbAlbumProvider.cs 8.3 KB

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