AudioDbArtistProvider.cs 9.6 KB

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