2014-01-29 01:45:48 +00:00
|
|
|
|
using MediaBrowser.Common;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-20 04:53:15 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
using System;
|
2014-02-20 04:53:15 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Music
|
|
|
|
|
{
|
2014-02-07 03:10:13 +00:00
|
|
|
|
public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
|
|
|
|
internal static MusicBrainzAlbumProvider Current;
|
|
|
|
|
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-01-29 01:45:48 +00:00
|
|
|
|
private readonly IApplicationHost _appHost;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public MusicBrainzAlbumProvider(IHttpClient httpClient, IApplicationHost appHost)
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
2014-01-29 01:45:48 +00:00
|
|
|
|
_appHost = appHost;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
Current = this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 04:53:15 +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-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-02-07 22:40:03 +00:00
|
|
|
|
var releaseId = id.GetReleaseId();
|
|
|
|
|
var releaseGroupId = id.GetReleaseGroupId();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-02-07 22:40:03 +00:00
|
|
|
|
var result = new MetadataResult<MusicAlbum>
|
|
|
|
|
{
|
|
|
|
|
Item = new MusicAlbum()
|
|
|
|
|
};
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(releaseId))
|
|
|
|
|
{
|
2014-02-09 07:27:44 +00:00
|
|
|
|
var artistMusicBrainzId = id.GetMusicBrainzArtistId();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-02-07 22:40:03 +00:00
|
|
|
|
var releaseResult = await GetReleaseResult(artistMusicBrainzId, id.GetAlbumArtist(), id.Name, cancellationToken).ConfigureAwait(false);
|
2014-01-31 19:55:21 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseResult.ReleaseId))
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
releaseId = releaseResult.ReleaseId;
|
|
|
|
|
result.HasMetadata = true;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(releaseResult.ReleaseGroupId))
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
releaseGroupId = releaseResult.ReleaseGroupId;
|
|
|
|
|
result.HasMetadata = true;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have a release Id but not a release group Id...
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseId) && string.IsNullOrEmpty(releaseGroupId))
|
|
|
|
|
{
|
|
|
|
|
releaseGroupId = await GetReleaseGroupId(releaseId, cancellationToken).ConfigureAwait(false);
|
2014-01-31 19:55:21 +00:00
|
|
|
|
result.HasMetadata = true;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 22:40:03 +00:00
|
|
|
|
if (result.HasMetadata)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
|
|
|
{
|
|
|
|
|
result.Item.SetProviderId(MetadataProviders.MusicBrainzAlbum, releaseId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseGroupId))
|
|
|
|
|
{
|
|
|
|
|
result.Item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, releaseGroupId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return result;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public string Name
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
get { return "MusicBrainz"; }
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
private Task<ReleaseResult> GetReleaseResult(string artistMusicBrainId, string artistName, string albumName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(artistMusicBrainId))
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return GetReleaseResult(albumName, artistMusicBrainId, cancellationToken);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
return GetReleaseResultByArtistName(albumName, artistName, cancellationToken);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ReleaseResult> GetReleaseResult(string albumName, string artistId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-01-29 05:18:12 +00:00
|
|
|
|
var url = string.Format("http://www.musicbrainz.org/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2013-11-12 17:12:11 +00:00
|
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
|
artistId);
|
|
|
|
|
|
|
|
|
|
var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return GetReleaseResult(doc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-01-29 05:18:12 +00:00
|
|
|
|
var url = string.Format("http://www.musicbrainz.org/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2013-11-12 17:12:11 +00:00
|
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
|
WebUtility.UrlEncode(artistName));
|
|
|
|
|
|
|
|
|
|
var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return GetReleaseResult(doc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ReleaseResult GetReleaseResult(XmlDocument doc)
|
|
|
|
|
{
|
|
|
|
|
var ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
|
|
|
|
|
|
|
|
|
var result = new ReleaseResult
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var releaseIdNode = doc.SelectSingleNode("//mb:release-list/mb:release/@id", ns);
|
|
|
|
|
|
|
|
|
|
if (releaseIdNode != null)
|
|
|
|
|
{
|
|
|
|
|
result.ReleaseId = releaseIdNode.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var releaseGroupIdNode = doc.SelectSingleNode("//mb:release-list/mb:release/mb:release-group/@id", ns);
|
|
|
|
|
|
|
|
|
|
if (releaseGroupIdNode != null)
|
|
|
|
|
{
|
|
|
|
|
result.ReleaseGroupId = releaseGroupIdNode.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class ReleaseResult
|
|
|
|
|
{
|
|
|
|
|
public string ReleaseId;
|
|
|
|
|
public string ReleaseGroupId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the release group id internal.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="releaseEntryId">The release entry id.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
|
|
|
|
private async Task<string> GetReleaseGroupId(string releaseEntryId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var url = string.Format("http://www.musicbrainz.org/ws/2/release-group/?query=reid:{0}", releaseEntryId);
|
|
|
|
|
|
|
|
|
|
var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
|
|
|
|
var node = doc.SelectSingleNode("//mb:release-group-list/mb:release-group/@id", ns);
|
|
|
|
|
|
|
|
|
|
return node != null ? node.Value : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _last music brainz request
|
|
|
|
|
/// </summary>
|
|
|
|
|
private DateTime _lastRequestDate = DateTime.MinValue;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _music brainz resource pool
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly SemaphoreSlim _musicBrainzResourcePool = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the music brainz response.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url">The URL.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{XmlDocument}.</returns>
|
|
|
|
|
internal async Task<XmlDocument> GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
await _musicBrainzResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var diff = 1500 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
|
|
|
|
|
|
|
|
|
|
// MusicBrainz is extremely adamant about limiting to one request per second
|
|
|
|
|
|
|
|
|
|
if (diff > 0)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_lastRequestDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
|
2014-01-29 01:45:48 +00:00
|
|
|
|
var userAgent = _appHost.Name + "/" + _appHost.ApplicationVersion;
|
|
|
|
|
|
2013-11-12 17:12:11 +00:00
|
|
|
|
using (var xml = await _httpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
2014-01-29 01:45:48 +00:00
|
|
|
|
UserAgent = userAgent
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
using (var oReader = new StreamReader(xml, Encoding.UTF8))
|
|
|
|
|
{
|
|
|
|
|
doc.Load(oReader);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_lastRequestDate = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
_musicBrainzResourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-31 19:55:21 +00:00
|
|
|
|
public int Order
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-01-31 19:55:21 +00:00
|
|
|
|
get { return 0; }
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|