jellyfin/MediaBrowser.Providers/Music/FanArtArtistProvider.cs

336 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-05-13 00:57:51 +02:00
using System.Globalization;
using System.IO;
using System.Linq;
2015-10-29 16:47:05 +01:00
using System.Net;
2013-03-04 15:34:00 +01:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.IO;
2015-10-29 16:47:05 +01:00
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Providers;
2015-10-29 16:47:05 +01:00
using MediaBrowser.Model.Serialization;
using MediaBrowser.Providers.TV;
using MediaBrowser.Providers.TV.FanArt;
2013-03-04 15:34:00 +01:00
2013-06-09 18:47:28 +02:00
namespace MediaBrowser.Providers.Music
2013-03-04 15:34:00 +01:00
{
2016-05-25 02:42:12 +02:00
public class FanartArtistProvider : IRemoteImageProvider, IHasOrder
2013-03-04 15:34:00 +01:00
{
2019-01-09 18:24:16 +01:00
internal const string ApiKey = "184e1a2b1fe3b94935365411f919f638";
2016-04-21 07:13:33 +02:00
private const string FanArtBaseUrl = "https://webservice.fanart.tv/v3.1/music/{1}?api_key={0}";
2014-01-31 05:50:09 +01:00
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IServerConfigurationManager _config;
private readonly IHttpClient _httpClient;
private readonly IFileSystem _fileSystem;
2015-10-29 16:47:05 +01:00
private readonly IJsonSerializer _jsonSerializer;
2014-01-31 05:50:09 +01:00
internal static FanartArtistProvider Current;
2015-10-29 16:47:05 +01:00
public FanartArtistProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer jsonSerializer)
2013-03-04 15:34:00 +01:00
{
2014-01-31 05:50:09 +01:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
2015-10-29 16:47:05 +01:00
_jsonSerializer = jsonSerializer;
Current = this;
2013-03-04 15:34:00 +01:00
}
public string Name => ProviderName;
2013-03-04 15:34:00 +01:00
public static string ProviderName => "FanArt";
2014-01-31 05:50:09 +01:00
2018-09-12 19:26:21 +02:00
public bool Supports(BaseItem item)
2013-03-04 15:34:00 +01:00
{
2013-05-02 16:30:38 +02:00
return item is MusicArtist;
2013-03-04 15:34:00 +01:00
}
2018-09-12 19:26:21 +02:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
2014-01-31 05:50:09 +01:00
return new List<ImageType>
{
2019-01-08 00:27:46 +01:00
ImageType.Primary,
2014-01-31 05:50:09 +01:00
ImageType.Logo,
ImageType.Art,
ImageType.Banner,
ImageType.Backdrop
};
}
2018-09-12 19:26:21 +02:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-01-31 05:50:09 +01:00
{
var artist = (MusicArtist)item;
var list = new List<RemoteImageInfo>();
var artistMusicBrainzId = artist.GetProviderId(MetadataProviders.MusicBrainzArtist);
2014-01-31 05:50:09 +01:00
if (!string.IsNullOrEmpty(artistMusicBrainzId))
2014-01-31 05:50:09 +01:00
{
2015-10-29 16:47:05 +01:00
await EnsureArtistJson(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
2014-01-31 05:50:09 +01:00
2015-10-29 16:47:05 +01:00
var artistJsonPath = GetArtistJsonPath(_config.CommonApplicationPaths, artistMusicBrainzId);
2014-01-31 05:50:09 +01:00
try
{
2015-10-29 16:47:05 +01:00
AddImages(list, artistJsonPath, cancellationToken);
2014-01-31 05:50:09 +01:00
}
catch (FileNotFoundException)
{
2014-12-23 04:58:14 +01:00
}
2016-10-27 20:37:46 +02:00
catch (IOException)
2014-12-23 04:58:14 +01:00
{
2014-01-31 05:50:09 +01:00
}
}
2014-01-31 05:50:09 +01:00
var language = item.GetPreferredMetadataLanguage();
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
2014-01-31 05:50:09 +01:00
// 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))
2014-01-31 05:50:09 +01:00
{
return 3;
}
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
2014-01-31 05:50:09 +01:00
{
return 2;
}
}
if (string.IsNullOrEmpty(i.Language))
2014-01-31 05:50:09 +01:00
{
return isLanguageEn ? 3 : 2;
}
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
/// <summary>
2014-01-31 05:50:09 +01:00
/// Adds the images.
/// </summary>
2014-01-31 05:50:09 +01:00
/// <param name="list">The list.</param>
2015-10-29 16:47:05 +01:00
/// <param name="path">The path.</param>
2014-01-31 05:50:09 +01:00
/// <param name="cancellationToken">The cancellation token.</param>
2015-10-29 16:47:05 +01:00
private void AddImages(List<RemoteImageInfo> list, string path, CancellationToken cancellationToken)
{
2015-10-29 16:47:05 +01:00
var obj = _jsonSerializer.DeserializeFromFile<FanartArtistResponse>(path);
PopulateImages(list, obj.artistbackground, ImageType.Backdrop, 1920, 1080);
2015-10-31 19:52:52 +01:00
PopulateImages(list, obj.artistthumb, ImageType.Primary, 500, 281);
2015-10-29 16:47:05 +01: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 16:47:05 +01:00
private void PopulateImages(List<RemoteImageInfo> list,
List<FanartArtistImage> images,
ImageType type,
int width,
int height)
{
2015-10-29 16:47:05 +01:00
if (images == null)
{
2015-10-29 16:47:05 +01:00
return;
}
2014-01-31 05:50:09 +01:00
2015-10-29 16:47:05 +01:00
list.AddRange(images.Select(i =>
2013-04-29 01:39:17 +02:00
{
2015-10-29 16:47:05 +01:00
var url = i.url;
if (!string.IsNullOrEmpty(url))
2014-01-31 05:50:09 +01:00
{
2015-10-29 16:47:05 +01:00
var likesString = i.likes;
var info = new RemoteImageInfo
2014-01-31 05:50:09 +01:00
{
2015-10-29 16:47:05 +01:00
RatingType = RatingType.Likes,
Type = type,
Width = width,
Height = height,
ProviderName = Name,
2018-09-12 19:26:21 +02:00
Url = url.Replace("http://", "https://", StringComparison.OrdinalIgnoreCase),
2015-10-29 16:47:05 +01:00
Language = i.lang
};
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Integer, _usCulture, out var likes))
2015-10-29 16:47:05 +01:00
{
info.CommunityRating = likes;
2014-01-31 05:50:09 +01:00
}
2013-03-04 15:34:00 +01:00
2015-10-29 16:47:05 +01:00
return info;
2014-01-31 05:50:09 +01:00
}
2015-10-29 16:47:05 +01:00
return null;
}).Where(i => i != null));
}
public int Order => 0;
2014-01-31 05:50:09 +01:00
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
2014-01-31 05:50:09 +01:00
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
2016-10-30 01:49:23 +02:00
Url = url
2014-01-31 05:50:09 +01:00
});
}
2015-10-29 16:47:05 +01:00
internal Task EnsureArtistJson(string musicBrainzId, CancellationToken cancellationToken)
2014-01-31 05:50:09 +01:00
{
2015-10-29 16:47:05 +01:00
var jsonPath = GetArtistJsonPath(_config.ApplicationPaths, musicBrainzId);
2014-01-31 05:50:09 +01:00
2015-10-29 16:47:05 +01:00
var fileInfo = _fileSystem.GetFileSystemInfo(jsonPath);
2014-01-31 05:50:09 +01:00
if (fileInfo.Exists)
{
2018-09-12 19:26:21 +02:00
if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
2014-01-31 05:50:09 +01:00
{
2018-09-12 19:26:21 +02:00
return Task.CompletedTask;
2014-01-31 05:50:09 +01:00
}
}
2015-10-29 16:47:05 +01:00
return DownloadArtistJson(musicBrainzId, cancellationToken);
}
/// <summary>
2015-10-29 16:47:05 +01: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 16:47:05 +01:00
internal async Task DownloadArtistJson(string musicBrainzId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
2015-01-18 06:45:10 +01:00
var clientKey = FanartSeriesProvider.Current.GetFanartOptions().UserApiKey;
if (!string.IsNullOrWhiteSpace(clientKey))
{
2015-01-18 06:45:10 +01:00
url += "&client_key=" + clientKey;
}
2015-10-29 16:47:05 +01:00
var jsonPath = GetArtistJsonPath(_config.ApplicationPaths, musicBrainzId);
2014-01-31 05:50:09 +01:00
Directory.CreateDirectory(Path.GetDirectoryName(jsonPath));
2015-10-29 16:47:05 +01:00
try
{
2017-10-20 18:16:56 +02:00
using (var httpResponse = await _httpClient.SendAsync(new HttpRequestOptions
2015-10-29 16:47:05 +01:00
{
Url = url,
2016-10-31 19:39:41 +01:00
CancellationToken = cancellationToken,
BufferContent = true
2017-10-20 18:16:56 +02:00
}, "GET").ConfigureAwait(false))
2015-10-29 16:47:05 +01:00
{
2017-10-20 18:16:56 +02:00
using (var response = httpResponse.Content)
2015-10-29 16:47:05 +01:00
{
2017-10-20 18:16:56 +02:00
using (var saveFileStream = _fileSystem.GetFileStream(jsonPath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
{
await response.CopyToAsync(saveFileStream).ConfigureAwait(false);
}
2015-10-29 16:47:05 +01:00
}
}
}
catch (HttpException ex)
2013-04-29 01:39:17 +02:00
{
2015-10-29 16:47:05 +01:00
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
{
2015-10-29 16:47:05 +01:00
_jsonSerializer.SerializeToFile(new FanartArtistResponse(), jsonPath);
}
else
{
throw;
}
2013-04-29 01:39:17 +02:00
}
}
2013-06-19 15:54:45 +02:00
/// <summary>
2014-01-31 05:50:09 +01:00
/// Gets the artist data path.
/// </summary>
2014-01-31 05:50:09 +01: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 05:50:09 +01:00
var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
2013-03-04 15:34:00 +01:00
2014-01-31 05:50:09 +01:00
return dataPath;
}
2014-01-31 05:50:09 +01: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 05:50:09 +01:00
return dataPath;
2013-03-04 15:34:00 +01:00
}
2015-10-29 16:47:05 +01:00
internal static string GetArtistJsonPath(IApplicationPaths appPaths, string musicBrainzArtistId)
{
2014-01-31 05:50:09 +01:00
var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
2015-10-29 16:47:05 +01: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 15:34:00 +01:00
}
}