From b27cad26182b6dff6c729e97514678a6c45dd704 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Tue, 5 Mar 2013 11:48:17 -0500 Subject: [PATCH] LastfmAlbumProvider --- .../MediaBrowser.Controller.csproj | 1 + .../Providers/Music/LastfmAlbumProvider.cs | 76 +++++++++++++++++++ .../Providers/Music/LastfmArtistProvider.cs | 2 +- .../Providers/Music/LastfmBaseProvider.cs | 21 ++++- .../Providers/Music/LastfmHelper.cs | 29 +++++-- 5 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index c7a26b77e..018b3960d 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -108,6 +108,7 @@ + diff --git a/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs new file mode 100644 index 000000000..70de42f4d --- /dev/null +++ b/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs @@ -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 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 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(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; + } + } +} diff --git a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs index 8aca8b9a0..29cb35cdc 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs @@ -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; } diff --git a/MediaBrowser.Controller/Providers/Music/LastfmBaseProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmBaseProvider.cs index f18a9cb65..d8b4c387f 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmBaseProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmBaseProvider.cs @@ -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; } diff --git a/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs b/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs index 1ad4a34a3..101422f01 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs @@ -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) - { - artist.AddGenre(tag.name); - } - + AddGenres(artist, data.tags); } } + public static void ProcessAlbumData(BaseItem item, LastfmAlbum data) + { + 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); + } + } } }