2014-01-31 19:55:21 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-09-06 15:23:20 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-11-06 16:29:20 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-09-06 15:23:20 +00:00
|
|
|
|
using System;
|
2013-11-06 16:29:20 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-01-31 19:55:21 +00:00
|
|
|
|
using System.IO;
|
2013-11-06 16:29:20 +00:00
|
|
|
|
using System.Linq;
|
2013-09-06 15:23:20 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Music
|
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public class LastfmImageProvider : IRemoteImageProvider, IHasOrder
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
|
|
|
|
|
public LastfmImageProvider(IHttpClient httpClient, IServerConfigurationManager config)
|
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_config = config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ProviderName; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string ProviderName
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
get { return "last.fm"; }
|
2013-09-06 15:23:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public bool Supports(IHasImages item)
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return item is MusicAlbum || item is MusicArtist;
|
2013-09-06 15:23:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return new List<ImageType>
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
ImageType.Primary
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var images = await GetAllImages(item, cancellationToken).ConfigureAwait(false);
|
2013-09-06 15:23:20 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return images.Where(i => i.Type == imageType);
|
2013-09-06 15:23:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
|
|
|
|
RemoteImageInfo info = null;
|
|
|
|
|
|
|
|
|
|
var musicBrainzId = item.GetProviderId(MetadataProviders.Musicbrainz);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(musicBrainzId))
|
2013-11-07 15:57:12 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
var cachePath = Path.Combine(_config.ApplicationPaths.CachePath, "lastfm", musicBrainzId, "image.txt");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var parts = File.ReadAllText(cachePath).Split('|');
|
2013-09-06 15:23:20 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
2013-11-07 15:57:12 +00:00
|
|
|
|
}
|
2013-09-06 15:23:20 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
if (info == null)
|
|
|
|
|
{
|
|
|
|
|
var musicBrainzReleaseGroupId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(musicBrainzReleaseGroupId))
|
|
|
|
|
{
|
|
|
|
|
var cachePath = Path.Combine(_config.ApplicationPaths.CachePath, "lastfm", musicBrainzReleaseGroupId, "image.txt");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var parts = File.ReadAllText(cachePath).Split('|');
|
2013-11-06 16:29:20 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (info != null)
|
|
|
|
|
{
|
|
|
|
|
list.Add(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The only info we have is size
|
|
|
|
|
return Task.FromResult<IEnumerable<RemoteImageInfo>>(list.OrderByDescending(i => i.Width ?? 0));
|
2013-09-06 15:23:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
private RemoteImageInfo GetInfo(string url, string size)
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-09-06 15:23:20 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
var info = new RemoteImageInfo
|
|
|
|
|
{
|
|
|
|
|
ProviderName = Name,
|
|
|
|
|
Url = url,
|
|
|
|
|
Type = ImageType.Primary
|
|
|
|
|
};
|
2013-09-06 15:23:20 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
if (string.Equals(size, "mega", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(size, "extralarge", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(size, "large", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (string.Equals(size, "medium", StringComparison.OrdinalIgnoreCase))
|
2013-09-06 15:23:20 +00:00
|
|
|
|
{
|
|
|
|
|
|
2013-09-06 15:38:22 +00:00
|
|
|
|
}
|
2014-01-31 19:55:21 +00:00
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Order
|
|
|
|
|
{
|
|
|
|
|
get { return 1; }
|
2013-11-06 16:29:20 +00:00
|
|
|
|
}
|
2013-09-06 15:38:22 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
2013-11-06 16:29:20 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = LastfmArtistProvider.LastfmResourcePool
|
|
|
|
|
});
|
2013-09-06 15:23:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|