2021-10-26 13:49:01 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-07-23 00:33:19 +00:00
|
|
|
#pragma warning disable CS1591, SA1401
|
2020-06-19 18:24:13 +00:00
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2014-02-20 04:53:15 +00:00
|
|
|
using System.Collections.Generic;
|
2019-03-13 20:31:21 +00:00
|
|
|
using System.Diagnostics;
|
2019-09-10 20:37:53 +00:00
|
|
|
using System.Globalization;
|
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;
|
2020-02-23 09:53:51 +00:00
|
|
|
using System.Net.Http;
|
2013-11-12 17:12:11 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml;
|
2020-08-31 17:05:21 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2020-02-22 06:04:52 +00:00
|
|
|
using MediaBrowser.Providers.Plugins.MusicBrainz;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-11-12 17:12:11 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Music
|
|
|
|
{
|
2021-07-23 03:16:38 +00:00
|
|
|
public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder, IDisposable
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2019-03-15 19:29:04 +00:00
|
|
|
/// <summary>
|
|
|
|
/// For each single MB lookup/search, this is the maximum number of
|
|
|
|
/// attempts that shall be made whilst receiving a 503 Server
|
|
|
|
/// Unavailable (indicating throttled) response.
|
|
|
|
/// </summary>
|
|
|
|
private const uint MusicBrainzQueryAttempts = 5u;
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The Jellyfin user-agent is unrestricted but source IP must not exceed
|
|
|
|
/// one request per second, therefore we rate limit to avoid throttling.
|
2022-08-15 10:48:34 +00:00
|
|
|
/// Be prudent, use a value slightly above the minimum required.
|
2021-07-23 00:33:19 +00:00
|
|
|
/// https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting.
|
2020-09-07 11:20:39 +00:00
|
|
|
/// </summary>
|
|
|
|
private readonly long _musicBrainzQueryIntervalMs;
|
2019-09-10 20:37:53 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-06-06 00:15:56 +00:00
|
|
|
private readonly ILogger<MusicBrainzAlbumProvider> _logger;
|
2019-09-10 20:37:53 +00:00
|
|
|
|
|
|
|
private readonly string _musicBrainzBaseUrl;
|
|
|
|
|
2020-10-03 00:14:57 +00:00
|
|
|
private SemaphoreSlim _apiRequestLock = new SemaphoreSlim(1, 1);
|
2019-09-10 20:37:53 +00:00
|
|
|
private Stopwatch _stopWatchMusicBrainz = new Stopwatch();
|
|
|
|
|
2019-03-08 16:15:52 +00:00
|
|
|
public MusicBrainzAlbumProvider(
|
2020-08-17 19:10:02 +00:00
|
|
|
IHttpClientFactory httpClientFactory,
|
2020-03-03 22:07:10 +00:00
|
|
|
ILogger<MusicBrainzAlbumProvider> logger)
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2014-09-04 01:44:40 +00:00
|
|
|
_logger = logger;
|
2019-03-12 15:37:18 +00:00
|
|
|
|
2020-02-22 06:04:52 +00:00
|
|
|
_musicBrainzBaseUrl = Plugin.Instance.Configuration.Server;
|
|
|
|
_musicBrainzQueryIntervalMs = Plugin.Instance.Configuration.RateLimit;
|
2019-03-12 15:37:18 +00:00
|
|
|
|
2019-03-13 20:31:21 +00:00
|
|
|
// Use a stopwatch to ensure we don't exceed the MusicBrainz rate limit
|
|
|
|
_stopWatchMusicBrainz.Start();
|
|
|
|
|
2013-11-12 17:12:11 +00:00
|
|
|
Current = this;
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
internal static MusicBrainzAlbumProvider Current { get; private set; }
|
|
|
|
|
2019-09-10 20:37:53 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "MusicBrainz";
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public int Order => 0;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
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();
|
2017-03-15 19:57:18 +00:00
|
|
|
var releaseGroupId = searchInfo.GetReleaseGroupId();
|
2014-03-14 03:23:58 +00:00
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
string url;
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
url = "/ws/2/release/?query=reid:" + releaseId.ToString(CultureInfo.InvariantCulture);
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
2017-03-15 19:57:18 +00:00
|
|
|
else if (!string.IsNullOrEmpty(releaseGroupId))
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
url = "/ws/2/release?release-group=" + releaseGroupId.ToString(CultureInfo.InvariantCulture);
|
2017-03-15 19:57:18 +00:00
|
|
|
}
|
2014-03-14 03:23:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(artistMusicBrainzId))
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
url = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2014-03-14 03:23:58 +00:00
|
|
|
WebUtility.UrlEncode(searchInfo.Name),
|
|
|
|
artistMusicBrainzId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
// I'm sure there is a better way but for now it resolves search for 12" Mixes
|
2020-09-07 11:20:39 +00:00
|
|
|
var queryName = searchInfo.Name.Replace("\"", string.Empty, StringComparison.Ordinal);
|
2018-09-12 17:26:21 +00:00
|
|
|
|
2019-09-10 20:37:53 +00:00
|
|
|
url = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2020-02-22 06:04:52 +00:00
|
|
|
WebUtility.UrlEncode(queryName),
|
|
|
|
WebUtility.UrlEncode(searchInfo.GetAlbumArtist()));
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
return GetResultsFromResponse(stream);
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
using var oReader = new StreamReader(stream, Encoding.UTF8);
|
|
|
|
var settings = new XmlReaderSettings()
|
|
|
|
{
|
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
|
|
|
|
|
|
|
using var reader = XmlReader.Create(oReader, settings);
|
|
|
|
var results = ReleaseResult.Parse(reader);
|
|
|
|
|
|
|
|
return results.Select(i =>
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
var result = new RemoteSearchResult
|
2019-02-01 16:43:31 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
Name = i.Title,
|
|
|
|
ProductionYear = i.Year
|
2019-02-01 16:43:31 +00:00
|
|
|
};
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
if (i.Artists.Count > 0)
|
2016-08-16 18:45:57 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
result.AlbumArtist = new RemoteSearchResult
|
2016-10-27 21:05:25 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
SearchProviderName = Name,
|
|
|
|
Name = i.Artists[0].Item1
|
|
|
|
};
|
2017-10-28 04:20:18 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
result.AlbumArtist.SetProviderId(MetadataProvider.MusicBrainzArtist, i.Artists[0].Item2);
|
|
|
|
}
|
2019-09-10 20:37:53 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(i.ReleaseId))
|
|
|
|
{
|
|
|
|
result.SetProviderId(MetadataProvider.MusicBrainzAlbum, i.ReleaseId);
|
|
|
|
}
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
|
|
|
|
{
|
|
|
|
result.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, i.ReleaseGroupId);
|
2016-10-27 21:05:25 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 20:37:53 +00:00
|
|
|
/// <inheritdoc />
|
2021-06-27 00:00:17 +00:00
|
|
|
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2021-06-27 00:00:17 +00:00
|
|
|
var releaseId = info.GetReleaseId();
|
|
|
|
var releaseGroupId = info.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
|
|
|
|
2017-03-15 19:57:18 +00:00
|
|
|
// If we have a release group Id but not a release Id...
|
|
|
|
if (string.IsNullOrWhiteSpace(releaseId) && !string.IsNullOrWhiteSpace(releaseGroupId))
|
|
|
|
{
|
|
|
|
releaseId = await GetReleaseIdFromReleaseGroupId(releaseGroupId, cancellationToken).ConfigureAwait(false);
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(releaseId))
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2021-06-27 00:00:17 +00:00
|
|
|
var artistMusicBrainzId = info.GetMusicBrainzArtistId();
|
2013-11-12 17:12:11 +00:00
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
var releaseResult = await GetReleaseResult(artistMusicBrainzId, info.GetAlbumArtist(), info.Name, cancellationToken).ConfigureAwait(false);
|
2014-01-31 19:55:21 +00:00
|
|
|
|
2016-10-08 18:51:07 +00:00
|
|
|
if (releaseResult != null)
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2017-03-15 19:57:18 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseResult.ReleaseId))
|
2016-10-08 18:51:07 +00:00
|
|
|
{
|
|
|
|
releaseId = releaseResult.ReleaseId;
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
2017-03-15 19:57:18 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseResult.ReleaseGroupId))
|
2016-10-08 18:51:07 +00:00
|
|
|
{
|
|
|
|
releaseGroupId = releaseResult.ReleaseGroupId;
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Item.ProductionYear = releaseResult.Year;
|
|
|
|
result.Item.Overview = releaseResult.Overview;
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a release Id but not a release group Id...
|
2017-03-15 19:57:18 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseId) && string.IsNullOrWhiteSpace(releaseGroupId))
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2017-03-15 19:57:18 +00:00
|
|
|
releaseGroupId = await GetReleaseGroupFromReleaseId(releaseId, cancellationToken).ConfigureAwait(false);
|
2014-01-31 19:55:21 +00:00
|
|
|
result.HasMetadata = true;
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 19:57:18 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseId) || !string.IsNullOrWhiteSpace(releaseGroupId))
|
2015-08-07 14:21:29 +00:00
|
|
|
{
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 22:40:03 +00:00
|
|
|
if (result.HasMetadata)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
result.Item.SetProviderId(MetadataProvider.MusicBrainzAlbum, releaseId);
|
2014-02-07 22:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseGroupId))
|
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
result.Item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, releaseGroupId);
|
2014-02-07 22:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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)
|
|
|
|
{
|
2020-09-07 11:20:39 +00:00
|
|
|
var url = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2013-11-12 17:12:11 +00:00
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
artistId);
|
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
using var oReader = new StreamReader(stream, Encoding.UTF8);
|
|
|
|
var settings = new XmlReaderSettings
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var reader = XmlReader.Create(oReader, settings);
|
|
|
|
return ReleaseResult.Parse(reader).FirstOrDefault();
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
var url = string.Format(
|
|
|
|
CultureInfo.InvariantCulture,
|
|
|
|
"/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2013-11-12 17:12:11 +00:00
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
WebUtility.UrlEncode(artistName));
|
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
using var oReader = new StreamReader(stream, Encoding.UTF8);
|
|
|
|
var settings = new XmlReaderSettings()
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var reader = XmlReader.Create(oReader, settings);
|
|
|
|
return ReleaseResult.Parse(reader).FirstOrDefault();
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
private static (string Name, string ArtistId) ParseArtistCredit(XmlReader reader)
|
2017-10-28 04:20:18 +00:00
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "name-credit":
|
2021-02-26 01:38:18 +00:00
|
|
|
{
|
2021-11-17 13:49:30 +00:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
return ParseArtistNameCredit(subReader);
|
|
|
|
}
|
2021-07-23 00:33:19 +00:00
|
|
|
|
2017-10-28 04:20:18 +00:00
|
|
|
default:
|
2021-06-27 00:00:17 +00:00
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
2017-10-28 04:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 11:20:39 +00:00
|
|
|
return default;
|
2017-10-28 04:20:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
private static (string Name, string ArtistId) ParseArtistNameCredit(XmlReader reader)
|
2017-10-28 04:20:18 +00:00
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "artist":
|
|
|
|
{
|
2021-11-17 13:49:30 +00:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-28 04:20:18 +00:00
|
|
|
var id = reader.GetAttribute("id");
|
2021-02-26 01:38:18 +00:00
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
return ParseArtistArtistCredit(subReader, id);
|
2017-10-28 04:20:18 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-10-28 04:20:18 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 20:52:10 +00:00
|
|
|
return (null, null);
|
2017-10-28 04:20:18 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
private static (string Name, string ArtistId) ParseArtistArtistCredit(XmlReader reader, string artistId)
|
2017-10-28 04:20:18 +00:00
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
string name = null;
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "name":
|
|
|
|
{
|
|
|
|
name = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2017-10-28 04:20:18 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
return (name, artistId);
|
2017-10-28 04:20:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 19:57:18 +00:00
|
|
|
private async Task<string> GetReleaseIdFromReleaseGroupId(string releaseGroupId, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
var url = "/ws/2/release?release-group=" + releaseGroupId.ToString(CultureInfo.InvariantCulture);
|
2017-03-15 19:57:18 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
using var oReader = new StreamReader(stream, Encoding.UTF8);
|
|
|
|
var settings = new XmlReaderSettings
|
2017-03-15 19:57:18 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2017-10-20 16:16:56 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var reader = XmlReader.Create(oReader, settings);
|
|
|
|
var result = ReleaseResult.Parse(reader).FirstOrDefault();
|
2017-03-15 19:57:18 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
return result?.ReleaseId;
|
2017-03-15 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
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>
|
2017-03-15 19:57:18 +00:00
|
|
|
private async Task<string> GetReleaseGroupFromReleaseId(string releaseEntryId, CancellationToken cancellationToken)
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
var url = "/ws/2/release-group/?query=reid:" + releaseEntryId.ToString(CultureInfo.InvariantCulture);
|
2013-11-12 17:12:11 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
using var oReader = new StreamReader(stream, Encoding.UTF8);
|
|
|
|
var settings = new XmlReaderSettings
|
2016-06-15 20:14:04 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
2022-06-14 19:24:19 +00:00
|
|
|
IgnoreComments = true,
|
|
|
|
Async = true
|
2020-08-17 19:10:02 +00:00
|
|
|
};
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
using var reader = XmlReader.Create(oReader, settings);
|
2021-11-17 13:49:30 +00:00
|
|
|
await reader.MoveToContentAsync().ConfigureAwait(false);
|
|
|
|
await reader.ReadAsync().ConfigureAwait(false);
|
2017-10-20 16:16:56 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2020-08-17 19:10:02 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
switch (reader.Name)
|
2019-02-01 16:43:31 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
case "release-group-list":
|
2016-10-27 21:05:25 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
if (reader.IsEmptyElement)
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2021-11-17 13:49:30 +00:00
|
|
|
await reader.ReadAsync().ConfigureAwait(false);
|
2021-02-26 01:38:18 +00:00
|
|
|
continue;
|
2020-08-17 19:10:02 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
return GetFirstReleaseGroupId(subReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2021-11-17 13:49:30 +00:00
|
|
|
await reader.SkipAsync().ConfigureAwait(false);
|
2021-02-26 01:38:18 +00:00
|
|
|
break;
|
2019-02-01 16:43:31 +00:00
|
|
|
}
|
2020-08-17 19:10:02 +00:00
|
|
|
}
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
else
|
|
|
|
{
|
2021-11-17 13:49:30 +00:00
|
|
|
await reader.ReadAsync().ConfigureAwait(false);
|
2021-02-26 01:38:18 +00:00
|
|
|
}
|
2016-06-15 20:14:04 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
|
|
|
|
return null;
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
private string GetFirstReleaseGroupId(XmlReader reader)
|
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
2016-11-02 17:08:20 +00:00
|
|
|
reader.Read();
|
2016-06-15 20:14:04 +00:00
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
// Loop through each element
|
2016-12-03 21:46:06 +00:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-06-15 20:14:04 +00:00
|
|
|
{
|
2016-10-27 21:05:25 +00:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-06-15 20:14:04 +00:00
|
|
|
{
|
2016-10-27 21:05:25 +00:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "release-group":
|
|
|
|
{
|
|
|
|
return reader.GetAttribute("id");
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-10-27 21:05:25 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 20:14:04 +00:00
|
|
|
}
|
2016-10-31 18:59:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-06-15 20:14:04 +00:00
|
|
|
}
|
2016-10-27 19:03:23 +00:00
|
|
|
|
2016-06-15 20:14:04 +00:00
|
|
|
return null;
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2019-03-13 20:31:21 +00:00
|
|
|
/// Makes request to MusicBrainz server and awaits a response.
|
2019-03-15 19:29:04 +00:00
|
|
|
/// A 503 Service Unavailable response indicates throttling to maintain a rate limit.
|
|
|
|
/// A number of retries shall be made in order to try and satisfy the request before
|
|
|
|
/// giving up and returning null.
|
2013-11-12 17:12:11 +00:00
|
|
|
/// </summary>
|
2021-07-23 00:33:19 +00:00
|
|
|
/// <param name="url">Address of MusicBrainz server.</param>
|
|
|
|
/// <param name="cancellationToken">CancellationToken to use for method.</param>
|
|
|
|
/// <returns>Returns response from MusicBrainz service.</returns>
|
2020-08-17 19:10:02 +00:00
|
|
|
internal async Task<HttpResponseMessage> GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
|
2013-11-12 17:12:11 +00:00
|
|
|
{
|
2020-10-03 00:14:57 +00:00
|
|
|
await _apiRequestLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
2019-03-15 19:29:04 +00:00
|
|
|
|
2020-10-03 00:14:57 +00:00
|
|
|
try
|
|
|
|
{
|
2020-10-03 00:27:43 +00:00
|
|
|
HttpResponseMessage response;
|
|
|
|
var attempts = 0u;
|
|
|
|
var requestUrl = _musicBrainzBaseUrl.TrimEnd('/') + url;
|
|
|
|
|
2020-10-03 00:14:57 +00:00
|
|
|
do
|
2019-03-15 19:29:04 +00:00
|
|
|
{
|
2020-10-03 00:14:57 +00:00
|
|
|
attempts++;
|
2019-03-15 19:29:04 +00:00
|
|
|
|
2020-10-03 00:14:57 +00:00
|
|
|
if (_stopWatchMusicBrainz.ElapsedMilliseconds < _musicBrainzQueryIntervalMs)
|
|
|
|
{
|
2020-10-16 18:15:42 +00:00
|
|
|
// MusicBrainz is extremely adamant about limiting to one request per second.
|
2020-10-03 00:14:57 +00:00
|
|
|
var delayMs = _musicBrainzQueryIntervalMs - _stopWatchMusicBrainz.ElapsedMilliseconds;
|
|
|
|
await Task.Delay((int)delayMs, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
2019-03-15 19:29:04 +00:00
|
|
|
|
2020-10-03 00:14:57 +00:00
|
|
|
// Write time since last request to debug log as evidence we're meeting rate limit
|
|
|
|
// requirement, before resetting stopwatch back to zero.
|
|
|
|
_logger.LogDebug("GetMusicBrainzResponse: Time since previous request: {0} ms", _stopWatchMusicBrainz.ElapsedMilliseconds);
|
|
|
|
_stopWatchMusicBrainz.Restart();
|
2019-03-15 19:29:04 +00:00
|
|
|
|
2020-10-03 00:27:43 +00:00
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
|
2021-02-15 13:19:08 +00:00
|
|
|
response = await _httpClientFactory
|
|
|
|
.CreateClient(NamedClient.MusicBrainz)
|
|
|
|
.SendAsync(request, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2020-10-03 00:14:57 +00:00
|
|
|
|
2020-10-16 18:15:42 +00:00
|
|
|
// We retry a finite number of times, and only whilst MB is indicating 503 (throttling).
|
2020-10-03 00:14:57 +00:00
|
|
|
}
|
|
|
|
while (attempts < MusicBrainzQueryAttempts && response.StatusCode == HttpStatusCode.ServiceUnavailable);
|
2020-10-03 00:19:35 +00:00
|
|
|
|
2020-10-16 18:15:42 +00:00
|
|
|
// Log error if unable to query MB database due to throttling.
|
2020-10-03 00:19:35 +00:00
|
|
|
if (attempts == MusicBrainzQueryAttempts && response.StatusCode == HttpStatusCode.ServiceUnavailable)
|
|
|
|
{
|
|
|
|
_logger.LogError("GetMusicBrainzResponse: 503 Service Unavailable (throttled) response received {0} times whilst requesting {1}", attempts, requestUrl);
|
|
|
|
}
|
2020-10-03 00:27:43 +00:00
|
|
|
|
|
|
|
return response;
|
2020-10-03 00:14:57 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_apiRequestLock.Release();
|
2019-03-15 19:29:04 +00:00
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
|
|
|
|
2019-09-10 20:37:53 +00:00
|
|
|
/// <inheritdoc />
|
2020-08-17 19:10:02 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2021-07-23 00:33:19 +00:00
|
|
|
|
2021-07-23 03:16:38 +00:00
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
_apiRequestLock?.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 01:52:33 +00:00
|
|
|
/// <inheritdoc />
|
2021-07-23 03:16:38 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2021-07-23 00:33:19 +00:00
|
|
|
private class ReleaseResult
|
|
|
|
{
|
|
|
|
public string ReleaseId;
|
|
|
|
public string ReleaseGroupId;
|
|
|
|
public string Title;
|
|
|
|
public string Overview;
|
|
|
|
public int? Year;
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
public List<(string, string)> Artists = new();
|
2021-07-23 00:33:19 +00:00
|
|
|
|
|
|
|
public static IEnumerable<ReleaseResult> Parse(XmlReader reader)
|
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "release-list":
|
|
|
|
{
|
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
return ParseReleaseList(subReader).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Enumerable.Empty<ReleaseResult>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static IEnumerable<ReleaseResult> ParseReleaseList(XmlReader reader)
|
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "release":
|
|
|
|
{
|
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var releaseId = reader.GetAttribute("id");
|
|
|
|
|
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
var release = ParseRelease(subReader, releaseId);
|
|
|
|
if (release != null)
|
|
|
|
{
|
|
|
|
yield return release;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ReleaseResult ParseRelease(XmlReader reader, string releaseId)
|
|
|
|
{
|
|
|
|
var result = new ReleaseResult
|
|
|
|
{
|
|
|
|
ReleaseId = releaseId
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
|
|
|
|
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "title":
|
|
|
|
{
|
|
|
|
result.Title = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "date":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString();
|
|
|
|
if (DateTime.TryParse(val, out var date))
|
|
|
|
{
|
|
|
|
result.Year = date.Year;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "annotation":
|
|
|
|
{
|
|
|
|
result.Overview = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "release-group":
|
|
|
|
{
|
|
|
|
result.ReleaseGroupId = reader.GetAttribute("id");
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case "artist-credit":
|
|
|
|
{
|
2021-11-17 13:49:30 +00:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-23 00:33:19 +00:00
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
var artist = ParseArtistCredit(subReader);
|
|
|
|
|
2021-12-24 21:18:24 +00:00
|
|
|
if (!string.IsNullOrEmpty(artist.Name))
|
2021-07-23 00:33:19 +00:00
|
|
|
{
|
|
|
|
result.Artists.Add(artist);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2013-11-12 17:12:11 +00:00
|
|
|
}
|
2018-12-28 15:48:26 +00:00
|
|
|
}
|