AudioDbAlbumProvider.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 CommonIO;
  17. namespace MediaBrowser.Providers.Music
  18. {
  19. public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, 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 AudioDbAlbumProvider Current;
  26. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  27. public AudioDbAlbumProvider(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 async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
  36. {
  37. return new List<RemoteSearchResult>();
  38. }
  39. public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
  40. {
  41. var result = new MetadataResult<MusicAlbum>();
  42. var id = info.GetReleaseGroupId();
  43. if (!string.IsNullOrWhiteSpace(id))
  44. {
  45. await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
  46. var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
  47. var obj = _json.DeserializeFromFile<RootObject>(path);
  48. if (obj != null && obj.album != null && obj.album.Count > 0)
  49. {
  50. result.Item = new MusicAlbum();
  51. result.HasMetadata = true;
  52. ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
  53. }
  54. }
  55. return result;
  56. }
  57. private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
  58. {
  59. if (!string.IsNullOrWhiteSpace(result.strArtist))
  60. {
  61. item.AlbumArtists = new List<string> { result.strArtist };
  62. }
  63. if (!string.IsNullOrEmpty(result.intYearReleased))
  64. {
  65. item.ProductionYear = int.Parse(result.intYearReleased, _usCulture);
  66. }
  67. if (!string.IsNullOrEmpty(result.strGenre))
  68. {
  69. item.Genres = new List<string> { result.strGenre };
  70. }
  71. item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
  72. item.SetProviderId(MetadataProviders.AudioDbAlbum, result.idAlbum);
  73. item.SetProviderId(MetadataProviders.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
  74. item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, result.strMusicBrainzID);
  75. string overview = null;
  76. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  77. {
  78. overview = result.strDescriptionDE;
  79. }
  80. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  81. {
  82. overview = result.strDescriptionFR;
  83. }
  84. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  85. {
  86. overview = result.strDescriptionNL;
  87. }
  88. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  89. {
  90. overview = result.strDescriptionRU;
  91. }
  92. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  93. {
  94. overview = result.strDescriptionIT;
  95. }
  96. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  97. {
  98. overview = result.strDescriptionPT;
  99. }
  100. if (string.IsNullOrWhiteSpace(overview))
  101. {
  102. overview = result.strDescriptionEN;
  103. }
  104. item.Overview = (overview ?? string.Empty).StripHtml();
  105. }
  106. public string Name
  107. {
  108. get { return "TheAudioDB"; }
  109. }
  110. private readonly Task _cachedTask = Task.FromResult(true);
  111. internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  112. {
  113. var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  114. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  115. if (fileInfo.Exists)
  116. {
  117. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
  118. {
  119. return _cachedTask;
  120. }
  121. }
  122. return DownloadInfo(musicBrainzReleaseGroupId, cancellationToken);
  123. }
  124. internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
  125. {
  126. cancellationToken.ThrowIfCancellationRequested();
  127. var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
  128. var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
  129. _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
  130. using (var response = await _httpClient.Get(new HttpRequestOptions
  131. {
  132. Url = url,
  133. ResourcePool = AudioDbArtistProvider.Current.AudioDbResourcePool,
  134. CancellationToken = cancellationToken
  135. }).ConfigureAwait(false))
  136. {
  137. using (var xmlFileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.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. }