jellyfin/MediaBrowser.Providers/Music/FanArtArtistProvider.cs

474 lines
17 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
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;
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;
2014-01-31 04:50:09 +00:00
using System.Text;
2013-03-04 14:34:00 +00:00
using System.Threading;
using System.Threading.Tasks;
2014-01-31 04:50:09 +00:00
using System.Xml;
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
{
2014-01-31 19:55:21 +00:00
public class FanartArtistProvider : IRemoteImageProvider, IHasChangeMonitor, 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";
private const string FanArtBaseUrl = "http://api.fanart.tv/webservice/artist/{0}/{1}/xml/all/1/1";
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;
2014-01-31 04:50:09 +00:00
internal static FanartArtistProvider Current;
public FanartArtistProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
2013-03-04 14:34:00 +00:00
{
2014-01-31 04:50:09 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
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))
{
await EnsureMovieXml(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
var artistXmlPath = GetArtistXmlPath(_config.CommonApplicationPaths, artistMusicBrainzId);
try
{
AddImages(list, artistXmlPath, cancellationToken);
}
catch (FileNotFoundException)
{
}
}
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>
/// <param name="xmlPath">The XML path.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private void AddImages(List<RemoteImageInfo> list, string xmlPath, CancellationToken cancellationToken)
{
2014-01-31 04:50:09 +00:00
using (var streamReader = new StreamReader(xmlPath, Encoding.UTF8))
{
2014-01-31 04:50:09 +00:00
// Use XmlReader for best performance
using (var reader = XmlReader.Create(streamReader, new XmlReaderSettings
{
CheckCharacters = false,
IgnoreProcessingInstructions = true,
IgnoreComments = true,
ValidationType = ValidationType.None
}))
{
reader.MoveToContent();
// Loop through each element
while (reader.Read())
{
cancellationToken.ThrowIfCancellationRequested();
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "music":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromMusicNode(list, subReader, cancellationToken);
}
break;
}
default:
reader.Skip();
break;
}
}
}
}
}
}
2014-01-31 04:50:09 +00:00
/// <summary>
/// Adds the images from music node.
/// </summary>
/// <param name="list">The list.</param>
/// <param name="reader">The reader.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private void AddImagesFromMusicNode(List<RemoteImageInfo> list, XmlReader reader, CancellationToken cancellationToken)
{
2014-01-31 04:50:09 +00:00
reader.MoveToContent();
while (reader.Read())
{
2014-01-31 04:50:09 +00:00
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "hdmusiclogos":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Logo, 800, 310, subReader, cancellationToken);
}
break;
}
case "musiclogos":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Logo, 400, 155, subReader, cancellationToken);
}
break;
}
case "artistbackgrounds":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Backdrop, 1920, 1080, subReader, cancellationToken);
}
break;
}
case "hdmusicarts":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Art, 1000, 562, subReader, cancellationToken);
}
break;
}
case "musicarts":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Art, 500, 281, subReader, cancellationToken);
}
break;
}
case "hdmusicbanners":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Banner, 1000, 185, subReader, cancellationToken);
}
break;
}
case "musicbanners":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Banner, 1000, 185, subReader, cancellationToken);
}
break;
}
case "artistthumbs":
{
using (var subReader = reader.ReadSubtree())
{
AddImagesFromImageTypeNode(list, ImageType.Primary, 1000, 1000, subReader, cancellationToken);
}
break;
}
default:
{
using (reader.ReadSubtree())
{
}
break;
}
}
}
}
}
2013-03-04 14:34:00 +00:00
/// <summary>
2014-01-31 04:50:09 +00:00
/// Adds the images from albums node.
2013-03-04 14:34:00 +00:00
/// </summary>
2014-01-31 04:50:09 +00:00
/// <param name="list">The list.</param>
/// <param name="type">The type.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="reader">The reader.</param>
/// <param name="cancellationToken">The cancellation token.</param>
private void AddImagesFromImageTypeNode(List<RemoteImageInfo> list, ImageType type, int width, int height, XmlReader reader, CancellationToken cancellationToken)
2013-03-04 14:34:00 +00:00
{
2014-01-31 04:50:09 +00:00
reader.MoveToContent();
while (reader.Read())
2013-04-28 23:39:17 +00:00
{
2014-01-31 04:50:09 +00:00
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "hdmusiclogo":
case "musiclogo":
case "artistbackground":
case "hdmusicart":
case "musicart":
case "hdmusicbanner":
case "musicbanner":
case "artistthumb":
{
AddImage(list, reader, type, width, height);
break;
}
default:
{
using (reader.ReadSubtree())
{
}
break;
}
}
}
2013-04-28 23:39:17 +00:00
}
2013-03-04 14:34:00 +00:00
}
2014-01-31 04:50:09 +00:00
/// <summary>
/// Adds the image.
/// </summary>
/// <param name="list">The list.</param>
/// <param name="reader">The reader.</param>
/// <param name="type">The type.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
private void AddImage(List<RemoteImageInfo> list, XmlReader reader, ImageType type, int width, int height)
{
2014-01-31 04:50:09 +00:00
var url = reader.GetAttribute("url");
2014-01-31 04:50:09 +00:00
var size = reader.GetAttribute("size");
if (!String.IsNullOrEmpty(size))
{
2014-01-31 04:50:09 +00:00
int sizeNum;
if (Int32.TryParse(size, NumberStyles.Any, _usCulture, out sizeNum))
{
width = sizeNum;
height = sizeNum;
}
}
2014-01-31 04:50:09 +00:00
var likesString = reader.GetAttribute("likes");
int likes;
var info = new RemoteImageInfo
{
RatingType = RatingType.Likes,
Type = type,
Width = width,
Height = height,
ProviderName = Name,
Url = url,
Language = reader.GetAttribute("lang")
};
2014-01-31 04:50:09 +00:00
if (!String.IsNullOrEmpty(likesString) && Int32.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
{
info.CommunityRating = likes;
}
2014-01-31 04:50:09 +00:00
list.Add(info);
}
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
});
}
2014-02-10 18:39:41 +00:00
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)
2013-03-04 14:34:00 +00:00
{
if (!_config.Configuration.EnableFanArtUpdates)
{
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
var artistXmlPath = GetArtistXmlPath(_config.CommonApplicationPaths, id);
2014-01-31 04:50:09 +00:00
var fileInfo = new FileInfo(artistXmlPath);
return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
}
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);
2014-01-31 04:50:09 +00:00
internal Task EnsureMovieXml(string musicBrainzId, CancellationToken cancellationToken)
{
var xmlPath = GetArtistXmlPath(_config.ApplicationPaths, musicBrainzId);
var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
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
}
}
2014-01-31 04:50:09 +00:00
return DownloadArtistXml(musicBrainzId, cancellationToken);
}
/// <summary>
/// Downloads the artist XML.
/// </summary>
/// <param name="musicBrainzId">The music brainz id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{System.Boolean}.</returns>
2014-01-31 04:50:09 +00:00
internal async Task DownloadArtistXml(string musicBrainzId, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var url = string.Format(FanArtBaseUrl, ApiKey, musicBrainzId);
2014-01-31 04:50:09 +00:00
var xmlPath = GetArtistXmlPath(_config.ApplicationPaths, musicBrainzId);
Directory.CreateDirectory(Path.GetDirectoryName(xmlPath));
2014-01-31 04:50:09 +00:00
using (var response = await _httpClient.Get(new HttpRequestOptions
{
2013-05-12 22:57:51 +00:00
Url = url,
ResourcePool = FanArtResourcePool,
CancellationToken = cancellationToken
2013-05-12 22:57:51 +00:00
}).ConfigureAwait(false))
2013-04-28 23:39:17 +00:00
{
using (var xmlFileStream = _fileSystem.GetFileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
{
await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
}
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
}
2014-01-31 04:50:09 +00:00
internal static string GetArtistXmlPath(IApplicationPaths appPaths, string musicBrainzArtistId)
{
2014-01-31 04:50:09 +00:00
var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
return Path.Combine(dataPath, "fanart.xml");
}
2013-03-04 14:34:00 +00:00
}
}