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-09-04 01:44:40 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
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;
|
2014-03-14 03:23:58 +00:00
|
|
|
|
using System.Linq;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
2016-06-20 17:07:02 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2014-09-04 01:44:40 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2016-06-20 17:07:02 +00:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
public static string MusicBrainzBaseUrl = "https://www.musicbrainz.org";
|
2016-05-23 03:54:22 +00:00
|
|
|
|
|
2016-06-20 17:07:02 +00:00
|
|
|
|
public MusicBrainzAlbumProvider(IHttpClient httpClient, IApplicationHost appHost, ILogger logger, IJsonSerializer json)
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
2014-01-29 01:45:48 +00:00
|
|
|
|
_appHost = appHost;
|
2014-09-04 01:44:40 +00:00
|
|
|
|
_logger = logger;
|
2016-06-20 17:07:02 +00:00
|
|
|
|
_json = json;
|
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)
|
|
|
|
|
{
|
2014-03-14 03:23:58 +00:00
|
|
|
|
var releaseId = searchInfo.GetReleaseId();
|
|
|
|
|
|
|
|
|
|
string url = null;
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var isNameSearch = false;
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
url = string.Format("/ws/2/release/?query=reid:{0}", releaseId);
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(artistMusicBrainzId))
|
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
url = string.Format("/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2014-03-14 03:23:58 +00:00
|
|
|
|
WebUtility.UrlEncode(searchInfo.Name),
|
|
|
|
|
artistMusicBrainzId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-28 16:50:33 +00:00
|
|
|
|
isNameSearch = true;
|
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
url = string.Format("/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2014-03-14 03:23:58 +00:00
|
|
|
|
WebUtility.UrlEncode(searchInfo.Name),
|
|
|
|
|
WebUtility.UrlEncode(searchInfo.GetAlbumArtist()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
|
|
|
{
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var doc = await GetMusicBrainzResponse(url, isNameSearch, cancellationToken).ConfigureAwait(false);
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
|
|
|
|
return GetResultsFromResponse(doc);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 04:53:15 +00:00
|
|
|
|
return new List<RemoteSearchResult>();
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlDocument doc)
|
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return ReleaseResult.Parse(doc).Select(i =>
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
var result = new RemoteSearchResult
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
Name = i.Title
|
|
|
|
|
};
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-08-16 18:45:57 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(i.ReleaseId))
|
|
|
|
|
{
|
|
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseId);
|
|
|
|
|
}
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
|
|
|
|
|
{
|
|
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseGroupId);
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return result;
|
|
|
|
|
});
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2015-08-07 14:21:29 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(releaseId) || !string.IsNullOrEmpty(releaseGroupId))
|
|
|
|
|
{
|
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
}
|
|
|
|
|
|
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-06-23 16:05:19 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(artistName))
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(new ReleaseResult());
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var url = string.Format("/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2013-11-12 17:12:11 +00:00
|
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
|
artistId);
|
|
|
|
|
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return ReleaseResult.Parse(doc, 1).FirstOrDefault();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var url = string.Format("/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2013-11-12 17:12:11 +00:00
|
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
|
WebUtility.UrlEncode(artistName));
|
|
|
|
|
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return ReleaseResult.Parse(doc, 1).FirstOrDefault();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
private class ReleaseResult
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
public string ReleaseId;
|
|
|
|
|
public string ReleaseGroupId;
|
2016-08-16 18:45:57 +00:00
|
|
|
|
public string Title;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-08-16 18:45:57 +00:00
|
|
|
|
public static List<ReleaseResult> Parse(XmlDocument doc, int? limit = null)
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var docElem = doc.DocumentElement;
|
2016-08-16 18:45:57 +00:00
|
|
|
|
var list = new List<ReleaseResult>();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
if (docElem == null)
|
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return list;
|
2016-06-15 19:52:38 +00:00
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var releaseList = docElem.FirstChild;
|
|
|
|
|
if (releaseList == null)
|
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return list;
|
2016-06-15 19:52:38 +00:00
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var nodes = releaseList.ChildNodes;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
if (nodes != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var node in nodes.Cast<XmlNode>())
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(node.Name, "release", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
var releaseId = node.Attributes["id"].Value;
|
|
|
|
|
var releaseGroupId = GetReleaseGroupIdFromReleaseNode(node);
|
|
|
|
|
|
|
|
|
|
list.Add(new ReleaseResult
|
|
|
|
|
{
|
|
|
|
|
ReleaseId = releaseId,
|
|
|
|
|
ReleaseGroupId = releaseGroupId,
|
|
|
|
|
Title = GetTitleFromReleaseNode(node)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (limit.HasValue && list.Count >= limit.Value)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2016-06-15 19:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-08-16 18:45:57 +00:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetTitleFromReleaseNode(XmlNode node)
|
|
|
|
|
{
|
|
|
|
|
var subNodes = node.ChildNodes;
|
|
|
|
|
if (subNodes != null)
|
2016-06-15 19:52:38 +00:00
|
|
|
|
{
|
2016-08-16 18:45:57 +00:00
|
|
|
|
foreach (var subNode in subNodes.Cast<XmlNode>())
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(subNode.Name, "title", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return subNode.InnerText;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
private static string GetReleaseGroupIdFromReleaseNode(XmlNode node)
|
|
|
|
|
{
|
|
|
|
|
var subNodes = node.ChildNodes;
|
|
|
|
|
if (subNodes != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var subNode in subNodes.Cast<XmlNode>())
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(subNode.Name, "release-group", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return subNode.Attributes["id"].Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var url = string.Format("/ws/2/release-group/?query=reid:{0}", releaseEntryId);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var doc = await GetMusicBrainzResponse(url, false, cancellationToken).ConfigureAwait(false);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 20:14:04 +00:00
|
|
|
|
var docElem = doc.DocumentElement;
|
|
|
|
|
|
|
|
|
|
if (docElem == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var releaseList = docElem.FirstChild;
|
|
|
|
|
if (releaseList == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 20:14:04 +00:00
|
|
|
|
var nodes = releaseList.ChildNodes;
|
|
|
|
|
|
|
|
|
|
if (nodes != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var node in nodes.Cast<XmlNode>())
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(node.Name, "release-group", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return node.Attributes["id"].Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _music brainz resource pool
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly SemaphoreSlim _musicBrainzResourcePool = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
private long _lastMbzUrlQueryTicks = 0;
|
|
|
|
|
private List<MbzUrl> _mbzUrls = null;
|
|
|
|
|
private MbzUrl _chosenUrl;
|
|
|
|
|
|
|
|
|
|
private async Task<MbzUrl> GetMbzUrl()
|
|
|
|
|
{
|
2016-06-20 17:07:02 +00:00
|
|
|
|
if (_chosenUrl == null || _mbzUrls == null || (DateTime.UtcNow.Ticks - _lastMbzUrlQueryTicks) > TimeSpan.FromHours(12).Ticks)
|
2016-06-15 19:52:38 +00:00
|
|
|
|
{
|
2016-06-20 17:07:02 +00:00
|
|
|
|
var urls = await RefreshMzbUrls().ConfigureAwait(false);
|
2016-06-15 19:52:38 +00:00
|
|
|
|
|
2016-06-23 05:25:30 +00:00
|
|
|
|
if (urls.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
_chosenUrl = urls[new Random().Next(0, urls.Count)];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_chosenUrl = urls[0];
|
|
|
|
|
}
|
2016-06-15 19:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _chosenUrl;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-20 17:07:02 +00:00
|
|
|
|
private async Task<List<MbzUrl>> RefreshMzbUrls()
|
2016-06-15 19:52:38 +00:00
|
|
|
|
{
|
2016-06-20 17:07:02 +00:00
|
|
|
|
List<MbzUrl> list;
|
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-06-20 17:07:02 +00:00
|
|
|
|
var options = new HttpRequestOptions
|
2016-06-15 19:52:38 +00:00
|
|
|
|
{
|
2016-06-20 17:07:02 +00:00
|
|
|
|
Url = "https://mb3admin.com/admin/service/standards/musicBrainzUrls",
|
|
|
|
|
UserAgent = _appHost.Name + "/" + _appHost.ApplicationVersion
|
2016-06-15 19:52:38 +00:00
|
|
|
|
};
|
2016-06-20 17:07:02 +00:00
|
|
|
|
|
|
|
|
|
using (var stream = await _httpClient.Get(options).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
list = _json.DeserializeFromStream<List<MbzUrl>>(stream);
|
|
|
|
|
}
|
2016-06-23 05:25:30 +00:00
|
|
|
|
_lastMbzUrlQueryTicks = DateTime.UtcNow.Ticks;
|
2016-06-15 19:52:38 +00:00
|
|
|
|
}
|
2016-06-20 17:07:02 +00:00
|
|
|
|
catch (Exception ex)
|
2016-06-15 19:52:38 +00:00
|
|
|
|
{
|
2016-06-20 17:07:02 +00:00
|
|
|
|
_logger.ErrorException("Error getting music brainz info", ex);
|
|
|
|
|
|
|
|
|
|
list = new List<MbzUrl>
|
2016-06-15 19:52:38 +00:00
|
|
|
|
{
|
|
|
|
|
new MbzUrl
|
|
|
|
|
{
|
|
|
|
|
url = MusicBrainzBaseUrl,
|
|
|
|
|
throttleMs = 1000
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-06-20 17:07:02 +00:00
|
|
|
|
|
|
|
|
|
_mbzUrls = list.ToList();
|
|
|
|
|
|
|
|
|
|
return list;
|
2016-06-15 19:52:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-12 17:12:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the music brainz response.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="url">The URL.</param>
|
2014-09-28 16:50:33 +00:00
|
|
|
|
/// <param name="isSearch">if set to <c>true</c> [is search].</param>
|
2013-11-12 17:12:11 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{XmlDocument}.</returns>
|
2014-09-28 16:50:33 +00:00
|
|
|
|
internal async Task<XmlDocument> GetMusicBrainzResponse(string url, bool isSearch, CancellationToken cancellationToken)
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var urlInfo = await GetMbzUrl().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (urlInfo.throttleMs > 0)
|
|
|
|
|
{
|
|
|
|
|
// MusicBrainz is extremely adamant about limiting to one request per second
|
|
|
|
|
await Task.Delay(urlInfo.throttleMs, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
url = urlInfo.url.TrimEnd('/') + url;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-10-11 20:38:13 +00:00
|
|
|
|
var doc = new XmlDocument();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
2014-10-11 20:38:13 +00:00
|
|
|
|
var options = new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
UserAgent = _appHost.Name + "/" + _appHost.ApplicationVersion,
|
|
|
|
|
ResourcePool = _musicBrainzResourcePool
|
|
|
|
|
};
|
2014-09-28 16:50:33 +00:00
|
|
|
|
|
2014-10-11 20:38:13 +00:00
|
|
|
|
using (var xml = await _httpClient.Get(options).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
using (var oReader = new StreamReader(xml, Encoding.UTF8))
|
2013-11-12 17:12:11 +00:00
|
|
|
|
{
|
2014-10-11 20:38:13 +00:00
|
|
|
|
doc.Load(oReader);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-11 20:38:13 +00:00
|
|
|
|
return doc;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2016-06-15 19:52:38 +00:00
|
|
|
|
|
|
|
|
|
internal class MbzUrl
|
|
|
|
|
{
|
|
|
|
|
public string url { get; set; }
|
|
|
|
|
public int throttleMs { get; set; }
|
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|