AudioDbArtistProvider.cs 9.9 KB

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