jellyfin-server/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs

82 lines
3.2 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Net;
2013-03-04 06:06:38 +00:00
using MediaBrowser.Controller.Configuration;
2013-03-03 20:55:30 +00:00
using MediaBrowser.Controller.Entities;
2013-03-04 01:46:06 +00:00
using MediaBrowser.Controller.Entities.Audio;
2013-03-03 20:55:30 +00:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
2013-03-03 20:55:30 +00:00
namespace MediaBrowser.Controller.Providers.Music
{
public class LastfmArtistProvider : LastfmBaseProvider
2013-03-03 20:55:30 +00:00
{
private readonly IProviderManager _providerManager;
public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
2013-03-04 06:06:38 +00:00
: base(jsonSerializer, httpClient, logManager, configurationManager)
2013-03-03 20:55:30 +00:00
{
_providerManager = providerManager;
LocalMetaFileName = LastfmHelper.LocalArtistMetaFileName;
2013-03-03 20:55:30 +00:00
}
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
2013-03-03 20:55:30 +00:00
{
return true;
}
2013-03-04 01:46:06 +00:00
protected override async Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
{
var url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artistaccent:{0}", UrlEncode(item.Name));
2013-03-04 01:46:06 +00:00
var doc = await FanArtAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
var node = doc.SelectSingleNode("//mb:artist-list/mb:artist/@id", ns);
2013-03-04 01:46:06 +00:00
return node != null ? node.Value : null;
2013-03-04 01:46:06 +00:00
}
protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
{
// Get artist info with provided id
var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(id), ApiKey);
2013-04-22 04:38:03 +00:00
LastfmGetArtistResult result;
2013-03-04 01:46:06 +00:00
2013-04-22 04:38:03 +00:00
using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
2013-03-04 01:46:06 +00:00
{
2013-04-22 04:38:03 +00:00
result = JsonSerializer.DeserializeFromStream<LastfmGetArtistResult>(json);
2013-03-04 01:46:06 +00:00
}
if (result != null && result.artist != null)
{
LastfmHelper.ProcessArtistData(item, result.artist);
//And save locally if indicated
2013-04-22 04:38:03 +00:00
if (SaveLocalMeta)
{
var ms = new MemoryStream();
JsonSerializer.SerializeToStream(result.artist, ms);
2013-04-25 17:27:36 +00:00
if (cancellationToken.IsCancellationRequested)
{
ms.Dispose();
cancellationToken.ThrowIfCancellationRequested();
}
await _providerManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
}
2013-03-04 01:46:06 +00:00
}
2013-03-03 20:55:30 +00:00
}
public override bool Supports(BaseItem item)
{
2013-05-02 14:30:38 +00:00
return item is MusicArtist;
}
2013-03-03 20:55:30 +00:00
}
}