jellyfin/MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs

131 lines
5.0 KiB
C#
Raw Normal View History

2013-04-22 04:38:03 +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;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
2013-04-22 04:38:03 +00:00
using MediaBrowser.Model.Net;
using System;
2013-04-22 04:38:03 +00:00
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2013-04-22 04:38:03 +00:00
using System.Xml;
2013-03-05 15:45:59 +00:00
namespace MediaBrowser.Controller.Providers.Music
{
public class FanArtAlbumProvider : FanartBaseProvider
{
private readonly IProviderManager _providerManager;
2013-04-22 04:38:03 +00:00
protected IHttpClient HttpClient { get; private set; }
public FanArtAlbumProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
: base(logManager, configurationManager)
2013-03-05 15:45:59 +00:00
{
_providerManager = providerManager;
2013-04-22 04:38:03 +00:00
HttpClient = httpClient;
2013-03-05 15:45:59 +00:00
}
public override bool Supports(BaseItem item)
{
2013-04-22 04:38:03 +00:00
return item is MusicAlbum;
2013-03-05 15:45:59 +00:00
}
/// <summary>
/// Needses the refresh internal.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="providerInfo">The provider info.</param>
/// <returns><c>true</c> if we need refreshing, <c>false</c> otherwise</returns>
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
//we fetch if image needed and haven't already tried recently
2013-04-22 04:38:03 +00:00
return (string.IsNullOrEmpty(item.PrimaryImagePath) || !item.HasImage(ImageType.Disc)) &&
2013-03-05 15:45:59 +00:00
DateTime.Today.Subtract(providerInfo.LastRefreshed).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays;
}
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
2013-03-05 15:45:59 +00:00
{
var mbid = item.GetProviderId(MetadataProviders.Musicbrainz);
if (mbid == null)
{
Logger.Warn("No Musicbrainz id associated with album {0}", item.Name);
2013-04-22 04:38:03 +00:00
SetLastRefreshed(item, DateTime.UtcNow);
return true;
2013-03-05 15:45:59 +00:00
}
cancellationToken.ThrowIfCancellationRequested();
2013-04-22 04:38:03 +00:00
var url = string.Format("http://api.fanart.tv/webservice/album/{0}/{1}/xml/all/1/1", APIKey, item.GetProviderId(MetadataProviders.Musicbrainz));
2013-04-22 04:38:03 +00:00
var doc = new XmlDocument();
2013-03-05 15:45:59 +00:00
2013-04-22 04:38:03 +00:00
try
{
using (var xml = await HttpClient.Get(url, FanArtResourcePool, cancellationToken).ConfigureAwait(false))
{
doc.Load(xml);
}
}
catch (HttpException)
2013-03-05 15:45:59 +00:00
{
}
2013-04-22 04:38:03 +00:00
cancellationToken.ThrowIfCancellationRequested();
2013-04-22 04:38:03 +00:00
if (doc.HasChildNodes)
{
2013-04-22 04:38:03 +00:00
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)
{
}
}
}
}
2013-04-22 04:38:03 +00:00
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
2013-03-05 15:45:59 +00:00
}
}