You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs

163 lines
5.9 KiB

using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MoreLinq;
12 years ago
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Music
{
public class LastfmAlbumProvider : LastfmBaseProvider
{
private static readonly Task<string> BlankId = Task.FromResult("");
public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(jsonSerializer, httpClient, logManager, configurationManager)
{
}
protected override Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
{
// We don't fetch by id
return BlankId;
}
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
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)
{
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);
}
protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
{
12 years ago
var result = await GetAlbumResult(item, cancellationToken).ConfigureAwait(false);
if (result != null && result.album != null)
{
LastfmHelper.ProcessAlbumData(item, result.album);
}
BaseProviderInfo data;
if (!item.ProviderData.TryGetValue(Id, out data))
{
data = new BaseProviderInfo();
item.ProviderData[Id] = data;
}
data.FileStamp = GetComparisonData(item as MusicAlbum);
}
12 years ago
private async Task<LastfmGetAlbumResult> GetAlbumResult(BaseItem item, CancellationToken cancellationToken)
{
var folder = (Folder)item;
// Get each song, distinct by the combination of AlbumArtist and Album
var songs = folder.RecursiveChildren.OfType<Audio>().DistinctBy(i => (i.AlbumArtist ?? string.Empty) + (i.Album ?? string.Empty), StringComparer.OrdinalIgnoreCase).ToList();
12 years ago
foreach (var song in songs.Where(song => !string.IsNullOrEmpty(song.Album) && !string.IsNullOrEmpty(song.AlbumArtist)))
{
var result = await GetAlbumResult(song.AlbumArtist, song.Album, cancellationToken).ConfigureAwait(false);
12 years ago
if (result != null && result.album != null)
{
return result;
}
}
// Try the folder name
return await GetAlbumResult(item.Parent.Name, item.Name, cancellationToken);
12 years ago
}
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))
12 years ago
{
return JsonSerializer.DeserializeFromStream<LastfmGetAlbumResult>(json);
}
}
protected override Task FetchData(BaseItem item, CancellationToken cancellationToken)
{
return FetchLastfmData(item, string.Empty, cancellationToken);
}
public override bool Supports(BaseItem item)
{
return item is MusicAlbum;
}
12 years ago
/// <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();
}
}
}