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

323 lines
10 KiB
C#
Raw Normal View History

2014-01-31 19:55:21 +00:00
using MediaBrowser.Common.Net;
2013-03-05 16:48:17 +00:00
using MediaBrowser.Controller.Configuration;
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.Providers;
2013-03-05 16:48:17 +00:00
using MediaBrowser.Model.Serialization;
2014-02-08 20:02:35 +00:00
using MoreLinq;
2013-04-22 04:38:03 +00:00
using System;
2014-01-31 19:55:21 +00:00
using System.Collections.Generic;
2013-09-06 17:26:56 +00:00
using System.IO;
2014-02-08 20:02:35 +00:00
using System.Linq;
2014-01-31 19:55:21 +00:00
using System.Net;
2013-04-22 04:38:03 +00:00
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
{
2014-02-07 03:10:13 +00:00
public class LastfmAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
2013-03-05 16:48:17 +00:00
{
2014-01-31 19:55:21 +00:00
private readonly IJsonSerializer _json;
private readonly IHttpClient _httpClient;
2014-01-31 19:55:21 +00:00
private readonly IServerConfigurationManager _config;
private readonly ILogger _logger;
2013-03-05 16:48:17 +00:00
2014-01-31 19:55:21 +00:00
public LastfmAlbumProvider(IHttpClient httpClient, IJsonSerializer json, IServerConfigurationManager config, ILogger logger)
2013-08-07 16:00:20 +00:00
{
2014-01-31 19:55:21 +00:00
_httpClient = httpClient;
_json = json;
_config = config;
_logger = logger;
2013-08-07 16:00:20 +00:00
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
{
return new List<RemoteSearchResult>();
}
2014-02-07 03:10:13 +00:00
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken)
2013-09-06 17:26:56 +00:00
{
2014-01-31 19:55:21 +00:00
var result = new MetadataResult<MusicAlbum>();
2013-09-06 17:26:56 +00:00
2014-02-07 03:10:13 +00:00
var lastFmData = await GetAlbumResult(id, cancellationToken).ConfigureAwait(false);
2014-01-31 19:55:21 +00:00
if (lastFmData != null && lastFmData.album != null)
2013-06-23 18:55:30 +00:00
{
2014-01-31 19:55:21 +00:00
result.HasMetadata = true;
2014-02-08 20:02:35 +00:00
result.Item = new MusicAlbum();
2014-01-31 19:55:21 +00:00
ProcessAlbumData(result.Item, lastFmData.album);
2013-06-23 18:55:30 +00:00
}
2014-01-31 19:55:21 +00:00
return result;
}
2014-02-07 03:10:13 +00:00
private async Task<LastfmGetAlbumResult> GetAlbumResult(AlbumInfo item, CancellationToken cancellationToken)
2013-04-22 04:38:03 +00:00
{
2013-09-04 17:02:19 +00:00
// Try album release Id
var id = item.GetReleaseId();
if (!string.IsNullOrEmpty(id))
2013-09-04 17:02:19 +00:00
{
var result = await GetAlbumResult(id, cancellationToken).ConfigureAwait(false);
2013-09-04 17:02:19 +00:00
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
id = item.GetReleaseGroupId();
if (!string.IsNullOrEmpty(id))
2013-09-04 17:02:19 +00:00
{
var result = await GetAlbumResult(id, cancellationToken).ConfigureAwait(false);
2013-09-04 17:02:19 +00:00
if (result != null && result.album != null)
{
return result;
}
}
2013-04-22 04:38:03 +00:00
var albumArtist = item.GetAlbumArtist();
2014-02-08 20:02:35 +00:00
// Get each song, distinct by the combination of AlbumArtist and Album
2014-06-23 16:05:19 +00:00
var songs = item.SongInfos.DistinctBy(i => (i.AlbumArtists.FirstOrDefault() ?? string.Empty) + (i.Album ?? string.Empty), StringComparer.OrdinalIgnoreCase).ToList();
2014-02-08 20:02:35 +00:00
2014-06-23 16:05:19 +00:00
foreach (var song in songs.Where(song => !string.IsNullOrEmpty(song.Album) && !string.IsNullOrEmpty(song.AlbumArtists.FirstOrDefault())))
2014-02-08 20:02:35 +00:00
{
2014-06-23 16:05:19 +00:00
var result = await GetAlbumResult(song.AlbumArtists.FirstOrDefault(), song.Album, cancellationToken).ConfigureAwait(false);
2014-02-08 20:02:35 +00:00
if (result != null && result.album != null)
{
return result;
}
}
2014-01-31 19:55:21 +00:00
if (string.IsNullOrEmpty(albumArtist))
2014-01-31 19:55:21 +00:00
{
return null;
2013-04-22 04:38:03 +00:00
}
return await GetAlbumResult(albumArtist, 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
2014-01-31 19:55:21 +00:00
var url = LastfmArtistProvider.RootUrl + string.Format("method=album.getInfo&artist={0}&album={1}&api_key={2}&format=json", UrlEncode(artist), UrlEncode(album), LastfmArtistProvider.ApiKey);
2013-04-22 04:38:03 +00:00
2014-01-31 19:55:21 +00:00
using (var json = await _httpClient.Get(new HttpRequestOptions
{
Url = url,
2014-01-31 19:55:21 +00:00
ResourcePool = LastfmArtistProvider.LastfmResourcePool,
CancellationToken = cancellationToken,
EnableHttpCompression = false
}).ConfigureAwait(false))
2013-04-22 04:38:03 +00:00
{
using (var reader = new StreamReader(json))
{
var jsonText = await reader.ReadToEndAsync().ConfigureAwait(false);
// Fix their bad json
jsonText = jsonText.Replace("\"#text\"", "\"url\"");
2014-01-31 19:55:21 +00:00
return _json.DeserializeFromString<LastfmGetAlbumResult>(jsonText);
}
2013-04-22 04:38:03 +00:00
}
}
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
2014-01-31 19:55:21 +00:00
var url = LastfmArtistProvider.RootUrl + string.Format("method=album.getInfo&mbid={0}&api_key={1}&format=json", musicbraizId, LastfmArtistProvider.ApiKey);
2013-09-04 17:02:19 +00:00
2014-01-31 19:55:21 +00:00
using (var json = await _httpClient.Get(new HttpRequestOptions
2013-09-04 17:02:19 +00:00
{
Url = url,
2014-01-31 19:55:21 +00:00
ResourcePool = LastfmArtistProvider.LastfmResourcePool,
2013-09-04 17:02:19 +00:00
CancellationToken = cancellationToken,
EnableHttpCompression = false
}).ConfigureAwait(false))
{
2014-01-31 19:55:21 +00:00
return _json.DeserializeFromStream<LastfmGetAlbumResult>(json);
2013-09-04 17:02:19 +00:00
}
}
2013-04-22 04:38:03 +00:00
2014-01-31 19:55:21 +00:00
private void ProcessAlbumData(MusicAlbum item, LastfmAlbum data)
2013-03-05 16:48:17 +00:00
{
2014-01-31 19:55:21 +00:00
var overview = data.wiki != null ? data.wiki.content : null;
if (!item.LockedFields.Contains(MetadataFields.Overview))
{
item.Overview = overview;
}
// Only grab the date here if the album doesn't already have one, since id3 tags are preferred
DateTime release;
if (DateTime.TryParse(data.releasedate, out release))
{
// Lastfm sends back null as sometimes 1901, other times 0
if (release.Year > 1901)
{
if (!item.PremiereDate.HasValue)
{
item.PremiereDate = release;
}
if (!item.ProductionYear.HasValue)
{
item.ProductionYear = release.Year;
}
}
}
string imageSize;
var url = LastfmHelper.GetImageUrl(data, out imageSize);
var musicBrainzId = item.GetProviderId(MetadataProviders.MusicBrainzAlbum) ??
2014-01-31 19:55:21 +00:00
item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
2014-02-09 21:11:11 +00:00
if (!string.IsNullOrEmpty(musicBrainzId) && !string.IsNullOrEmpty(url))
{
LastfmHelper.SaveImageInfo(_config.ApplicationPaths, _logger, musicBrainzId, url, imageSize);
}
2013-03-05 16:48:17 +00:00
}
2013-04-22 04:38:03 +00:00
/// <summary>
2014-01-31 19:55:21 +00:00
/// Encodes an URL.
/// </summary>
2014-01-31 19:55:21 +00:00
/// <param name="name">The name.</param>
/// <returns>System.String.</returns>
private string UrlEncode(string name)
{
return WebUtility.UrlEncode(name);
}
public string Name
{
2014-01-31 19:55:21 +00:00
get { return "last.fm"; }
}
2014-01-31 19:55:21 +00:00
public int Order
{
get
{
2014-02-09 21:11:11 +00:00
// After fanart & audiodb
return 2;
}
2014-01-31 19:55:21 +00:00
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
2014-01-31 19:55:21 +00:00
}
2014-01-31 19:55:21 +00:00
#region Result Objects
2014-01-31 19:55:21 +00:00
public class LastfmStats
{
public string listeners { get; set; }
public string playcount { get; set; }
}
2014-01-31 19:55:21 +00:00
public class LastfmTag
{
public string name { get; set; }
public string url { get; set; }
2013-03-05 16:48:17 +00:00
}
2014-01-31 19:55:21 +00:00
public class LastfmTags
{
public List<LastfmTag> tag { get; set; }
}
public class LastfmFormationInfo
{
public string yearfrom { get; set; }
public string yearto { get; set; }
}
public class LastFmBio
{
public string published { get; set; }
public string summary { get; set; }
public string content { get; set; }
public string placeformed { get; set; }
public string yearformed { get; set; }
public List<LastfmFormationInfo> formationlist { get; set; }
}
public class LastFmImage
{
public string url { get; set; }
public string size { get; set; }
}
public class LastfmArtist : IHasLastFmImages
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public string streamable { get; set; }
public string ontour { get; set; }
public LastfmStats stats { get; set; }
public List<LastfmArtist> similar { get; set; }
public LastfmTags tags { get; set; }
public LastFmBio bio { get; set; }
public List<LastFmImage> image { get; set; }
}
public class LastfmAlbum : IHasLastFmImages
{
public string name { get; set; }
public string artist { get; set; }
public string id { get; set; }
public string mbid { get; set; }
public string releasedate { get; set; }
public int listeners { get; set; }
public int playcount { get; set; }
public LastfmTags toptags { get; set; }
public LastFmBio wiki { get; set; }
public List<LastFmImage> image { get; set; }
}
public interface IHasLastFmImages
{
List<LastFmImage> image { get; set; }
}
public class LastfmGetAlbumResult
{
public LastfmAlbum album { get; set; }
}
public class LastfmGetArtistResult
{
public LastfmArtist artist { get; set; }
}
public class Artistmatches
{
public List<LastfmArtist> artist { get; set; }
}
public class LastfmArtistSearchResult
{
public Artistmatches artistmatches { get; set; }
}
public class LastfmArtistSearchResults
{
public LastfmArtistSearchResult results { get; set; }
}
#endregion
2013-03-05 16:48:17 +00:00
}