jellyfin/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs

203 lines
7.5 KiB
C#
Raw Normal View History

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-06-09 16:47:28 +00:00
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
2013-03-05 16:48:17 +00:00
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
2013-06-23 18:55:30 +00:00
using MoreLinq;
2013-04-22 04:38:03 +00:00
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2013-03-05 16:48:17 +00:00
2013-06-09 16:47:28 +00:00
namespace MediaBrowser.Providers.Music
2013-03-05 16:48:17 +00:00
{
public class LastfmAlbumProvider : LastfmBaseProvider
{
private static readonly Task<string> BlankId = Task.FromResult("");
2013-03-05 16:48:17 +00:00
2013-06-23 18:55:30 +00:00
public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
2013-03-05 16:48:17 +00:00
: base(jsonSerializer, httpClient, logManager, configurationManager)
{
}
protected override Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
{
// We don't fetch by id
return BlankId;
}
2013-08-07 16:00:20 +00:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
2013-06-23 18:55:30 +00:00
private bool HasAltMeta(BaseItem item)
{
return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName("album.xml");
}
/// <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)
{
2013-06-23 18:55:30 +00:00
if (HasAltMeta(item))
{
return false;
}
// 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.FileStamp)
{
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-09-04 17:02:19 +00:00
var album = (MusicAlbum)item;
var result = await GetAlbumResult(album, cancellationToken).ConfigureAwait(false);
2013-03-05 16:48:17 +00:00
if (result != null && result.album != null)
{
LastfmHelper.ProcessAlbumData(item, result.album);
}
BaseProviderInfo data;
2013-05-06 13:21:34 +00:00
if (!item.ProviderData.TryGetValue(Id, out data))
{
2013-05-06 13:21:34 +00:00
data = new BaseProviderInfo();
item.ProviderData[Id] = data;
}
2013-05-06 13:21:34 +00:00
2013-09-04 17:02:19 +00:00
data.FileStamp = GetComparisonData(album);
2013-03-05 16:48:17 +00:00
}
2013-09-04 17:02:19 +00:00
private async Task<LastfmGetAlbumResult> GetAlbumResult(MusicAlbum item, CancellationToken cancellationToken)
2013-04-22 04:38:03 +00:00
{
2013-09-04 17:02:19 +00:00
// Try album release Id
if (!string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)))
{
var result = await GetAlbumResult(item.GetProviderId(MetadataProviders.Musicbrainz), cancellationToken).ConfigureAwait(false);
if (result != null && result.album != null)
{
return result;
}
}
2013-04-22 04:38:03 +00:00
2013-09-04 17:02:19 +00:00
// Try album release group Id
if (!string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup)))
{
var result = await GetAlbumResult(item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup), cancellationToken).ConfigureAwait(false);
if (result != null && result.album != null)
{
return result;
}
}
2013-04-22 04:38:03 +00:00
// Get each song, distinct by the combination of AlbumArtist and Album
2013-09-04 17:02:19 +00:00
var songs = item.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(new HttpRequestOptions
{
Url = url,
ResourcePool = LastfmResourcePool,
CancellationToken = cancellationToken,
EnableHttpCompression = false
}).ConfigureAwait(false))
2013-04-22 04:38:03 +00:00
{
return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
}
}
2013-09-04 17:02:19 +00:00
private async Task<LastfmGetAlbumResult> GetAlbumResult(string musicbraizId, CancellationToken cancellationToken)
{
// Get albu info using artist and album name
var url = RootUrl + string.Format("method=album.getInfo&mbid={0}&api_key={1}&format=json", musicbraizId, ApiKey);
using (var json = await HttpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = LastfmResourcePool,
CancellationToken = cancellationToken,
EnableHttpCompression = false
}).ConfigureAwait(false))
{
return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
}
}
2013-04-22 04:38:03 +00:00
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
/// <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
}
}