jellyfin/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs

219 lines
7.2 KiB
C#
Raw Normal View History

2016-03-27 21:11:27 +00:00
using MediaBrowser.Common.Net;
2013-03-05 15:45:59 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
2013-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
2014-01-31 19:55:21 +00:00
using MediaBrowser.Model.Dto;
2013-03-05 15:45:59 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using System;
using System.Collections.Generic;
2014-01-31 19:55:21 +00:00
using System.Globalization;
2013-04-22 04:38:03 +00:00
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
2015-10-29 15:47:05 +00:00
using MediaBrowser.Model.Serialization;
2013-03-05 15:45:59 +00:00
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.Music
2013-03-05 15:45:59 +00:00
{
2016-05-25 00:42:12 +00:00
public class FanartAlbumProvider : IRemoteImageProvider, IHasOrder
2013-03-05 15:45:59 +00:00
{
2014-01-31 19:55:21 +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;
2015-10-29 15:47:05 +00:00
public FanartAlbumProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem, IJsonSerializer jsonSerializer)
2013-03-05 15:45:59 +00:00
{
2014-01-31 19:55:21 +00:00
_config = config;
_httpClient = httpClient;
_fileSystem = fileSystem;
2015-10-29 15:47:05 +00:00
_jsonSerializer = jsonSerializer;
2013-03-05 15:45:59 +00:00
}
2014-01-31 19:55:21 +00:00
public string Name
2013-08-07 16:00:20 +00:00
{
2014-01-31 19:55:21 +00:00
get { return ProviderName; }
2013-08-07 16:00:20 +00:00
}
2014-01-31 19:55:21 +00:00
public static string ProviderName
{
get { return "FanArt"; }
}
public bool Supports(IHasImages item)
2013-03-05 15:45:59 +00:00
{
2013-04-22 04:38:03 +00:00
return item is MusicAlbum;
2013-03-05 15:45:59 +00:00
}
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,
ImageType.Disc
};
}
2014-01-31 19:55:21 +00:00
2014-02-13 05:11:54 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(IHasImages item, CancellationToken cancellationToken)
2014-01-31 19:55:21 +00:00
{
var album = (MusicAlbum)item;
var list = new List<RemoteImageInfo>();
2016-01-19 16:09:33 +00:00
var musicArtist = album.MusicArtist;
if (musicArtist == null)
{
return list;
}
var artistMusicBrainzId = musicArtist.GetProviderId(MetadataProviders.MusicBrainzArtist);
2014-01-31 19:55:21 +00:00
if (!string.IsNullOrEmpty(artistMusicBrainzId))
2013-05-01 16:06:57 +00:00
{
2015-10-29 15:47:05 +00:00
await FanartArtistProvider.Current.EnsureArtistJson(artistMusicBrainzId, cancellationToken).ConfigureAwait(false);
2014-01-31 19:55:21 +00:00
2015-10-29 15:47:05 +00:00
var artistJsonPath = FanartArtistProvider.GetArtistJsonPath(_config.CommonApplicationPaths, artistMusicBrainzId);
2014-01-31 19:55:21 +00:00
var musicBrainzReleaseGroupId = album.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
var musicBrainzId = album.GetProviderId(MetadataProviders.MusicBrainzAlbum);
2014-01-31 19:55:21 +00:00
try
{
2015-10-29 15:47:05 +00:00
AddImages(list, artistJsonPath, musicBrainzId, musicBrainzReleaseGroupId, cancellationToken);
2014-01-31 19:55:21 +00:00
}
catch (FileNotFoundException)
{
2014-12-23 03:58:14 +00:00
}
2016-10-27 18:37:46 +00:00
catch (IOException)
2014-12-23 03:58:14 +00:00
{
2014-01-31 19:55:21 +00:00
}
2013-05-01 16:06:57 +00:00
}
2014-01-31 19:55:21 +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);
2013-05-01 16:06:57 +00:00
}
/// <summary>
2014-01-31 19:55:21 +00:00
/// Adds the images.
/// </summary>
2014-01-31 19:55:21 +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 19:55:21 +00:00
/// <param name="releaseId">The release identifier.</param>
/// <param name="releaseGroupId">The release group identifier.</param>
/// <param name="cancellationToken">The cancellation token.</param>
2015-10-29 15:47:05 +00:00
private void AddImages(List<RemoteImageInfo> list, string path, string releaseId, string releaseGroupId, CancellationToken cancellationToken)
2013-05-01 16:06:57 +00:00
{
2015-10-29 15:47:05 +00:00
var obj = _jsonSerializer.DeserializeFromFile<FanartArtistProvider.FanartArtistResponse>(path);
2014-01-31 19:55:21 +00:00
2015-10-29 15:47:05 +00:00
if (obj.albums != null)
2013-03-05 15:45:59 +00:00
{
2015-10-29 15:47:05 +00:00
var album = obj.albums.FirstOrDefault(i => string.Equals(i.release_group_id, releaseGroupId, StringComparison.OrdinalIgnoreCase));
if (album != null)
2014-01-31 19:55:21 +00:00
{
2015-10-29 15:47:05 +00:00
PopulateImages(list, album.albumcover, ImageType.Primary, 1000, 1000);
PopulateImages(list, album.cdart, ImageType.Disc, 1000, 1000);
2014-01-31 19:55:21 +00:00
}
2013-03-05 15:45:59 +00:00
}
}
private Regex _regex_http = new Regex("^http://");
2015-10-29 15:47:05 +00:00
private void PopulateImages(List<RemoteImageInfo> list,
List<FanartArtistProvider.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 19:55:21 +00:00
}
2015-10-29 15:47:05 +00:00
list.AddRange(images.Select(i =>
2013-12-15 00:26:01 +00:00
{
2015-10-29 15:47:05 +00:00
var url = i.url;
if (!string.IsNullOrEmpty(url))
2014-01-31 19:55:21 +00:00
{
2015-10-29 15:47:05 +00:00
var likesString = i.likes;
int likes;
var info = new RemoteImageInfo
{
RatingType = RatingType.Likes,
Type = type,
Width = width,
Height = height,
ProviderName = Name,
Url = _regex_http.Replace(url, "https://", 1),
2015-10-29 15:47:05 +00:00
Language = i.lang
};
if (!string.IsNullOrEmpty(likesString) && int.TryParse(likesString, NumberStyles.Any, _usCulture, out likes))
2014-01-31 19:55:21 +00:00
{
2015-10-29 15:47:05 +00:00
info.CommunityRating = likes;
2014-01-31 19:55:21 +00:00
}
2015-10-29 15:47:05 +00:00
return info;
2014-01-31 19:55:21 +00:00
}
2015-10-29 15:47:05 +00:00
return null;
}).Where(i => i != null));
}
2014-01-31 19:55:21 +00:00
public int Order
{
get
{
// After embedded provider
return 1;
}
2014-01-31 19:55:21 +00:00
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
2014-01-31 19:55:21 +00:00
CancellationToken = cancellationToken,
2016-10-31 18:39:41 +00:00
Url = url
2014-01-31 19:55:21 +00:00
});
}
2013-03-05 15:45:59 +00:00
}
}