2013-03-05 16:48:17 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers.Music
|
|
|
|
|
{
|
|
|
|
|
public class LastfmAlbumProvider : LastfmBaseProvider
|
|
|
|
|
{
|
2013-04-05 15:15:53 +00:00
|
|
|
|
private static readonly Task<string> BlankId = Task.FromResult("");
|
2013-03-05 16:48:17 +00:00
|
|
|
|
|
2013-03-08 05:08:27 +00:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
|
|
|
|
|
|
|
|
|
public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
|
2013-03-05 16:48:17 +00:00
|
|
|
|
: base(jsonSerializer, httpClient, logManager, configurationManager)
|
|
|
|
|
{
|
2013-03-08 05:08:27 +00:00
|
|
|
|
_providerManager = providerManager;
|
2013-03-05 16:48:17 +00:00
|
|
|
|
LocalMetaFileName = LastfmHelper.LocalAlbumMetaFileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
// We don't fetch by id
|
|
|
|
|
return BlankId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
// Get albu info using artist and album name
|
|
|
|
|
var url = RootUrl + string.Format("method=album.getInfo&artist={0}&album={1}&api_key={2}&format=json", UrlEncode(item.Parent.Name), UrlEncode(item.Name), ApiKey);
|
|
|
|
|
|
|
|
|
|
LastfmGetAlbumResult result = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
result = JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException e)
|
|
|
|
|
{
|
|
|
|
|
if (e.StatusCode == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
|
|
|
|
throw new LastfmProviderException(string.Format("Unable to retrieve album info for {0} with artist {1}", item.Name, item.Parent.Name));
|
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result != null && result.album != null)
|
|
|
|
|
{
|
|
|
|
|
LastfmHelper.ProcessAlbumData(item, result.album);
|
|
|
|
|
//And save locally if indicated
|
|
|
|
|
if (ConfigurationManager.Configuration.SaveLocalMeta)
|
|
|
|
|
{
|
|
|
|
|
var ms = new MemoryStream();
|
|
|
|
|
JsonSerializer.SerializeToStream(result.album, ms);
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2013-03-08 05:08:27 +00:00
|
|
|
|
await _providerManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
|
2013-03-05 16:48:17 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Supports(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
return item is MusicAlbum;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|