|
@@ -1,6 +1,6 @@
|
|
using System;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
-using System.Globalization;
|
|
|
|
|
|
+using System.Diagnostics;
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net;
|
|
@@ -28,9 +28,16 @@ namespace MediaBrowser.Providers.Music
|
|
private readonly IApplicationHost _appHost;
|
|
private readonly IApplicationHost _appHost;
|
|
private readonly ILogger _logger;
|
|
private readonly ILogger _logger;
|
|
private readonly IJsonSerializer _json;
|
|
private readonly IJsonSerializer _json;
|
|
|
|
+ private Stopwatch _stopWatchMusicBrainz = new Stopwatch();
|
|
|
|
|
|
public readonly string MusicBrainzBaseUrl;
|
|
public readonly string MusicBrainzBaseUrl;
|
|
|
|
|
|
|
|
+ // The Jellyfin user-agent is unrestricted but source IP must not exceed
|
|
|
|
+ // one request per second, therefore we rate limit to avoid throttling.
|
|
|
|
+ // Be prudent, use a value slightly above the minimun required.
|
|
|
|
+ // https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting
|
|
|
|
+ private const long MusicBrainzQueryIntervalMs = 1050u;
|
|
|
|
+
|
|
public MusicBrainzAlbumProvider(
|
|
public MusicBrainzAlbumProvider(
|
|
IHttpClient httpClient,
|
|
IHttpClient httpClient,
|
|
IApplicationHost appHost,
|
|
IApplicationHost appHost,
|
|
@@ -45,6 +52,9 @@ namespace MediaBrowser.Providers.Music
|
|
|
|
|
|
MusicBrainzBaseUrl = configuration["MusicBrainz:BaseUrl"];
|
|
MusicBrainzBaseUrl = configuration["MusicBrainz:BaseUrl"];
|
|
|
|
|
|
|
|
+ // Use a stopwatch to ensure we don't exceed the MusicBrainz rate limit
|
|
|
|
+ _stopWatchMusicBrainz.Start();
|
|
|
|
+
|
|
Current = this;
|
|
Current = this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -54,8 +64,6 @@ namespace MediaBrowser.Providers.Music
|
|
var releaseGroupId = searchInfo.GetReleaseGroupId();
|
|
var releaseGroupId = searchInfo.GetReleaseGroupId();
|
|
|
|
|
|
string url;
|
|
string url;
|
|
- var isNameSearch = false;
|
|
|
|
- bool forceMusicBrainzProper = false;
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
{
|
|
{
|
|
@@ -64,7 +72,6 @@ namespace MediaBrowser.Providers.Music
|
|
else if (!string.IsNullOrEmpty(releaseGroupId))
|
|
else if (!string.IsNullOrEmpty(releaseGroupId))
|
|
{
|
|
{
|
|
url = string.Format("/ws/2/release?release-group={0}", releaseGroupId);
|
|
url = string.Format("/ws/2/release?release-group={0}", releaseGroupId);
|
|
- forceMusicBrainzProper = true;
|
|
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -78,8 +85,6 @@ namespace MediaBrowser.Providers.Music
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- isNameSearch = true;
|
|
|
|
-
|
|
|
|
// I'm sure there is a better way but for now it resolves search for 12" Mixes
|
|
// I'm sure there is a better way but for now it resolves search for 12" Mixes
|
|
var queryName = searchInfo.Name.Replace("\"", string.Empty);
|
|
var queryName = searchInfo.Name.Replace("\"", string.Empty);
|
|
|
|
|
|
@@ -91,7 +96,7 @@ namespace MediaBrowser.Providers.Music
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
{
|
|
{
|
|
- using (var response = await GetMusicBrainzResponse(url, isNameSearch, forceMusicBrainzProper, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
|
+ using (var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
|
{
|
|
{
|
|
using (var stream = response.Content)
|
|
using (var stream = response.Content)
|
|
{
|
|
{
|
|
@@ -247,7 +252,7 @@ namespace MediaBrowser.Providers.Music
|
|
WebUtility.UrlEncode(albumName),
|
|
WebUtility.UrlEncode(albumName),
|
|
artistId);
|
|
artistId);
|
|
|
|
|
|
- using (var response = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
|
+ using (var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
|
using (var stream = response.Content)
|
|
using (var stream = response.Content)
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
{
|
|
{
|
|
@@ -272,7 +277,7 @@ namespace MediaBrowser.Providers.Music
|
|
WebUtility.UrlEncode(albumName),
|
|
WebUtility.UrlEncode(albumName),
|
|
WebUtility.UrlEncode(artistName));
|
|
WebUtility.UrlEncode(artistName));
|
|
|
|
|
|
- using (var response = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
|
+ using (var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
|
using (var stream = response.Content)
|
|
using (var stream = response.Content)
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
{
|
|
{
|
|
@@ -589,7 +594,7 @@ namespace MediaBrowser.Providers.Music
|
|
{
|
|
{
|
|
var url = string.Format("/ws/2/release?release-group={0}", releaseGroupId);
|
|
var url = string.Format("/ws/2/release?release-group={0}", releaseGroupId);
|
|
|
|
|
|
- using (var response = await GetMusicBrainzResponse(url, true, true, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
|
+ using (var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
|
using (var stream = response.Content)
|
|
using (var stream = response.Content)
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
{
|
|
{
|
|
@@ -625,7 +630,7 @@ namespace MediaBrowser.Providers.Music
|
|
{
|
|
{
|
|
var url = string.Format("/ws/2/release-group/?query=reid:{0}", releaseEntryId);
|
|
var url = string.Format("/ws/2/release-group/?query=reid:{0}", releaseEntryId);
|
|
|
|
|
|
- using (var response = await GetMusicBrainzResponse(url, false, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
|
+ using (var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
|
using (var stream = response.Content)
|
|
using (var stream = response.Content)
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
|
{
|
|
{
|
|
@@ -710,34 +715,29 @@ namespace MediaBrowser.Providers.Music
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- internal Task<HttpResponseInfo> GetMusicBrainzResponse(string url, bool isSearch, CancellationToken cancellationToken)
|
|
|
|
- {
|
|
|
|
- return GetMusicBrainzResponse(url, isSearch, false, cancellationToken);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// Gets the music brainz response.
|
|
|
|
|
|
+ /// Makes request to MusicBrainz server and awaits a response.
|
|
/// </summary>
|
|
/// </summary>
|
|
- internal async Task<HttpResponseInfo> GetMusicBrainzResponse(string url, bool isSearch, bool forceMusicBrainzProper, CancellationToken cancellationToken)
|
|
|
|
|
|
+ internal async Task<HttpResponseInfo> GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
|
|
{
|
|
{
|
|
- var urlInfo = new MbzUrl(MusicBrainzBaseUrl, 1000);
|
|
|
|
- var throttleMs = urlInfo.throttleMs;
|
|
|
|
-
|
|
|
|
- if (throttleMs > 0)
|
|
|
|
|
|
+ if (_stopWatchMusicBrainz.ElapsedMilliseconds < MusicBrainzQueryIntervalMs)
|
|
{
|
|
{
|
|
// MusicBrainz is extremely adamant about limiting to one request per second
|
|
// MusicBrainz is extremely adamant about limiting to one request per second
|
|
- _logger.LogDebug("Throttling MusicBrainz by {0}ms", throttleMs.ToString(CultureInfo.InvariantCulture));
|
|
|
|
- await Task.Delay(throttleMs, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
+ var delayMs = MusicBrainzQueryIntervalMs - _stopWatchMusicBrainz.ElapsedMilliseconds;
|
|
|
|
+ await Task.Delay((int)delayMs, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
|
|
- url = urlInfo.url.TrimEnd('/') + url;
|
|
|
|
|
|
+ _logger.LogDebug("MusicBrainz time since previous request: {0}ms", _stopWatchMusicBrainz.ElapsedMilliseconds);
|
|
|
|
+ _stopWatchMusicBrainz.Restart();
|
|
|
|
|
|
var options = new HttpRequestOptions
|
|
var options = new HttpRequestOptions
|
|
{
|
|
{
|
|
- Url = url,
|
|
|
|
|
|
+ Url = MusicBrainzBaseUrl.TrimEnd('/') + url,
|
|
CancellationToken = cancellationToken,
|
|
CancellationToken = cancellationToken,
|
|
- UserAgent = _appHost.ApplicationUserAgent,
|
|
|
|
- BufferContent = throttleMs > 0
|
|
|
|
|
|
+ // MusicBrainz request a contact email address is supplied, as comment, in user agent field:
|
|
|
|
+ // https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting#User-Agent
|
|
|
|
+ UserAgent = string.Format("{0} ( {1} )", _appHost.ApplicationUserAgent, _appHost.ApplicationUserAgentAddress),
|
|
|
|
+ BufferContent = false
|
|
};
|
|
};
|
|
|
|
|
|
return await _httpClient.SendAsync(options, "GET").ConfigureAwait(false);
|
|
return await _httpClient.SendAsync(options, "GET").ConfigureAwait(false);
|
|
@@ -749,17 +749,5 @@ namespace MediaBrowser.Providers.Music
|
|
{
|
|
{
|
|
throw new NotImplementedException();
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
-
|
|
|
|
- internal class MbzUrl
|
|
|
|
- {
|
|
|
|
- internal MbzUrl(string url, int throttleMs)
|
|
|
|
- {
|
|
|
|
- this.url = url;
|
|
|
|
- this.throttleMs = throttleMs;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public string url { get; set; }
|
|
|
|
- public int throttleMs { get; set; }
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|