AudioDbAlbumProvider.cs 10 KB

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