ManualLastFmImageProvider.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Entities.Audio;
  4. using MediaBrowser.Controller.Providers;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Providers;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Providers.Music
  13. {
  14. public class ManualLastFmImageProvider : IRemoteImageProvider
  15. {
  16. private readonly IHttpClient _httpClient;
  17. public ManualLastFmImageProvider(IHttpClient httpClient)
  18. {
  19. _httpClient = httpClient;
  20. }
  21. public string Name
  22. {
  23. get { return ProviderName; }
  24. }
  25. public static string ProviderName
  26. {
  27. get { return "last.fm"; }
  28. }
  29. public bool Supports(IHasImages item)
  30. {
  31. return item is MusicAlbum || item is MusicArtist;
  32. }
  33. public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
  34. {
  35. return new List<ImageType>
  36. {
  37. ImageType.Primary
  38. };
  39. }
  40. public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
  41. {
  42. var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false);
  43. return images.Where(i => i.Type == imageType);
  44. }
  45. public Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
  46. {
  47. var list = new List<RemoteImageInfo>();
  48. RemoteImageInfo info = null;
  49. var album = item as MusicAlbum;
  50. if (album != null)
  51. {
  52. info = GetInfo(album.LastFmImageUrl, album.LastFmImageSize);
  53. }
  54. var musicArtist = item as MusicArtist;
  55. if (musicArtist != null)
  56. {
  57. info = GetInfo(musicArtist.LastFmImageUrl, musicArtist.LastFmImageSize);
  58. }
  59. if (info != null)
  60. {
  61. list.Add(info);
  62. }
  63. // The only info we have is size
  64. return Task.FromResult<IEnumerable<RemoteImageInfo>>(list.OrderByDescending(i => i.Width ?? 0));
  65. }
  66. private RemoteImageInfo GetInfo(string url, string size)
  67. {
  68. if (string.IsNullOrEmpty(url))
  69. {
  70. return null;
  71. }
  72. var info = new RemoteImageInfo
  73. {
  74. ProviderName = Name,
  75. Url = url,
  76. Type = ImageType.Primary
  77. };
  78. if (string.Equals(size, "mega", StringComparison.OrdinalIgnoreCase))
  79. {
  80. }
  81. else if (string.Equals(size, "extralarge", StringComparison.OrdinalIgnoreCase))
  82. {
  83. }
  84. else if (string.Equals(size, "large", StringComparison.OrdinalIgnoreCase))
  85. {
  86. }
  87. else if (string.Equals(size, "medium", StringComparison.OrdinalIgnoreCase))
  88. {
  89. }
  90. return info;
  91. }
  92. public int Order
  93. {
  94. get { return 1; }
  95. }
  96. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  97. {
  98. return _httpClient.GetResponse(new HttpRequestOptions
  99. {
  100. CancellationToken = cancellationToken,
  101. Url = url,
  102. ResourcePool = LastfmBaseProvider.LastfmResourcePool
  103. });
  104. }
  105. }
  106. }