AlbumProvider.cs 9.9 KB

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