AlbumProvider.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // TODO maybe remove when artist metadata can be disabled
  50. if (!Plugin.Instance.Configuration.Enable)
  51. {
  52. return result;
  53. }
  54. var id = info.GetReleaseGroupId();
  55. if (!string.IsNullOrWhiteSpace(id))
  56. {
  57. await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
  58. var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
  59. var obj = _json.DeserializeFromFile<RootObject>(path);
  60. if (obj != null && obj.album != null && obj.album.Count > 0)
  61. {
  62. result.Item = new MusicAlbum();
  63. result.HasMetadata = true;
  64. ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
  65. }
  66. }
  67. return result;
  68. }
  69. private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
  70. {
  71. if (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
  72. {
  73. item.Album = result.strAlbum;
  74. }
  75. if (!string.IsNullOrWhiteSpace(result.strArtist))
  76. {
  77. item.AlbumArtists = new string[] { result.strArtist };
  78. }
  79. if (!string.IsNullOrEmpty(result.intYearReleased))
  80. {
  81. item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
  82. }
  83. if (!string.IsNullOrEmpty(result.strGenre))
  84. {
  85. item.Genres = new[] { result.strGenre };
  86. }
  87. item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
  88. item.SetProviderId(MetadataProvider.AudioDbAlbum, result.idAlbum);
  89. item.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
  90. item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, result.strMusicBrainzID);
  91. string overview = null;
  92. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  93. {
  94. overview = result.strDescriptionDE;
  95. }
  96. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  97. {
  98. overview = result.strDescriptionFR;
  99. }
  100. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  101. {
  102. overview = result.strDescriptionNL;
  103. }
  104. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  105. {
  106. overview = result.strDescriptionRU;
  107. }
  108. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  109. {
  110. overview = result.strDescriptionIT;
  111. }
  112. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  113. {
  114. overview = result.strDescriptionPT;
  115. }
  116. if (string.IsNullOrWhiteSpace(overview))
  117. {
  118. overview = result.strDescriptionEN;
  119. }
  120. item.Overview = (overview ?? string.Empty).StripHtml();
  121. }
  122. internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  123. {
  124. var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  125. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  126. if (fileInfo.Exists)
  127. {
  128. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
  129. {
  130. return Task.CompletedTask;
  131. }
  132. }
  133. return DownloadInfo(musicBrainzReleaseGroupId, cancellationToken);
  134. }
  135. internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  136. {
  137. cancellationToken.ThrowIfCancellationRequested();
  138. var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
  139. var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  140. Directory.CreateDirectory(Path.GetDirectoryName(path));
  141. using (var httpResponse = await _httpClient.SendAsync(
  142. new HttpRequestOptions
  143. {
  144. Url = url,
  145. CancellationToken = cancellationToken
  146. },
  147. HttpMethod.Get).ConfigureAwait(false))
  148. using (var response = httpResponse.Content)
  149. using (var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
  150. {
  151. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  152. }
  153. }
  154. private static string GetAlbumDataPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
  155. {
  156. var dataPath = Path.Combine(GetAlbumDataPath(appPaths), musicBrainzReleaseGroupId);
  157. return dataPath;
  158. }
  159. private static string GetAlbumDataPath(IApplicationPaths appPaths)
  160. {
  161. var dataPath = Path.Combine(appPaths.CachePath, "audiodb-album");
  162. return dataPath;
  163. }
  164. internal static string GetAlbumInfoPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
  165. {
  166. var dataPath = GetAlbumDataPath(appPaths, musicBrainzReleaseGroupId);
  167. return Path.Combine(dataPath, "album.json");
  168. }
  169. public class Album
  170. {
  171. public string idAlbum { get; set; }
  172. public string idArtist { get; set; }
  173. public string strAlbum { get; set; }
  174. public string strArtist { get; set; }
  175. public string intYearReleased { get; set; }
  176. public string strGenre { get; set; }
  177. public string strSubGenre { get; set; }
  178. public string strReleaseFormat { get; set; }
  179. public string intSales { get; set; }
  180. public string strAlbumThumb { get; set; }
  181. public string strAlbumCDart { get; set; }
  182. public string strDescriptionEN { get; set; }
  183. public string strDescriptionDE { get; set; }
  184. public string strDescriptionFR { get; set; }
  185. public string strDescriptionCN { get; set; }
  186. public string strDescriptionIT { get; set; }
  187. public string strDescriptionJP { get; set; }
  188. public string strDescriptionRU { get; set; }
  189. public string strDescriptionES { get; set; }
  190. public string strDescriptionPT { get; set; }
  191. public string strDescriptionSE { get; set; }
  192. public string strDescriptionNL { get; set; }
  193. public string strDescriptionHU { get; set; }
  194. public string strDescriptionNO { get; set; }
  195. public string strDescriptionIL { get; set; }
  196. public string strDescriptionPL { get; set; }
  197. public object intLoved { get; set; }
  198. public object intScore { get; set; }
  199. public string strReview { get; set; }
  200. public object strMood { get; set; }
  201. public object strTheme { get; set; }
  202. public object strSpeed { get; set; }
  203. public object strLocation { get; set; }
  204. public string strMusicBrainzID { get; set; }
  205. public string strMusicBrainzArtistID { get; set; }
  206. public object strItunesID { get; set; }
  207. public object strAmazonID { get; set; }
  208. public string strLocked { get; set; }
  209. }
  210. public class RootObject
  211. {
  212. public List<Album> album { get; set; }
  213. }
  214. /// <inheritdoc />
  215. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  216. {
  217. throw new NotImplementedException();
  218. }
  219. }
  220. }