ArtistProvider.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Common.Extensions;
  10. using MediaBrowser.Common.Net;
  11. using MediaBrowser.Controller.Configuration;
  12. using MediaBrowser.Controller.Entities.Audio;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Providers;
  17. using MediaBrowser.Model.Serialization;
  18. using MediaBrowser.Providers.Music;
  19. namespace MediaBrowser.Providers.Plugins.AudioDb
  20. {
  21. public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
  22. {
  23. private readonly IServerConfigurationManager _config;
  24. private readonly IFileSystem _fileSystem;
  25. private readonly IHttpClient _httpClient;
  26. private readonly IJsonSerializer _json;
  27. public static AudioDbArtistProvider Current;
  28. private const string ApiKey = "195003";
  29. public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey;
  30. public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)
  31. {
  32. _config = config;
  33. _fileSystem = fileSystem;
  34. _httpClient = httpClient;
  35. _json = json;
  36. Current = this;
  37. }
  38. /// <inheritdoc />
  39. public string Name => "TheAudioDB";
  40. /// <inheritdoc />
  41. // After musicbrainz
  42. public int Order => 1;
  43. /// <inheritdoc />
  44. public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
  45. => Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
  46. /// <inheritdoc />
  47. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
  48. {
  49. var result = new MetadataResult<MusicArtist>();
  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.GetMusicBrainzArtistId();
  56. if (!string.IsNullOrWhiteSpace(id))
  57. {
  58. await EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
  59. var path = GetArtistInfoPath(_config.ApplicationPaths, id);
  60. var obj = _json.DeserializeFromFile<RootObject>(path);
  61. if (obj != null && obj.artists != null && obj.artists.Count > 0)
  62. {
  63. result.Item = new MusicArtist();
  64. result.HasMetadata = true;
  65. ProcessResult(result.Item, obj.artists[0], info.MetadataLanguage);
  66. }
  67. }
  68. return result;
  69. }
  70. private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage)
  71. {
  72. //item.HomePageUrl = result.strWebsite;
  73. if (!string.IsNullOrEmpty(result.strGenre))
  74. {
  75. item.Genres = new[] { result.strGenre };
  76. }
  77. item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
  78. item.SetProviderId(MetadataProvider.MusicBrainzArtist, result.strMusicBrainzID);
  79. string overview = null;
  80. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  81. {
  82. overview = result.strBiographyDE;
  83. }
  84. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  85. {
  86. overview = result.strBiographyFR;
  87. }
  88. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  89. {
  90. overview = result.strBiographyNL;
  91. }
  92. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  93. {
  94. overview = result.strBiographyRU;
  95. }
  96. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  97. {
  98. overview = result.strBiographyIT;
  99. }
  100. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  101. {
  102. overview = result.strBiographyPT;
  103. }
  104. if (string.IsNullOrWhiteSpace(overview))
  105. {
  106. overview = result.strBiographyEN;
  107. }
  108. item.Overview = (overview ?? string.Empty).StripHtml();
  109. }
  110. internal Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  111. {
  112. var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  113. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  114. if (fileInfo.Exists
  115. && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
  116. {
  117. return Task.CompletedTask;
  118. }
  119. return DownloadArtistInfo(musicBrainzId, cancellationToken);
  120. }
  121. internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  122. {
  123. cancellationToken.ThrowIfCancellationRequested();
  124. var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId;
  125. var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  126. using (var httpResponse = await _httpClient.SendAsync(
  127. new HttpRequestOptions
  128. {
  129. Url = url,
  130. CancellationToken = cancellationToken,
  131. BufferContent = true
  132. },
  133. HttpMethod.Get).ConfigureAwait(false))
  134. using (var response = httpResponse.Content)
  135. {
  136. Directory.CreateDirectory(Path.GetDirectoryName(path));
  137. using (var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
  138. {
  139. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// Gets the artist data path.
  145. /// </summary>
  146. /// <param name="appPaths">The application paths.</param>
  147. /// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
  148. /// <returns>System.String.</returns>
  149. private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  150. => Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  151. /// <summary>
  152. /// Gets the artist data path.
  153. /// </summary>
  154. /// <param name="appPaths">The application paths.</param>
  155. /// <returns>System.String.</returns>
  156. private static string GetArtistDataPath(IApplicationPaths appPaths)
  157. => Path.Combine(appPaths.CachePath, "audiodb-artist");
  158. internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  159. {
  160. var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
  161. return Path.Combine(dataPath, "artist.json");
  162. }
  163. public class Artist
  164. {
  165. public string idArtist { get; set; }
  166. public string strArtist { get; set; }
  167. public string strArtistAlternate { get; set; }
  168. public object idLabel { get; set; }
  169. public string intFormedYear { get; set; }
  170. public string intBornYear { get; set; }
  171. public object intDiedYear { get; set; }
  172. public object strDisbanded { get; set; }
  173. public string strGenre { get; set; }
  174. public string strSubGenre { get; set; }
  175. public string strWebsite { get; set; }
  176. public string strFacebook { get; set; }
  177. public string strTwitter { get; set; }
  178. public string strBiographyEN { get; set; }
  179. public string strBiographyDE { get; set; }
  180. public string strBiographyFR { get; set; }
  181. public string strBiographyCN { get; set; }
  182. public string strBiographyIT { get; set; }
  183. public string strBiographyJP { get; set; }
  184. public string strBiographyRU { get; set; }
  185. public string strBiographyES { get; set; }
  186. public string strBiographyPT { get; set; }
  187. public string strBiographySE { get; set; }
  188. public string strBiographyNL { get; set; }
  189. public string strBiographyHU { get; set; }
  190. public string strBiographyNO { get; set; }
  191. public string strBiographyIL { get; set; }
  192. public string strBiographyPL { get; set; }
  193. public string strGender { get; set; }
  194. public string intMembers { get; set; }
  195. public string strCountry { get; set; }
  196. public string strCountryCode { get; set; }
  197. public string strArtistThumb { get; set; }
  198. public string strArtistLogo { get; set; }
  199. public string strArtistFanart { get; set; }
  200. public string strArtistFanart2 { get; set; }
  201. public string strArtistFanart3 { get; set; }
  202. public string strArtistBanner { get; set; }
  203. public string strMusicBrainzID { get; set; }
  204. public object strLastFMChart { get; set; }
  205. public string strLocked { get; set; }
  206. }
  207. public class RootObject
  208. {
  209. public List<Artist> artists { get; set; }
  210. }
  211. /// <inheritdoc />
  212. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  213. {
  214. throw new NotImplementedException();
  215. }
  216. }
  217. }