LastfmAlbumProvider

This commit is contained in:
Eric Reed 2013-03-05 11:48:17 -05:00
parent d433ea5bf9
commit b27cad2618
5 changed files with 122 additions and 7 deletions

View File

@ -108,6 +108,7 @@
<Compile Include="IServerApplicationPaths.cs" />
<Compile Include="Library\ChildrenChangedEventArgs.cs" />
<Compile Include="Library\DtoBuilder.cs" />
<Compile Include="Providers\Music\LastfmAlbumProvider.cs" />
<Compile Include="Providers\Music\FanArtAlbumProvider.cs" />
<Compile Include="Providers\Music\FanArtArtistProvider.cs" />
<Compile Include="Providers\Music\LastfmArtistProvider.cs" />

View File

@ -0,0 +1,76 @@
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
{
private static readonly Task<string> BlankId = Task.FromResult("0000");
public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(jsonSerializer, httpClient, logManager, configurationManager)
{
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();
await Kernel.Instance.FileSystemManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
}
}
}
public override bool Supports(BaseItem item)
{
return item is MusicAlbum;
}
}
}

View File

@ -75,7 +75,7 @@ namespace MediaBrowser.Controller.Providers.Music
{
if (e.StatusCode == HttpStatusCode.NotFound)
{
throw new LastfmProviderException(string.Format("Unable to retrieve artist info for {0} with id {0}", item.Name, id));
throw new LastfmProviderException(string.Format("Unable to retrieve artist info for {0} with id {1}", item.Name, id));
}
throw;
}

View File

@ -123,7 +123,7 @@ namespace MediaBrowser.Controller.Providers.Music
var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
if (id != null)
{
Logger.Debug("LastfmProvider - getting info with id: " + id);
Logger.Debug("LastfmProvider - getting info for {0}", item.Name);
cancellationToken.ThrowIfCancellationRequested();
@ -275,6 +275,25 @@ namespace MediaBrowser.Controller.Providers.Music
public LastFmBio bio { get; set; }
}
public class LastfmAlbum
{
public string name { get; set; }
public string artist { get; set; }
public string id { get; set; }
public string mbid { get; set; }
public string releasedate { get; set; }
public int listeners { get; set; }
public int playcount { get; set; }
public LastfmTags toptags { get; set; }
public LastFmBio wiki { get; set; }
}
public class LastfmGetAlbumResult
{
public LastfmAlbum album { get; set; }
}
public class LastfmGetArtistResult
{
public LastfmArtist artist { get; set; }

View File

@ -4,12 +4,14 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Controller.Providers.Music
{
public static class LastfmHelper
{
public static string LocalArtistMetaFileName = "MBArtist.json";
public static string LocalAlbumMetaFileName = "MBAlbum.json";
public static void ProcessArtistData(BaseItem artist, LastfmArtist data)
{
@ -31,13 +33,30 @@ namespace MediaBrowser.Controller.Providers.Music
artist.PremiereDate = new DateTime(yearFormed, 1,1);
if (data.tags != null)
{
foreach (var tag in data.tags.tag)
AddGenres(artist, data.tags);
}
}
public static void ProcessAlbumData(BaseItem item, LastfmAlbum data)
{
artist.AddGenre(tag.name);
}
if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid);
item.Overview = data.wiki != null ? data.wiki.content : null;
var release = DateTime.MinValue;
DateTime.TryParse(data.releasedate, out release);
item.PremiereDate = release;
if (data.toptags != null)
{
AddGenres(item, data.toptags);
}
}
private static void AddGenres(BaseItem item, LastfmTags tags)
{
foreach (var tag in tags.tag)
{
item.AddGenre(tag.name);
}
}
}
}