LastFmImageProvider.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
  45. {
  46. var list = new List<RemoteImageInfo>();
  47. RemoteImageInfo info = null;
  48. var musicBrainzId = item is MusicAlbum ?
  49. item.GetProviderId(MetadataProviders.MusicBrainzAlbum) :
  50. item.GetProviderId(MetadataProviders.MusicBrainzArtist);
  51. if (!string.IsNullOrEmpty(musicBrainzId))
  52. {
  53. var cachePath = Path.Combine(_config.ApplicationPaths.CachePath, "lastfm", musicBrainzId, "image.txt");
  54. try
  55. {
  56. var parts = File.ReadAllText(cachePath).Split('|');
  57. info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
  58. }
  59. catch (DirectoryNotFoundException)
  60. {
  61. }
  62. catch (FileNotFoundException)
  63. {
  64. }
  65. }
  66. if (info == null)
  67. {
  68. var musicBrainzReleaseGroupId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
  69. if (!string.IsNullOrEmpty(musicBrainzReleaseGroupId))
  70. {
  71. var cachePath = Path.Combine(_config.ApplicationPaths.CachePath, "lastfm", musicBrainzReleaseGroupId, "image.txt");
  72. try
  73. {
  74. var parts = File.ReadAllText(cachePath).Split('|');
  75. info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
  76. }
  77. catch (DirectoryNotFoundException)
  78. {
  79. }
  80. catch (FileNotFoundException)
  81. {
  82. }
  83. }
  84. }
  85. if (info != null)
  86. {
  87. list.Add(info);
  88. }
  89. // The only info we have is size
  90. return Task.FromResult<IEnumerable<RemoteImageInfo>>(list.OrderByDescending(i => i.Width ?? 0));
  91. }
  92. private RemoteImageInfo GetInfo(string url, string size)
  93. {
  94. if (string.IsNullOrEmpty(url))
  95. {
  96. return null;
  97. }
  98. var info = new RemoteImageInfo
  99. {
  100. ProviderName = Name,
  101. Url = url,
  102. Type = ImageType.Primary
  103. };
  104. if (string.Equals(size, "mega", StringComparison.OrdinalIgnoreCase))
  105. {
  106. }
  107. else if (string.Equals(size, "extralarge", StringComparison.OrdinalIgnoreCase))
  108. {
  109. }
  110. else if (string.Equals(size, "large", StringComparison.OrdinalIgnoreCase))
  111. {
  112. }
  113. else if (string.Equals(size, "medium", StringComparison.OrdinalIgnoreCase))
  114. {
  115. }
  116. return info;
  117. }
  118. public int Order
  119. {
  120. get
  121. {
  122. // After all others
  123. return 3;
  124. }
  125. }
  126. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  127. {
  128. return _httpClient.GetResponse(new HttpRequestOptions
  129. {
  130. CancellationToken = cancellationToken,
  131. Url = url,
  132. ResourcePool = LastfmArtistProvider.LastfmResourcePool
  133. });
  134. }
  135. }
  136. }