123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795 |
- #pragma warning disable CS1591
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Xml;
- using MediaBrowser.Common;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Controller.Entities.Audio;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Providers;
- using MediaBrowser.Providers.Plugins.MusicBrainz;
- using Microsoft.Extensions.Logging;
- namespace MediaBrowser.Providers.Music
- {
- public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
- {
- /// <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;
- /// <summary>
- /// 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
- /// </summary>
- private readonly long _musicBrainzQueryIntervalMs;
- private readonly IHttpClientFactory _httpClientFactory;
- private readonly IApplicationHost _appHost;
- private readonly ILogger<MusicBrainzAlbumProvider> _logger;
- private readonly string _musicBrainzBaseUrl;
- private Stopwatch _stopWatchMusicBrainz = new Stopwatch();
- public MusicBrainzAlbumProvider(
- IHttpClientFactory httpClientFactory,
- IApplicationHost appHost,
- ILogger<MusicBrainzAlbumProvider> logger)
- {
- _httpClientFactory = httpClientFactory;
- _appHost = appHost;
- _logger = logger;
- _musicBrainzBaseUrl = Plugin.Instance.Configuration.Server;
- _musicBrainzQueryIntervalMs = Plugin.Instance.Configuration.RateLimit;
- // Use a stopwatch to ensure we don't exceed the MusicBrainz rate limit
- _stopWatchMusicBrainz.Start();
- Current = this;
- }
- internal static MusicBrainzAlbumProvider Current { get; private set; }
- /// <inheritdoc />
- public string Name => "MusicBrainz";
- /// <inheritdoc />
- public int Order => 0;
- /// <inheritdoc />
- public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
- {
- // TODO maybe remove when artist metadata can be disabled
- if (!Plugin.Instance.Configuration.Enable)
- {
- return Enumerable.Empty<RemoteSearchResult>();
- }
- var releaseId = searchInfo.GetReleaseId();
- var releaseGroupId = searchInfo.GetReleaseGroupId();
- string url;
- if (!string.IsNullOrEmpty(releaseId))
- {
- url = "/ws/2/release/?query=reid:" + releaseId.ToString(CultureInfo.InvariantCulture);
- }
- else if (!string.IsNullOrEmpty(releaseGroupId))
- {
- url = "/ws/2/release?release-group=" + releaseGroupId.ToString(CultureInfo.InvariantCulture);
- }
- else
- {
- var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId();
- if (!string.IsNullOrWhiteSpace(artistMusicBrainzId))
- {
- url = string.Format(
- CultureInfo.InvariantCulture,
- "/ws/2/release/?query=\"{0}\" AND arid:{1}",
- WebUtility.UrlEncode(searchInfo.Name),
- artistMusicBrainzId);
- }
- else
- {
- // I'm sure there is a better way but for now it resolves search for 12" Mixes
- var queryName = searchInfo.Name.Replace("\"", string.Empty, StringComparison.Ordinal);
- url = string.Format(
- CultureInfo.InvariantCulture,
- "/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
- WebUtility.UrlEncode(queryName),
- WebUtility.UrlEncode(searchInfo.GetAlbumArtist()));
- }
- }
- if (!string.IsNullOrWhiteSpace(url))
- {
- using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
- await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
- return GetResultsFromResponse(stream);
- }
- return Enumerable.Empty<RemoteSearchResult>();
- }
- private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
- {
- 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 =>
- {
- var result = new RemoteSearchResult
- {
- Name = i.Title,
- ProductionYear = i.Year
- };
- if (i.Artists.Count > 0)
- {
- result.AlbumArtist = new RemoteSearchResult
- {
- SearchProviderName = Name,
- Name = i.Artists[0].Item1
- };
- result.AlbumArtist.SetProviderId(MetadataProvider.MusicBrainzArtist, i.Artists[0].Item2);
- }
- if (!string.IsNullOrWhiteSpace(i.ReleaseId))
- {
- result.SetProviderId(MetadataProvider.MusicBrainzAlbum, i.ReleaseId);
- }
- if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
- {
- result.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, i.ReleaseGroupId);
- }
- return result;
- });
- }
- }
- }
- /// <inheritdoc />
- public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken)
- {
- var releaseId = id.GetReleaseId();
- var releaseGroupId = id.GetReleaseGroupId();
- var result = new MetadataResult<MusicAlbum>
- {
- Item = new MusicAlbum()
- };
- // TODO maybe remove when artist metadata can be disabled
- if (!Plugin.Instance.Configuration.Enable)
- {
- return result;
- }
- // 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))
- {
- var artistMusicBrainzId = id.GetMusicBrainzArtistId();
- var releaseResult = await GetReleaseResult(artistMusicBrainzId, id.GetAlbumArtist(), id.Name, cancellationToken).ConfigureAwait(false);
- if (releaseResult != null)
- {
- if (!string.IsNullOrWhiteSpace(releaseResult.ReleaseId))
- {
- releaseId = releaseResult.ReleaseId;
- result.HasMetadata = true;
- }
- if (!string.IsNullOrWhiteSpace(releaseResult.ReleaseGroupId))
- {
- releaseGroupId = releaseResult.ReleaseGroupId;
- result.HasMetadata = true;
- }
- result.Item.ProductionYear = releaseResult.Year;
- result.Item.Overview = releaseResult.Overview;
- }
- }
- // If we have a release Id but not a release group Id...
- if (!string.IsNullOrWhiteSpace(releaseId) && string.IsNullOrWhiteSpace(releaseGroupId))
- {
- releaseGroupId = await GetReleaseGroupFromReleaseId(releaseId, cancellationToken).ConfigureAwait(false);
- result.HasMetadata = true;
- }
- if (!string.IsNullOrWhiteSpace(releaseId) || !string.IsNullOrWhiteSpace(releaseGroupId))
- {
- result.HasMetadata = true;
- }
- if (result.HasMetadata)
- {
- if (!string.IsNullOrEmpty(releaseId))
- {
- result.Item.SetProviderId(MetadataProvider.MusicBrainzAlbum, releaseId);
- }
- if (!string.IsNullOrEmpty(releaseGroupId))
- {
- result.Item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, releaseGroupId);
- }
- }
- return result;
- }
- private Task<ReleaseResult> GetReleaseResult(string artistMusicBrainId, string artistName, string albumName, CancellationToken cancellationToken)
- {
- if (!string.IsNullOrEmpty(artistMusicBrainId))
- {
- return GetReleaseResult(albumName, artistMusicBrainId, cancellationToken);
- }
- if (string.IsNullOrWhiteSpace(artistName))
- {
- return Task.FromResult(new ReleaseResult());
- }
- return GetReleaseResultByArtistName(albumName, artistName, cancellationToken);
- }
- private async Task<ReleaseResult> GetReleaseResult(string albumName, string artistId, CancellationToken cancellationToken)
- {
- var url = string.Format(
- CultureInfo.InvariantCulture,
- "/ws/2/release/?query=\"{0}\" AND arid:{1}",
- WebUtility.UrlEncode(albumName),
- artistId);
- using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
- await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
- 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);
- return ReleaseResult.Parse(reader).FirstOrDefault();
- }
- private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
- {
- var url = string.Format(
- CultureInfo.InvariantCulture,
- "/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
- WebUtility.UrlEncode(albumName),
- WebUtility.UrlEncode(artistName));
- using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
- await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
- 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);
- return ReleaseResult.Parse(reader).FirstOrDefault();
- }
- private class ReleaseResult
- {
- public string ReleaseId;
- public string ReleaseGroupId;
- public string Title;
- public string Overview;
- public int? Year;
- public List<ValueTuple<string, string>> Artists = new List<ValueTuple<string, string>>();
- 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":
- {
- using (var subReader = reader.ReadSubtree())
- {
- var artist = ParseArtistCredit(subReader);
- if (!string.IsNullOrEmpty(artist.Item1))
- {
- result.Artists.Add(artist);
- }
- }
- break;
- }
- default:
- {
- reader.Skip();
- break;
- }
- }
- }
- else
- {
- reader.Read();
- }
- }
- return result;
- }
- }
- private static (string, string) ParseArtistCredit(XmlReader reader)
- {
- 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":
- {
- using (var subReader = reader.ReadSubtree())
- {
- return ParseArtistNameCredit(subReader);
- }
- }
- default:
- {
- reader.Skip();
- break;
- }
- }
- }
- else
- {
- reader.Read();
- }
- }
- return default;
- }
- private static (string, string) ParseArtistNameCredit(XmlReader reader)
- {
- 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":
- {
- var id = reader.GetAttribute("id");
- using (var subReader = reader.ReadSubtree())
- {
- return ParseArtistArtistCredit(subReader, id);
- }
- }
- default:
- {
- reader.Skip();
- break;
- }
- }
- }
- else
- {
- reader.Read();
- }
- }
- return (null, null);
- }
- private static (string name, string id) ParseArtistArtistCredit(XmlReader reader, string artistId)
- {
- 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;
- }
- default:
- {
- reader.Skip();
- break;
- }
- }
- }
- else
- {
- reader.Read();
- }
- }
- return (name, artistId);
- }
- private async Task<string> GetReleaseIdFromReleaseGroupId(string releaseGroupId, CancellationToken cancellationToken)
- {
- var url = "/ws/2/release?release-group=" + releaseGroupId.ToString(CultureInfo.InvariantCulture);
- using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
- await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
- 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 result = ReleaseResult.Parse(reader).FirstOrDefault();
- return result?.ReleaseId;
- }
- /// <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> GetReleaseGroupFromReleaseId(string releaseEntryId, CancellationToken cancellationToken)
- {
- var url = "/ws/2/release-group/?query=reid:" + releaseEntryId.ToString(CultureInfo.InvariantCulture);
- using var response = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
- await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
- 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))
- {
- 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-group-list":
- {
- if (reader.IsEmptyElement)
- {
- reader.Read();
- continue;
- }
- using (var subReader = reader.ReadSubtree())
- {
- return GetFirstReleaseGroupId(subReader);
- }
- }
- default:
- {
- reader.Skip();
- break;
- }
- }
- }
- else
- {
- reader.Read();
- }
- }
- return null;
- }
- }
- private string GetFirstReleaseGroupId(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-group":
- {
- return reader.GetAttribute("id");
- }
- default:
- {
- reader.Skip();
- break;
- }
- }
- }
- else
- {
- reader.Read();
- }
- }
- return null;
- }
- /// <summary>
- /// Makes request to MusicBrainz server and awaits a response.
- /// 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.
- /// </summary>
- internal async Task<HttpResponseMessage> GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
- {
- using var options = new HttpRequestMessage(HttpMethod.Get, _musicBrainzBaseUrl.TrimEnd('/') + url);
- // 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
- options.Headers.UserAgent.ParseAdd(string.Format(
- CultureInfo.InvariantCulture,
- "{0} ( {1} )",
- _appHost.ApplicationUserAgent,
- _appHost.ApplicationUserAgentAddress));
- HttpResponseMessage response;
- var attempts = 0u;
- do
- {
- attempts++;
- if (_stopWatchMusicBrainz.ElapsedMilliseconds < _musicBrainzQueryIntervalMs)
- {
- // MusicBrainz is extremely adamant about limiting to one request per second
- var delayMs = _musicBrainzQueryIntervalMs - _stopWatchMusicBrainz.ElapsedMilliseconds;
- await Task.Delay((int)delayMs, cancellationToken).ConfigureAwait(false);
- }
- // 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();
- response = await _httpClientFactory.CreateClient(NamedClient.Default).SendAsync(options).ConfigureAwait(false);
- // We retry a finite number of times, and only whilst MB is indicating 503 (throttling)
- }
- while (attempts < MusicBrainzQueryAttempts && response.StatusCode == HttpStatusCode.ServiceUnavailable);
- // Log error if unable to query MB database due to throttling
- if (attempts == MusicBrainzQueryAttempts && response.StatusCode == HttpStatusCode.ServiceUnavailable)
- {
- _logger.LogError("GetMusicBrainzResponse: 503 Service Unavailable (throttled) response received {0} times whilst requesting {1}", attempts, options.RequestUri);
- }
- return response;
- }
- /// <inheritdoc />
- public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
- {
- throw new NotImplementedException();
- }
- }
- }
|