AudioDbAlbumProvider.cs 9.6 KB

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