2013-05-25 17:55:32 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-04 06:06:38 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-03-04 01:46:06 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-05-06 18:34:42 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-06-09 16:47:28 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
2013-05-06 18:34:42 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-05-25 17:55:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
2013-04-10 15:56:36 +00:00
|
|
|
|
using System.IO;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
using System.Linq;
|
2013-05-25 17:55:32 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
2013-04-10 15:56:36 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2013-05-05 05:46:17 +00:00
|
|
|
|
using System.Xml;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
|
2013-06-09 16:47:28 +00:00
|
|
|
|
namespace MediaBrowser.Providers.Music
|
2013-03-03 20:55:30 +00:00
|
|
|
|
{
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class LastfmArtistProvider
|
|
|
|
|
/// </summary>
|
2013-03-04 20:03:36 +00:00
|
|
|
|
public class LastfmArtistProvider : LastfmBaseProvider
|
2013-03-03 20:55:30 +00:00
|
|
|
|
{
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
2013-05-27 15:02:16 +00:00
|
|
|
|
protected readonly ILibraryManager LibraryManager;
|
2013-05-06 18:34:42 +00:00
|
|
|
|
|
2013-06-10 01:40:28 +00:00
|
|
|
|
/// <summary>
|
2013-06-23 18:55:30 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="LastfmArtistProvider" /> class.
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="jsonSerializer">The json serializer.</param>
|
|
|
|
|
/// <param name="httpClient">The HTTP client.</param>
|
|
|
|
|
/// <param name="logManager">The log manager.</param>
|
|
|
|
|
/// <param name="configurationManager">The configuration manager.</param>
|
|
|
|
|
/// <param name="libraryManager">The library manager.</param>
|
2013-06-23 18:55:30 +00:00
|
|
|
|
public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager libraryManager)
|
2013-03-04 06:06:38 +00:00
|
|
|
|
: base(jsonSerializer, httpClient, logManager, configurationManager)
|
2013-03-03 20:55:30 +00:00
|
|
|
|
{
|
2013-05-27 15:02:16 +00:00
|
|
|
|
LibraryManager = libraryManager;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-23 18:55:30 +00:00
|
|
|
|
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
|
|
|
|
|
{
|
|
|
|
|
if (HasAltMeta(item))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.NeedsRefreshInternal(item, providerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 02:06:51 +00:00
|
|
|
|
protected override string ProviderVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2013-11-06 16:29:20 +00:00
|
|
|
|
return "9";
|
2013-08-13 02:06:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-07 17:11:02 +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("artist.xml");
|
|
|
|
|
}
|
2013-10-16 20:49:44 +00:00
|
|
|
|
|
2013-05-06 18:34:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2013-11-05 21:07:22 +00:00
|
|
|
|
private async Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
|
2013-05-06 01:21:07 +00:00
|
|
|
|
{
|
2013-05-06 17:28:29 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// If we don't get anything, go directly to music brainz
|
|
|
|
|
return await FindIdFromMusicBrainz(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException e)
|
|
|
|
|
{
|
|
|
|
|
if (e.StatusCode.HasValue && e.StatusCode.Value == HttpStatusCode.BadRequest)
|
|
|
|
|
{
|
|
|
|
|
// They didn't like a character in the name. Handle the exception so that the provider doesn't keep retrying over and over
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-05-06 01:21:07 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-05-06 01:21:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-05 21:07:22 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="force">if set to <c>true</c> [force].</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token</param>
|
|
|
|
|
/// <returns>Task{System.Boolean}.</returns>
|
|
|
|
|
public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(id))
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
item.SetProviderId(MetadataProviders.Musicbrainz, id);
|
|
|
|
|
|
|
|
|
|
await FetchLastfmData(item, id, force, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetLastRefreshed(item, DateTime.UtcNow);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the id from music artist entity.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2013-05-06 18:34:42 +00:00
|
|
|
|
private string FindIdFromMusicArtistEntity(BaseItem item)
|
|
|
|
|
{
|
2013-05-27 15:02:16 +00:00
|
|
|
|
var artist = LibraryManager.RootFolder.RecursiveChildren.OfType<MusicArtist>()
|
2013-05-22 03:42:25 +00:00
|
|
|
|
.FirstOrDefault(i => string.Compare(i.Name, item.Name, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0);
|
2013-05-06 18:34:42 +00:00
|
|
|
|
|
|
|
|
|
return artist != null ? artist.GetProviderId(MetadataProviders.Musicbrainz) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the id from music brainz.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2013-05-06 01:21:07 +00:00
|
|
|
|
private async Task<string> FindIdFromMusicBrainz(BaseItem item, CancellationToken cancellationToken)
|
2013-05-05 05:46:17 +00:00
|
|
|
|
{
|
2013-05-06 17:28:29 +00:00
|
|
|
|
// They seem to throw bad request failures on any term with a slash
|
|
|
|
|
var nameToSearch = item.Name.Replace('/', ' ');
|
|
|
|
|
|
2013-10-18 14:00:55 +00:00
|
|
|
|
var url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artist:\"{0}\"", UrlEncode(nameToSearch));
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-11-12 17:12:11 +00:00
|
|
|
|
var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2013-05-02 15:18:28 +00:00
|
|
|
|
|
2013-05-05 05:46:17 +00:00
|
|
|
|
var ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
2013-08-12 19:13:58 +00:00
|
|
|
|
var node = doc.SelectSingleNode("//mb:artist-list/mb:artist/@id", ns);
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
|
|
|
|
if (node != null && node.Value != null)
|
|
|
|
|
{
|
|
|
|
|
return node.Value;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
if (HasDiacritics(item.Name))
|
|
|
|
|
{
|
|
|
|
|
// Try again using the search with accent characters url
|
2013-10-18 14:00:55 +00:00
|
|
|
|
url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
2013-11-12 17:12:11 +00:00
|
|
|
|
doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
2013-08-12 19:13:58 +00:00
|
|
|
|
node = doc.SelectSingleNode("//mb:artist-list/mb:artist/@id", ns);
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
if (node != null && node.Value != null)
|
|
|
|
|
{
|
|
|
|
|
return node.Value;
|
|
|
|
|
}
|
2013-05-05 13:27:20 +00:00
|
|
|
|
}
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-05-05 13:27:20 +00:00
|
|
|
|
return null;
|
2013-03-04 01:46:06 +00:00
|
|
|
|
}
|
2013-05-06 17:28:29 +00:00
|
|
|
|
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether the specified text has diacritics.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
|
/// <returns><c>true</c> if the specified text has diacritics; otherwise, <c>false</c>.</returns>
|
2013-05-06 17:28:29 +00:00
|
|
|
|
private bool HasDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return !string.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
|
|
|
|
|
}
|
2013-05-10 17:12:50 +00:00
|
|
|
|
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the diacritics.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2013-05-06 17:28:29 +00:00
|
|
|
|
private string RemoveDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return string.Concat(
|
|
|
|
|
text.Normalize(NormalizationForm.FormD)
|
|
|
|
|
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
|
|
|
|
|
UnicodeCategory.NonSpacingMark)
|
|
|
|
|
).Normalize(NormalizationForm.FormC);
|
|
|
|
|
}
|
2013-05-10 17:12:50 +00:00
|
|
|
|
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fetches the lastfm data.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="musicBrainzId">The music brainz id.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-11-05 21:07:22 +00:00
|
|
|
|
protected virtual async Task FetchLastfmData(BaseItem item, string musicBrainzId, bool force, CancellationToken cancellationToken)
|
2013-03-04 01:46:06 +00:00
|
|
|
|
{
|
|
|
|
|
// Get artist info with provided id
|
2013-05-25 17:55:32 +00:00
|
|
|
|
var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(musicBrainzId), ApiKey);
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-04-22 04:38:03 +00:00
|
|
|
|
LastfmGetArtistResult result;
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-05-06 19:31:57 +00:00
|
|
|
|
using (var json = await HttpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = LastfmResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken,
|
2013-05-20 18:03:09 +00:00
|
|
|
|
EnableHttpCompression = false
|
2013-05-06 19:31:57 +00:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
2013-03-04 01:46:06 +00:00
|
|
|
|
{
|
2013-09-06 15:23:20 +00:00
|
|
|
|
using (var reader = new StreamReader(json))
|
|
|
|
|
{
|
|
|
|
|
var jsonText = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
// Fix their bad json
|
|
|
|
|
jsonText = jsonText.Replace("\"#text\"", "\"url\"");
|
|
|
|
|
|
|
|
|
|
result = JsonSerializer.DeserializeFromString<LastfmGetArtistResult>(jsonText);
|
|
|
|
|
}
|
2013-03-04 01:46:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result != null && result.artist != null)
|
|
|
|
|
{
|
2013-03-04 20:03:36 +00:00
|
|
|
|
LastfmHelper.ProcessArtistData(item, result.artist);
|
2013-03-04 01:46:06 +00:00
|
|
|
|
}
|
2013-03-03 20:55:30 +00:00
|
|
|
|
}
|
2013-03-04 20:03:36 +00:00
|
|
|
|
|
2013-05-25 17:55:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supportses the specified item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
|
2013-04-10 15:56:36 +00:00
|
|
|
|
public override bool Supports(BaseItem item)
|
2013-03-04 20:03:36 +00:00
|
|
|
|
{
|
2013-05-02 14:30:38 +00:00
|
|
|
|
return item is MusicArtist;
|
2013-03-04 20:03:36 +00:00
|
|
|
|
}
|
2013-03-03 20:55:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|