jellyfin/MediaBrowser.Providers/Music/FanArtArtistProvider.cs

348 lines
12 KiB
C#
Raw Normal View History

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