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