You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs

118 lines
4.3 KiB

12 years ago
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
12 years ago
using MediaBrowser.Model.Net;
using System;
12 years ago
using System.IO;
using System.Threading;
using System.Threading.Tasks;
12 years ago
using System.Xml;
namespace MediaBrowser.Controller.Providers.Music
{
public class FanArtAlbumProvider : FanartBaseProvider
{
private readonly IProviderManager _providerManager;
12 years ago
protected IHttpClient HttpClient { get; private set; }
public FanArtAlbumProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
: base(logManager, configurationManager)
{
_providerManager = providerManager;
12 years ago
HttpClient = httpClient;
}
public override bool Supports(BaseItem item)
{
12 years ago
return item is MusicAlbum;
}
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
var mbid = item.GetProviderId(MetadataProviders.Musicbrainz);
if (mbid == null)
{
Logger.Warn("No Musicbrainz id associated with album {0}", item.Name);
12 years ago
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
cancellationToken.ThrowIfCancellationRequested();
12 years ago
var url = string.Format("http://api.fanart.tv/webservice/album/{0}/{1}/xml/all/1/1", APIKey, item.GetProviderId(MetadataProviders.Musicbrainz));
12 years ago
var doc = new XmlDocument();
12 years ago
try
{
using (var xml = await HttpClient.Get(url, FanArtResourcePool, cancellationToken).ConfigureAwait(false))
{
doc.Load(xml);
}
}
catch (HttpException)
{
}
12 years ago
cancellationToken.ThrowIfCancellationRequested();
12 years ago
if (doc.HasChildNodes)
{
12 years ago
if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc && !item.ResolveArgs.ContainsMetaFileByName(DISC_FILE))
{
var node = doc.SelectSingleNode("//fanart/music/albums/album//cdart/@url");
var path = node != null ? node.Value : null;
if (!string.IsNullOrEmpty(path))
{
Logger.Debug("FanArtProvider getting Disc for " + item.Name);
try
{
item.SetImage(ImageType.Disc, await _providerManager.DownloadAndSaveImage(item, path, DISC_FILE, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
}
catch (HttpException)
{
}
catch (IOException)
{
}
}
}
if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary && !item.ResolveArgs.ContainsMetaFileByName(PRIMARY_FILE))
{
var node = doc.SelectSingleNode("//fanart/music/albums/album//albumcover/@url");
var path = node != null ? node.Value : null;
if (!string.IsNullOrEmpty(path))
{
Logger.Debug("FanArtProvider getting albumcover for " + item.Name);
try
{
item.SetImage(ImageType.Primary, await _providerManager.DownloadAndSaveImage(item, path, PRIMARY_FILE, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
}
catch (HttpException)
{
}
catch (IOException)
{
}
}
}
}
12 years ago
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
}
}