2013-05-02 22:32:15 +00:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-05 16:48:17 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-05-02 22:32:15 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-03-05 16:48:17 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-04-22 04:38:03 +00:00
|
|
|
|
using MoreLinq;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2013-03-05 16:48:17 +00:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-02 22:32:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Needses the refresh internal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="providerInfo">The provider info.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
// If song metadata has changed and we don't have an mbid, refresh
|
|
|
|
|
if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)) &&
|
|
|
|
|
GetComparisonData(item as MusicAlbum) != providerInfo.Data)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-05 16:48:17 +00:00
|
|
|
|
protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2013-04-22 04:38:03 +00:00
|
|
|
|
var result = await GetAlbumResult(item, cancellationToken).ConfigureAwait(false);
|
2013-03-05 16:48:17 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-02 22:32:15 +00:00
|
|
|
|
|
|
|
|
|
BaseProviderInfo data;
|
|
|
|
|
|
|
|
|
|
if (item.ProviderData.TryGetValue(Id, out data))
|
|
|
|
|
{
|
|
|
|
|
data.Data = GetComparisonData(item as MusicAlbum);
|
|
|
|
|
}
|
2013-03-05 16:48:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-22 04:38:03 +00:00
|
|
|
|
private async Task<LastfmGetAlbumResult> GetAlbumResult(BaseItem item, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var folder = (Folder)item;
|
|
|
|
|
|
|
|
|
|
// Get each song, distinct by the combination of AlbumArtist and Album
|
2013-04-27 13:05:33 +00:00
|
|
|
|
var songs = folder.RecursiveChildren.OfType<Audio>().DistinctBy(i => (i.AlbumArtist ?? string.Empty) + (i.Album ?? string.Empty), StringComparer.OrdinalIgnoreCase).ToList();
|
2013-04-22 04:38:03 +00:00
|
|
|
|
|
|
|
|
|
foreach (var song in songs.Where(song => !string.IsNullOrEmpty(song.Album) && !string.IsNullOrEmpty(song.AlbumArtist)))
|
|
|
|
|
{
|
2013-04-28 16:25:14 +00:00
|
|
|
|
var result = await GetAlbumResult(song.AlbumArtist, song.Album, cancellationToken).ConfigureAwait(false);
|
2013-04-22 04:38:03 +00:00
|
|
|
|
|
|
|
|
|
if (result != null && result.album != null)
|
|
|
|
|
{
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-28 16:25:14 +00:00
|
|
|
|
// Try the folder name
|
|
|
|
|
return await GetAlbumResult(item.Parent.Name, item.Name, cancellationToken);
|
2013-04-22 04:38:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<LastfmGetAlbumResult> GetAlbumResult(string artist, string album, 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(artist), UrlEncode(album), ApiKey);
|
|
|
|
|
|
|
|
|
|
using (var json = await HttpClient.Get(url, LastfmResourcePool, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task FetchData(BaseItem item, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return FetchLastfmData(item, string.Empty, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-05 16:48:17 +00:00
|
|
|
|
public override bool Supports(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
return item is MusicAlbum;
|
|
|
|
|
}
|
2013-04-22 04:38:03 +00:00
|
|
|
|
|
|
|
|
|
protected override bool RefreshOnFileSystemStampChange
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-02 22:32:15 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="album">The album.</param>
|
|
|
|
|
/// <returns>Guid.</returns>
|
|
|
|
|
private Guid GetComparisonData(MusicAlbum album)
|
|
|
|
|
{
|
|
|
|
|
var songs = album.RecursiveChildren.OfType<Audio>().ToList();
|
|
|
|
|
|
|
|
|
|
var albumArtists = songs.Select(i => i.AlbumArtist)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var albumNames = songs.Select(i => i.AlbumArtist)
|
|
|
|
|
.Where(i => !string.IsNullOrEmpty(i))
|
|
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
albumArtists.AddRange(albumNames);
|
|
|
|
|
|
|
|
|
|
return string.Join(string.Empty, albumArtists.OrderBy(i => i).ToArray()).GetMD5();
|
|
|
|
|
}
|
2013-03-05 16:48:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|