AudioDbAlbumProvider.cs 9.5 KB

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