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

367 lines
13 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
2013-03-04 15:13:03 +00:00
using MediaBrowser.Controller.Configuration;
2013-03-04 14:34:00 +00:00
using MediaBrowser.Controller.Entities;
2013-05-02 14:30:38 +00:00
using MediaBrowser.Controller.Entities.Audio;
2013-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
2014-01-31 04:50:09 +00:00
using MediaBrowser.Model.Dto;
2013-03-04 14:34:00 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2015-06-04 20:27:46 +00:00
using MediaBrowser.Providers.TV;
2013-03-04 14:34:00 +00:00
using System;
using System.Collections.Generic;
2013-05-12 22:57:51 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
2015-10-29 15:47:05 +00:00
using System.Net;
2013-03-04 14:34:00 +00:00
using System.Threading;
using System.Threading.Tasks;
2015-10-04 04:23:11 +00:00
using CommonIO;
2015-10-29 15:47:05 +00:00
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Serialization;
2013-03-04 14:34:00 +00:00
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.Music
2013-03-04 14:34:00 +00:00
{
2016-04-27 20:52:09 +00:00
public class FanartArtistProvider : IRemoteImageProvider, IHasItemChangeMonitor, IHasOrder
2013-03-04 14:34:00 +00:00
{
internal readonly SemaphoreSlim FanArtResourcePool = new SemaphoreSlim(3, 3);
2014-01-31 04:50:09 +00:00
internal const string ApiKey = "5c6b04c68e904cfed1e6cbc9a9e683d4";
2016-04-21 05:13:33 +00:00
private const string FanArtBaseUrl = "https://webservice.fanart.tv/v3.1/music/{1}?api_key={0}";
2014-01-31 04:50:09 +00:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
2015-10-29 15:47:05 +00:00
private readonly IJsonSerializer _jsonSerializer;
2014-01-31 04:50:09 +00:00
internal static FanartArtistProvider Current;
2015-10-29 15:47:05 +00:00
public FanartArtistProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer jsonSerializer)
2013-03-04 14:34:00 +00:00
{
2014-01-31 04:50:09 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
2015-10-29 15:47:05 +00:00
_jsonSerializer = jsonSerializer;
Current = this;
2013-03-04 14:34:00 +00:00
}
2014-01-31 04:50:09 +00:00
public string Name
{
get { return ProviderName; }
}
2013-03-04 14:34:00 +00:00
2014-01-31 04:50:09 +00:00
public static string ProviderName
{
2014-01-31 04:50:09 +00:00
get { return "FanArt"; }
}
2014-01-31 04:50:09 +00:00
public bool Supports(IHasImages item)
2013-03-04 14:34:00 +00:00
{
2013-05-02 14:30:38 +00:00
return item is MusicArtist;
2013-03-04 14:34:00 +00:00
}
2014-01-31 04:50:09 +00:00
public IEnumerable<ImageType> GetSupportedImages(IHasImages item)
{
2014-01-31 04:50:09 +00:00
return new List<ImageType>
{
2014-01-31 04:50:09 +00:00
ImageType.Primary,
ImageType.Logo,
ImageType.Art,
ImageType.Banner,
ImageType.Backdrop
};
}
2014-02-13 05:11:54 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
2014-01-31 04:50:09 +00:00
{
var artist = (MusicArtist)item;
var list = new List<RemoteImageInfo>();
var artistMusicBrainzId = artist.GetProviderId(MetadataProviders.MusicBrainzArtist);
2014-01-31 04:50:09 +00:00
if (!String.IsNullOrEmpty(artistMusicBrainzId))
{
2015-10-29 15:47:05 +00:00
await EnsureArtistJson(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
2014-01-31 04:50:09 +00:00
2015-10-29 15:47:05 +00:00
var artistJsonPath = GetArtistJsonPath(_config.CommonApplicationPaths, artistMusicBrainzId);
2014-01-31 04:50:09 +00:00
try
{
2015-10-29 15:47:05 +00:00
AddImages(list, artistJsonPath, cancellationToken);
2014-01-31 04:50:09 +00:00
}
catch (FileNotFoundException)
{
2014-12-23 03:58:14 +00:00
}
catch (DirectoryNotFoundException)
{
2014-01-31 04:50:09 +00:00
}
}
2014-01-31 04:50:09 +00:00
var language = item.GetPreferredMetadataLanguage();
var isLanguageEn = String.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
// Sort first by width to prioritize HD versions
return list.OrderByDescending(i => i.Width ?? 0)
.ThenByDescending(i =>
{
if (String.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
if (!isLanguageEn)
{
if (String.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
return 2;
}
}
if (String.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
/// <summary>
2014-01-31 04:50:09 +00:00
/// Adds the images.
/// </summary>
2014-01-31 04:50:09 +00:00
/// <param name="list">The list.</param>
2015-10-29 15:47:05 +00:00
/// <param name="path">The path.</param>
2014-01-31 04:50:09 +00:00
/// <param name="cancellationToken">The cancellation token.</param>
2015-10-29 15:47:05 +00:00
private void AddImages(List<RemoteImageInfo> list, string path, CancellationToken cancellationToken)
{
2015-10-29 15:47:05 +00:00
var obj = _jsonSerializer.DeserializeFromFile<FanartArtistResponse>(path);
PopulateImages(list, obj.artistbackground, ImageType.Backdrop, 1920, 1080);
2015-10-31 18:52:52 +00:00
PopulateImages(list, obj.artistthumb, ImageType.Primary, 500, 281);
2015-10-29 15:47:05 +00:00
PopulateImages(list, obj.hdmusiclogo, ImageType.Logo, 800, 310);
PopulateImages(list, obj.musicbanner, ImageType.Banner, 1000, 185);
PopulateImages(list, obj.musiclogo, ImageType.Logo, 400, 155);
PopulateImages(list, obj.hdmusicarts, ImageType.Art, 1000, 562);
PopulateImages(list, obj.musicarts, ImageType.Art, 500, 281);
}
2015-10-29 15:47:05 +00:00
private void PopulateImages(List<RemoteImageInfo> list,
List<FanartArtistImage> images,
ImageType type,
int width,
int height)
{
2015-10-29 15:47:05 +00:00
if (images == null)
{
2015-10-29 15:47:05 +00:00
return;
}
2014-01-31 04:50:09 +00:00
2015-10-29 15:47:05 +00:00
list.AddRange(images.Select(i =>
2013-04-28 23:39:17 +00:00
{
2015-10-29 15:47:05 +00:00
var url = i.url;
if (!string.IsNullOrEmpty(url))
2014-01-31 04:50:09 +00:00
{
2015-10-29 15:47:05 +00:00
var likesString = i.likes;
int likes;
var info = new RemoteImageInfo
2014-01-31 04:50:09 +00:00
{
2015-10-29 15:47:05 +00:00
RatingType = RatingType.Likes,
Type = type,
Width = width,
Height = height,
ProviderName = Name,
Url = url,
Language = i.lang
};
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
{
info.CommunityRating = likes;
2014-01-31 04:50:09 +00:00
}
2013-03-04 14:34:00 +00:00
2015-10-29 15:47:05 +00:00
return info;
2014-01-31 04:50:09 +00:00
}
2015-10-29 15:47:05 +00:00
return null;
}).Where(i => i != null));
}
2014-01-31 04:50:09 +00:00
public int Order
{
2014-01-31 04:50:09 +00:00
get { return 0; }
}
2014-01-31 04:50:09 +00:00
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
2014-01-31 04:50:09 +00:00
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
Url = url,
ResourcePool = FanArtResourcePool
});
}
2016-04-27 20:52:09 +00:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
2013-03-04 14:34:00 +00:00
{
2015-01-18 05:45:10 +00:00
var options = FanartSeriesProvider.Current.GetFanartOptions();
if (!options.EnableAutomaticUpdates)
{
return false;
}
var id = item.GetProviderId(MetadataProviders.MusicBrainzArtist);
2013-03-04 14:34:00 +00:00
2014-01-31 04:50:09 +00:00
if (!String.IsNullOrEmpty(id))
{
// Process images
2015-10-29 15:47:05 +00:00
var artistJsonPath = GetArtistJsonPath(_config.CommonApplicationPaths, id);
2015-10-29 15:47:05 +00:00
var fileInfo = _fileSystem.GetFileInfo(artistJsonPath);
2016-04-27 20:52:09 +00:00
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > item.DateLastRefreshed;
}
2014-01-31 04:50:09 +00:00
return false;
}
2014-02-03 17:44:13 +00:00
private readonly Task _cachedTask = Task.FromResult(true);
2015-10-29 15:47:05 +00:00
internal Task EnsureArtistJson(string musicBrainzId, CancellationToken cancellationToken)
2014-01-31 04:50:09 +00:00
{
2015-10-29 15:47:05 +00:00
var jsonPath = GetArtistJsonPath(_config.ApplicationPaths, musicBrainzId);
2014-01-31 04:50:09 +00:00
2015-10-29 15:47:05 +00:00
var fileInfo = _fileSystem.GetFileSystemInfo(jsonPath);
2014-01-31 04:50:09 +00:00
if (fileInfo.Exists)
{
2014-02-19 18:50:37 +00:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
2014-01-31 04:50:09 +00:00
{
2014-02-03 17:44:13 +00:00
return _cachedTask;
2014-01-31 04:50:09 +00:00
}
}
2015-10-29 15:47:05 +00:00
return DownloadArtistJson(musicBrainzId, cancellationToken);
}
/// <summary>
2015-10-29 15:47:05 +00:00
/// Downloads the artist data.
/// </summary>
/// <param name="musicBrainzId">The music brainz id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
2015-10-29 15:47:05 +00:00
internal async Task DownloadArtistJson(string musicBrainzId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
2015-01-18 05:45:10 +00:00
var clientKey = FanartSeriesProvider.Current.GetFanartOptions().UserApiKey;
if (!string.IsNullOrWhiteSpace(clientKey))
{
2015-01-18 05:45:10 +00:00
url += "&client_key=" + clientKey;
}
2015-10-29 15:47:05 +00:00
var jsonPath = GetArtistJsonPath(_config.ApplicationPaths, musicBrainzId);
2014-01-31 04:50:09 +00:00
2015-10-29 15:47:05 +00:00
_fileSystem.CreateDirectory(Path.GetDirectoryName(jsonPath));
2015-10-29 15:47:05 +00:00
try
{
2015-10-29 15:47:05 +00:00
using (var response = await _httpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = FanArtResourcePool,
CancellationToken = cancellationToken
2015-10-29 15:47:05 +00:00
}).ConfigureAwait(false))
{
using (var saveFileStream = _fileSystem.GetFileStream(jsonPath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
{
await response.CopyToAsync(saveFileStream).ConfigureAwait(false);
}
}
}
catch (HttpException ex)
2013-04-28 23:39:17 +00:00
{
2015-10-29 15:47:05 +00:00
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
{
2015-10-29 15:47:05 +00:00
_jsonSerializer.SerializeToFile(new FanartArtistResponse(), jsonPath);
}
else
{
throw;
}
2013-04-28 23:39:17 +00:00
}
}
2013-06-19 13:54:45 +00:00
/// <summary>
2014-01-31 04:50:09 +00:00
/// Gets the artist data path.
/// </summary>
2014-01-31 04:50:09 +00:00
/// <param name="appPaths">The application paths.</param>
/// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
/// <returns>System.String.</returns>
private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
{
2014-01-31 04:50:09 +00:00
var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
2013-03-04 14:34:00 +00:00
2014-01-31 04:50:09 +00:00
return dataPath;
}
2014-01-31 04:50:09 +00:00
/// <summary>
/// Gets the artist data path.
/// </summary>
/// <param name="appPaths">The application paths.</param>
/// <returns>System.String.</returns>
internal static string GetArtistDataPath(IApplicationPaths appPaths)
{
var dataPath = Path.Combine(appPaths.CachePath, "fanart-music");
2014-01-31 04:50:09 +00:00
return dataPath;
2013-03-04 14:34:00 +00:00
}
2015-10-29 15:47:05 +00:00
internal static string GetArtistJsonPath(IApplicationPaths appPaths, string musicBrainzArtistId)
{
2014-01-31 04:50:09 +00:00
var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
2015-10-29 15:47:05 +00:00
return Path.Combine(dataPath, "fanart.json");
}
public class FanartArtistImage
{
public string id { get; set; }
public string url { get; set; }
public string likes { get; set; }
public string disc { get; set; }
public string size { get; set; }
public string lang { get; set; }
}
public class Album
{
public string release_group_id { get; set; }
public List<FanartArtistImage> cdart { get; set; }
public List<FanartArtistImage> albumcover { get; set; }
}
public class FanartArtistResponse
{
public string name { get; set; }
public string mbid_id { get; set; }
public List<FanartArtistImage> artistthumb { get; set; }
public List<FanartArtistImage> artistbackground { get; set; }
public List<FanartArtistImage> hdmusiclogo { get; set; }
public List<FanartArtistImage> musicbanner { get; set; }
public List<FanartArtistImage> musiclogo { get; set; }
public List<FanartArtistImage> musicarts { get; set; }
public List<FanartArtistImage> hdmusicarts { get; set; }
public List<Album> albums { get; set; }
}
2013-03-04 14:34:00 +00:00
}
}