LastFmImageProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Audio;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Providers;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Providers.Music
  15. {
  16. public class LastfmImageProvider : IRemoteImageProvider, IHasOrder
  17. {
  18. private readonly IHttpClient _httpClient;
  19. private readonly IServerConfigurationManager _config;
  20. public LastfmImageProvider(IHttpClient httpClient, IServerConfigurationManager config)
  21. {
  22. _httpClient = httpClient;
  23. _config = config;
  24. }
  25. public string Name
  26. {
  27. get { return ProviderName; }
  28. }
  29. public static string ProviderName
  30. {
  31. get { return "last.fm"; }
  32. }
  33. public bool Supports(IHasImages item)
  34. {
  35. return item is MusicAlbum || item is MusicArtist;
  36. }
  37. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  38. {
  39. return new List<ImageType>
  40. {
  41. ImageType.Primary
  42. };
  43. }
  44. public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
  45. {
  46. var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false);
  47. return images.Where(i => i.Type == imageType);
  48. }
  49. public Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
  50. {
  51. var list = new List<RemoteImageInfo>();
  52. RemoteImageInfo info = null;
  53. var musicBrainzId = item is MusicAlbum ?
  54. item.GetProviderId(MetadataProviders.MusicBrainzAlbum) :
  55. item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  56. if (!string.IsNullOrEmpty(musicBrainzId))
  57. {
  58. var cachePath = Path.Combine(_config.ApplicationPaths.CachePath, "lastfm", musicBrainzId, "image.txt");
  59. try
  60. {
  61. var parts = File.ReadAllText(cachePath).Split('|');
  62. info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
  63. }
  64. catch (DirectoryNotFoundException)
  65. {
  66. }
  67. catch (FileNotFoundException)
  68. {
  69. }
  70. }
  71. if (info == null)
  72. {
  73. var musicBrainzReleaseGroupId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  74. if (!string.IsNullOrEmpty(musicBrainzReleaseGroupId))
  75. {
  76. var cachePath = Path.Combine(_config.ApplicationPaths.CachePath, "lastfm", musicBrainzReleaseGroupId, "image.txt");
  77. try
  78. {
  79. var parts = File.ReadAllText(cachePath).Split('|');
  80. info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
  81. }
  82. catch (DirectoryNotFoundException)
  83. {
  84. }
  85. catch (FileNotFoundException)
  86. {
  87. }
  88. }
  89. }
  90. if (info != null)
  91. {
  92. list.Add(info);
  93. }
  94. // The only info we have is size
  95. return Task.FromResult<IEnumerable<RemoteImageInfo>>(list.OrderByDescending(i => i.Width ?? 0));
  96. }
  97. private RemoteImageInfo GetInfo(string url, string size)
  98. {
  99. if (string.IsNullOrEmpty(url))
  100. {
  101. return null;
  102. }
  103. var info = new RemoteImageInfo
  104. {
  105. ProviderName = Name,
  106. Url = url,
  107. Type = ImageType.Primary
  108. };
  109. if (string.Equals(size, "mega", StringComparison.OrdinalIgnoreCase))
  110. {
  111. }
  112. else if (string.Equals(size, "extralarge", StringComparison.OrdinalIgnoreCase))
  113. {
  114. }
  115. else if (string.Equals(size, "large", StringComparison.OrdinalIgnoreCase))
  116. {
  117. }
  118. else if (string.Equals(size, "medium", StringComparison.OrdinalIgnoreCase))
  119. {
  120. }
  121. return info;
  122. }
  123. public int Order
  124. {
  125. get { return 1; }
  126. }
  127. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  128. {
  129. return _httpClient.GetResponse(new HttpRequestOptions
  130. {
  131. CancellationToken = cancellationToken,
  132. Url = url,
  133. ResourcePool = LastfmArtistProvider.LastfmResourcePool
  134. });
  135. }
  136. }
  137. }