jellyfin-server/MediaBrowser.Providers/Music/LastFmImageProvider.cs

168 lines
5.0 KiB
C#
Raw Normal View History

2014-01-31 19:55:21 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using System;
using System.Collections.Generic;
2014-01-31 19:55:21 +00:00
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Music
{
2014-01-31 19:55:21 +00:00
public class LastfmImageProvider : IRemoteImageProvider, IHasOrder
{
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
{
2014-01-31 19:55:21 +00:00
get { return "last.fm"; }
}
2014-01-31 19:55:21 +00:00
public bool Supports(IHasImages item)
{
2014-01-31 19:55:21 +00:00
return item is MusicAlbum || item is MusicArtist;
}
2014-01-31 19:55:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
2014-01-31 19:55:21 +00:00
return new List<ImageType>
{
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);
2014-01-31 19:55:21 +00:00
return images.Where(i => i.Type == imageType);
}
2014-01-31 19:55:21 +00:00
public Task<IEnumerable<RemoteImageInfo>> GetAllImages(IHasImages item, CancellationToken cancellationToken)
{
2014-01-31 19:55:21 +00:00
var list = new List<RemoteImageInfo>();
RemoteImageInfo info = null;
var musicBrainzId = item is MusicAlbum ?
item.GetProviderId(MetadataProviders.MusicBrainzAlbum) :
item.GetProviderId(MetadataProviders.MusicBrainzArtist);
2014-01-31 19:55:21 +00:00
if (!string.IsNullOrEmpty(musicBrainzId))
{
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('|');
2014-01-31 19:55:21 +00:00
info = GetInfo(parts.FirstOrDefault(), parts.LastOrDefault());
}
catch (DirectoryNotFoundException)
{
}
catch (FileNotFoundException)
{
}
}
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('|');
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));
}
2014-01-31 19:55:21 +00:00
private RemoteImageInfo GetInfo(string url, string size)
{
2014-01-31 19:55:21 +00:00
if (string.IsNullOrEmpty(url))
{
return null;
}
2014-01-31 19:55:21 +00:00
var info = new RemoteImageInfo
{
ProviderName = Name,
Url = url,
Type = ImageType.Primary
};
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))
{
}
2014-01-31 19:55:21 +00:00
return info;
}
public int Order
{
get { return 1; }
}
2014-01-31 19:55:21 +00:00
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
2014-01-31 19:55:21 +00:00
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = url,
ResourcePool = LastfmArtistProvider.LastfmResourcePool
});
}
}
}