jellyfin-server/MediaBrowser.Providers/Music/LastfmArtistProvider.cs

187 lines
6.3 KiB
C#
Raw Normal View History

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-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;
using MediaBrowser.Model.Providers;
2013-03-03 20:55:30 +00:00
using MediaBrowser.Model.Serialization;
2013-05-25 17:55:32 +00:00
using System;
using System.Collections.Generic;
2013-05-25 17:55:32 +00:00
using System.Globalization;
using System.IO;
using System.Linq;
2013-05-25 17:55:32 +00:00
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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
{
2014-02-07 03:10:13 +00:00
public class LastfmArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
2013-03-03 20:55:30 +00:00
{
2014-01-31 04:50:09 +00:00
private readonly IJsonSerializer _json;
private readonly IHttpClient _httpClient;
2013-05-06 18:34:42 +00:00
2014-01-31 04:50:09 +00:00
internal static readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(4, 4);
internal const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
internal static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
private readonly IServerConfigurationManager _config;
2014-01-31 19:55:21 +00:00
private readonly ILogger _logger;
2014-01-31 04:50:09 +00:00
2014-01-31 19:55:21 +00:00
public LastfmArtistProvider(IHttpClient httpClient, IJsonSerializer json, IServerConfigurationManager config, ILogger logger)
2013-03-03 20:55:30 +00:00
{
2014-01-31 04:50:09 +00:00
_httpClient = httpClient;
_json = json;
2014-01-31 19:55:21 +00:00
_config = config;
_logger = logger;
2013-03-03 20:55:30 +00:00
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
{
return new List<RemoteSearchResult>();
}
2014-02-07 03:10:13 +00:00
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo id, CancellationToken cancellationToken)
2013-06-23 18:55:30 +00:00
{
2014-01-31 04:50:09 +00:00
var result = new MetadataResult<MusicArtist>();
2014-02-09 07:27:44 +00:00
var musicBrainzId = id.GetMusicBrainzArtistId();
2014-01-31 04:50:09 +00:00
if (!String.IsNullOrWhiteSpace(musicBrainzId))
2013-06-23 18:55:30 +00:00
{
2014-01-31 04:50:09 +00:00
cancellationToken.ThrowIfCancellationRequested();
result.Item = new MusicArtist();
result.HasMetadata = true;
await FetchLastfmData(result.Item, musicBrainzId, cancellationToken).ConfigureAwait(false);
2013-06-23 18:55:30 +00:00
}
2014-01-31 04:50:09 +00:00
return result;
2013-06-23 18:55:30 +00:00
}
2014-01-31 04:50:09 +00:00
protected virtual async Task FetchLastfmData(MusicArtist item, string musicBrainzId, CancellationToken cancellationToken)
2013-08-13 02:06:51 +00:00
{
2014-01-31 04:50:09 +00:00
// Get artist info with provided id
var url = RootUrl + String.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(musicBrainzId), ApiKey);
LastfmGetArtistResult result;
using (var json = await _httpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = LastfmResourcePool,
CancellationToken = cancellationToken,
EnableHttpCompression = false
}).ConfigureAwait(false))
{
using (var reader = new StreamReader(json))
{
var jsonText = await reader.ReadToEndAsync().ConfigureAwait(false);
// Fix their bad json
jsonText = jsonText.Replace("\"#text\"", "\"url\"");
result = _json.DeserializeFromString<LastfmGetArtistResult>(jsonText);
}
}
if (result != null && result.artist != null)
2013-08-13 02:06:51 +00:00
{
2014-01-31 04:50:09 +00:00
ProcessArtistData(item, result.artist, musicBrainzId);
2013-08-13 02:06:51 +00:00
}
}
2014-01-31 04:50:09 +00:00
private void ProcessArtistData(MusicArtist artist, LastfmArtist data, string musicBrainzId)
2013-08-07 17:11:02 +00:00
{
2014-01-31 04:50:09 +00:00
var yearFormed = 0;
2013-08-07 17:11:02 +00:00
2014-01-31 04:50:09 +00:00
if (data.bio != null)
{
Int32.TryParse(data.bio.yearformed, out yearFormed);
if (!artist.LockedFields.Contains(MetadataFields.Overview))
{
artist.Overview = data.bio.content;
}
if (!string.IsNullOrEmpty(data.bio.placeformed) && !artist.LockedFields.Contains(MetadataFields.ProductionLocations))
{
artist.AddProductionLocation(data.bio.placeformed);
}
}
2014-01-31 04:50:09 +00:00
if (yearFormed > 0)
{
artist.PremiereDate = new DateTime(yearFormed, 1, 1, 0, 0, 0, DateTimeKind.Utc);
artist.ProductionYear = yearFormed;
}
string imageSize;
var url = LastfmHelper.GetImageUrl(data, out imageSize);
2014-02-09 21:11:11 +00:00
if (!string.IsNullOrEmpty(musicBrainzId) && !string.IsNullOrEmpty(url))
{
LastfmHelper.SaveImageInfo(_config.ApplicationPaths, _logger, musicBrainzId, url, imageSize);
}
2014-01-31 04:50:09 +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)
{
2014-01-31 04:50:09 +00:00
return !String.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
2013-05-06 17:28:29 +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)
{
2014-01-31 04:50:09 +00:00
return String.Concat(
2013-05-06 17:28:29 +00:00
text.Normalize(NormalizationForm.FormD)
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
UnicodeCategory.NonSpacingMark)
).Normalize(NormalizationForm.FormC);
}
2013-05-25 17:55:32 +00:00
/// <summary>
2014-01-31 04:50:09 +00:00
/// Encodes an URL.
2013-05-25 17:55:32 +00:00
/// </summary>
2014-01-31 04:50:09 +00:00
/// <param name="name">The name.</param>
/// <returns>System.String.</returns>
private string UrlEncode(string name)
2013-03-04 01:46:06 +00:00
{
2014-01-31 04:50:09 +00:00
return WebUtility.UrlEncode(name);
2013-03-03 20:55:30 +00:00
}
2014-01-31 04:50:09 +00:00
public string Name
{
2014-01-31 04:50:09 +00:00
get { return "last.fm"; }
}
public int Order
{
2014-02-09 07:27:44 +00:00
get
{
// After fanart & audiodb
return 2;
}
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
2013-03-03 20:55:30 +00:00
}
}