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

106 lines
3.9 KiB
C#
Raw Normal View History

2013-03-03 20:55:30 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2013-03-03 20:55:30 +00:00
using System.Linq;
2013-03-04 01:46:06 +00:00
using System.Net;
2013-03-03 20:55:30 +00:00
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
2013-03-04 06:06:38 +00:00
using MediaBrowser.Controller.Configuration;
2013-03-04 14:34:00 +00:00
using MediaBrowser.Model.Entities;
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;
2013-03-04 01:46:06 +00:00
using MediaBrowser.Model.Net;
2013-03-03 20:55:30 +00:00
using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Controller.Providers.Music
{
public class LastfmArtistProvider : LastfmBaseProvider
2013-03-03 20:55:30 +00:00
{
2013-03-04 06:06:38 +00:00
public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(jsonSerializer, httpClient, logManager, configurationManager)
2013-03-03 20:55:30 +00:00
{
LocalMetaFileName = LastfmHelper.LocalArtistMetaFileName;
2013-03-03 20:55:30 +00:00
}
2013-03-04 01:46:06 +00:00
protected override async Task<string> FindId(Entities.BaseItem item, System.Threading.CancellationToken cancellationToken)
2013-03-03 20:55:30 +00:00
{
2013-03-04 01:46:06 +00:00
//Execute the Artist search against our name and assume first one is the one we want
var url = RootUrl + string.Format("method=artist.search&artist={0}&api_key={1}&format=json", UrlEncode(item.Name), ApiKey);
LastfmArtistSearchResults searchResult = null;
try
{
2013-03-04 06:06:38 +00:00
using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
2013-03-04 01:46:06 +00:00
{
searchResult = JsonSerializer.DeserializeFromStream<LastfmArtistSearchResults>(json);
}
}
catch (HttpException e)
{
if (e.StatusCode == HttpStatusCode.NotFound)
{
return null;
}
throw;
}
if (searchResult != null && searchResult.results != null && searchResult.results.artistmatches != null && searchResult.results.artistmatches.artist.Any())
{
return searchResult.results.artistmatches.artist.First().mbid;
}
return null;
}
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);
LastfmGetArtistResult result = null;
try
{
2013-03-04 06:06:38 +00:00
using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
2013-03-04 01:46:06 +00:00
{
result = JsonSerializer.DeserializeFromStream<LastfmGetArtistResult>(json);
}
}
catch (HttpException e)
{
if (e.StatusCode == HttpStatusCode.NotFound)
{
throw new LastfmProviderException(string.Format("Unable to retrieve artist info for {0} with id {0}", item.Name, id));
}
throw;
}
if (result != null && result.artist != null)
{
LastfmHelper.ProcessArtistData(item, result.artist);
//And save locally if indicated
if (ConfigurationManager.Configuration.SaveLocalMeta)
{
var ms = new MemoryStream();
JsonSerializer.SerializeToStream(result.artist, ms);
cancellationToken.ThrowIfCancellationRequested();
await Kernel.Instance.FileSystemManager.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(Entities.BaseItem item)
{
return item is MusicArtist;
}
2013-03-03 20:55:30 +00:00
}
}