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;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace MediaBrowser.Providers.Music
{
public class LastfmAlbumProvider : LastfmBaseProvider
{
internal static LastfmAlbumProvider Current;
///
/// The _music brainz resource pool
///
private readonly SemaphoreSlim _musicBrainzResourcePool = new SemaphoreSlim(1, 1);
public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(jsonSerializer, httpClient, logManager, configurationManager)
{
Current = this;
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
protected override string ProviderVersion
{
get
{
return "9";
}
}
private bool HasAltMeta(BaseItem item)
{
return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName("album.xml");
}
///
/// Needses the refresh internal.
///
/// The item.
/// The provider info.
/// true if XXXX, false otherwise
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
var hasId = !string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Musicbrainz)) &&
!string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup));
if (hasId && HasAltMeta(item))
{
return false;
}
// If song metadata has changed and we don't have an mbid, refresh
if (!hasId && GetComparisonData(item as MusicAlbum) != providerInfo.FileStamp)
{
return true;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
///
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
///
/// The item.
/// if set to true [force].
/// The cancellation token
/// Task{System.Boolean}.
public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var album = (MusicAlbum)item;
var result = await GetAlbumResult(album, cancellationToken).ConfigureAwait(false);
if (result != null && result.album != null)
{
LastfmHelper.ProcessAlbumData(item, result.album);
}
var releaseEntryId = item.GetProviderId(MetadataProviders.Musicbrainz);
if (!string.IsNullOrEmpty(releaseEntryId))
{
var musicBrainzReleaseGroupId = album.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
if (string.IsNullOrEmpty(musicBrainzReleaseGroupId))
{
musicBrainzReleaseGroupId = await GetReleaseGroupId(releaseEntryId, cancellationToken).ConfigureAwait(false);
album.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, musicBrainzReleaseGroupId);
}
}
BaseProviderInfo data;
if (!item.ProviderData.TryGetValue(Id, out data))
{
data = new BaseProviderInfo();
item.ProviderData[Id] = data;
}
data.FileStamp = GetComparisonData(album);
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
private async Task GetAlbumResult(MusicAlbum item, CancellationToken cancellationToken)
{
// 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;
}
}
// 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;
}
}
// Get each song, distinct by the combination of AlbumArtist and Album
var songs = item.RecursiveChildren.OfType