AlbumProvider.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text.Json;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Common.Configuration;
  12. using MediaBrowser.Common.Extensions;
  13. using MediaBrowser.Common.Json;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Controller.Configuration;
  16. using MediaBrowser.Controller.Entities.Audio;
  17. using MediaBrowser.Controller.Providers;
  18. using MediaBrowser.Model.Entities;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.Providers;
  21. using MediaBrowser.Model.Serialization;
  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.GetOptions();
  31. public static AudioDbAlbumProvider Current;
  32. public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpClientFactory)
  33. {
  34. _config = config;
  35. _fileSystem = fileSystem;
  36. _httpClientFactory = httpClientFactory;
  37. Current = this;
  38. }
  39. /// <inheritdoc />
  40. public string Name => "TheAudioDB";
  41. /// <inheritdoc />
  42. // After music brainz
  43. public int Order => 1;
  44. /// <inheritdoc />
  45. public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
  46. => Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
  47. /// <inheritdoc />
  48. public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
  49. {
  50. var result = new MetadataResult<MusicAlbum>();
  51. var id = info.GetReleaseGroupId();
  52. if (!string.IsNullOrWhiteSpace(id))
  53. {
  54. await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
  55. var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
  56. await using FileStream jsonStream = File.OpenRead(path);
  57. var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  58. if (obj != null && obj.album != null && obj.album.Count > 0)
  59. {
  60. result.Item = new MusicAlbum();
  61. result.HasMetadata = true;
  62. ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
  63. }
  64. }
  65. return result;
  66. }
  67. private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
  68. {
  69. if (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
  70. {
  71. item.Album = result.strAlbum;
  72. }
  73. if (!string.IsNullOrWhiteSpace(result.strArtist))
  74. {
  75. item.AlbumArtists = new string[] { result.strArtist };
  76. }
  77. if (!string.IsNullOrEmpty(result.intYearReleased))
  78. {
  79. item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
  80. }
  81. if (!string.IsNullOrEmpty(result.strGenre))
  82. {
  83. item.Genres = new[] { result.strGenre };
  84. }
  85. item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
  86. item.SetProviderId(MetadataProvider.AudioDbAlbum, result.idAlbum);
  87. item.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
  88. item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, result.strMusicBrainzID);
  89. string overview = null;
  90. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  91. {
  92. overview = result.strDescriptionDE;
  93. }
  94. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  95. {
  96. overview = result.strDescriptionFR;
  97. }
  98. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  99. {
  100. overview = result.strDescriptionNL;
  101. }
  102. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  103. {
  104. overview = result.strDescriptionRU;
  105. }
  106. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  107. {
  108. overview = result.strDescriptionIT;
  109. }
  110. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  111. {
  112. overview = result.strDescriptionPT;
  113. }
  114. if (string.IsNullOrWhiteSpace(overview))
  115. {
  116. overview = result.strDescriptionEN;
  117. }
  118. item.Overview = (overview ?? string.Empty).StripHtml();
  119. }
  120. internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  121. {
  122. var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  123. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  124. if (fileInfo.Exists)
  125. {
  126. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
  127. {
  128. return Task.CompletedTask;
  129. }
  130. }
  131. return DownloadInfo(musicBrainzReleaseGroupId, cancellationToken);
  132. }
  133. internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  134. {
  135. cancellationToken.ThrowIfCancellationRequested();
  136. var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
  137. var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  138. Directory.CreateDirectory(Path.GetDirectoryName(path));
  139. using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken).ConfigureAwait(false);
  140. await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  141. await using var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true);
  142. await stream.CopyToAsync(xmlFileStream, cancellationToken).ConfigureAwait(false);
  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<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  206. {
  207. throw new NotImplementedException();
  208. }
  209. }
  210. }