AlbumProvider.cs 9.5 KB

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