AudioDbArtistProvider.cs 9.7 KB

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