2019-01-13 15:03:10 -05:00
|
|
|
using System;
|
2014-02-19 23:53:15 -05:00
|
|
|
using System.Collections.Generic;
|
2016-10-31 14:59:58 -04:00
|
|
|
using System.Globalization;
|
2013-11-12 12:12:11 -05:00
|
|
|
using System.IO;
|
2014-03-13 23:23:58 -04:00
|
|
|
using System.Linq;
|
2013-11-12 12:12:11 -05:00
|
|
|
using System.Net;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml;
|
2019-01-13 14:26:31 -05:00
|
|
|
using MediaBrowser.Common;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2016-06-20 13:07:02 -04:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-01-13 14:26:31 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-11-12 12:12:11 -05:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Music
|
|
|
|
{
|
2014-02-06 22:10:13 -05:00
|
|
|
public class MusicBrainzAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
|
|
|
internal static MusicBrainzAlbumProvider Current;
|
|
|
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-01-28 20:45:48 -05:00
|
|
|
private readonly IApplicationHost _appHost;
|
2014-09-03 21:44:40 -04:00
|
|
|
private readonly ILogger _logger;
|
2016-06-20 13:07:02 -04:00
|
|
|
private readonly IJsonSerializer _json;
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2016-06-15 15:52:38 -04:00
|
|
|
public static string MusicBrainzBaseUrl = "https://www.musicbrainz.org";
|
2016-05-22 23:54:22 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
public MusicBrainzAlbumProvider(IHttpClient httpClient, IApplicationHost appHost, ILogger logger, IJsonSerializer json)
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
2014-01-28 20:45:48 -05:00
|
|
|
_appHost = appHost;
|
2014-09-03 21:44:40 -04:00
|
|
|
_logger = logger;
|
2016-06-20 13:07:02 -04:00
|
|
|
_json = json;
|
2013-11-12 12:12:11 -05:00
|
|
|
Current = this;
|
|
|
|
}
|
|
|
|
|
2014-02-19 23:53:15 -05:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2014-03-13 23:23:58 -04:00
|
|
|
var releaseId = searchInfo.GetReleaseId();
|
2017-03-15 15:57:18 -04:00
|
|
|
var releaseGroupId = searchInfo.GetReleaseGroupId();
|
2014-03-13 23:23:58 -04:00
|
|
|
|
|
|
|
string url = null;
|
2014-09-28 12:50:33 -04:00
|
|
|
var isNameSearch = false;
|
2017-03-15 15:57:18 -04:00
|
|
|
bool forceMusicBrainzProper = false;
|
2014-03-13 23:23:58 -04:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(releaseId))
|
|
|
|
{
|
2016-06-15 15:52:38 -04:00
|
|
|
url = string.Format("/ws/2/release/?query=reid:{0}", releaseId);
|
2014-03-13 23:23:58 -04:00
|
|
|
}
|
2017-03-15 15:57:18 -04:00
|
|
|
else if (!string.IsNullOrEmpty(releaseGroupId))
|
|
|
|
{
|
|
|
|
url = string.Format("/ws/2/release?release-group={0}", releaseGroupId);
|
|
|
|
forceMusicBrainzProper = true;
|
|
|
|
}
|
2014-03-13 23:23:58 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var artistMusicBrainzId = searchInfo.GetMusicBrainzArtistId();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(artistMusicBrainzId))
|
|
|
|
{
|
2016-06-15 15:52:38 -04:00
|
|
|
url = string.Format("/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2014-03-13 23:23:58 -04:00
|
|
|
WebUtility.UrlEncode(searchInfo.Name),
|
|
|
|
artistMusicBrainzId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-28 12:50:33 -04:00
|
|
|
isNameSearch = true;
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
// I'm sure there is a better way but for now it resolves search for 12" Mixes
|
|
|
|
var queryName = searchInfo.Name.Replace("\"", string.Empty);
|
|
|
|
|
2016-06-15 15:52:38 -04:00
|
|
|
url = string.Format("/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2018-09-12 13:26:21 -04:00
|
|
|
WebUtility.UrlEncode(queryName),
|
2014-03-13 23:23:58 -04:00
|
|
|
WebUtility.UrlEncode(searchInfo.GetAlbumArtist()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(url))
|
|
|
|
{
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var response = await GetMusicBrainzResponse(url, isNameSearch, forceMusicBrainzProper, cancellationToken).ConfigureAwait(false))
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var stream = response.Content)
|
|
|
|
{
|
|
|
|
return GetResultsFromResponse(stream);
|
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2014-03-13 23:23:58 -04:00
|
|
|
}
|
|
|
|
|
2014-02-19 23:53:15 -05:00
|
|
|
return new List<RemoteSearchResult>();
|
|
|
|
}
|
|
|
|
|
2016-10-27 17:05:25 -04:00
|
|
|
private List<RemoteSearchResult> GetResultsFromResponse(Stream stream)
|
2014-03-13 23:23:58 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
2014-03-13 23:23:58 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
var settings = new XmlReaderSettings()
|
|
|
|
{
|
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 17:05:25 -04:00
|
|
|
|
|
|
|
using (var reader = XmlReader.Create(oReader, settings))
|
2016-08-16 14:45:57 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
var results = ReleaseResult.Parse(reader);
|
2014-03-13 23:23:58 -04:00
|
|
|
|
2016-10-27 17:05:25 -04:00
|
|
|
return results.Select(i =>
|
|
|
|
{
|
|
|
|
var result = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
Name = i.Title,
|
|
|
|
ProductionYear = i.Year
|
|
|
|
};
|
|
|
|
|
2017-10-28 00:20:18 -04:00
|
|
|
if (i.Artists.Count > 0)
|
|
|
|
{
|
|
|
|
result.AlbumArtist = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
SearchProviderName = Name,
|
|
|
|
Name = i.Artists[0].Item1
|
|
|
|
};
|
|
|
|
|
|
|
|
result.AlbumArtist.SetProviderId(MetadataProviders.MusicBrainzArtist, i.Artists[0].Item2);
|
|
|
|
}
|
|
|
|
|
2016-10-27 17:05:25 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(i.ReleaseId))
|
|
|
|
{
|
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseId);
|
|
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
|
|
|
|
{
|
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, i.ReleaseGroupId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2017-08-09 15:56:38 -04:00
|
|
|
|
2017-08-09 17:08:01 -04:00
|
|
|
}).ToList();
|
2016-10-27 17:05:25 -04:00
|
|
|
}
|
|
|
|
}
|
2014-03-13 23:23:58 -04:00
|
|
|
}
|
|
|
|
|
2014-02-06 22:10:13 -05:00
|
|
|
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken)
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2014-02-07 17:40:03 -05:00
|
|
|
var releaseId = id.GetReleaseId();
|
|
|
|
var releaseGroupId = id.GetReleaseGroupId();
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2014-02-07 17:40:03 -05:00
|
|
|
var result = new MetadataResult<MusicAlbum>
|
|
|
|
{
|
|
|
|
Item = new MusicAlbum()
|
|
|
|
};
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2017-03-15 15:57:18 -04: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 12:12:11 -05:00
|
|
|
{
|
2014-02-09 02:27:44 -05:00
|
|
|
var artistMusicBrainzId = id.GetMusicBrainzArtistId();
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2014-02-07 17:40:03 -05:00
|
|
|
var releaseResult = await GetReleaseResult(artistMusicBrainzId, id.GetAlbumArtist(), id.Name, cancellationToken).ConfigureAwait(false);
|
2014-01-31 14:55:21 -05:00
|
|
|
|
2016-10-08 14:51:07 -04:00
|
|
|
if (releaseResult != null)
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2017-03-15 15:57:18 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseResult.ReleaseId))
|
2016-10-08 14:51:07 -04:00
|
|
|
{
|
|
|
|
releaseId = releaseResult.ReleaseId;
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2017-03-15 15:57:18 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseResult.ReleaseGroupId))
|
2016-10-08 14:51:07 -04:00
|
|
|
{
|
|
|
|
releaseGroupId = releaseResult.ReleaseGroupId;
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Item.ProductionYear = releaseResult.Year;
|
|
|
|
result.Item.Overview = releaseResult.Overview;
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a release Id but not a release group Id...
|
2017-03-15 15:57:18 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseId) && string.IsNullOrWhiteSpace(releaseGroupId))
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2017-03-15 15:57:18 -04:00
|
|
|
releaseGroupId = await GetReleaseGroupFromReleaseId(releaseId, cancellationToken).ConfigureAwait(false);
|
2014-01-31 14:55:21 -05:00
|
|
|
result.HasMetadata = true;
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2017-03-15 15:57:18 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(releaseId) || !string.IsNullOrWhiteSpace(releaseGroupId))
|
2015-08-07 10:21:29 -04:00
|
|
|
{
|
|
|
|
result.HasMetadata = true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 17:40:03 -05: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 14:55:21 -05:00
|
|
|
return result;
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2019-01-13 15:31:14 -05:00
|
|
|
public string Name => "MusicBrainz";
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2014-01-31 14:55:21 -05:00
|
|
|
private Task<ReleaseResult> GetReleaseResult(string artistMusicBrainId, string artistName, string albumName, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(artistMusicBrainId))
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2014-01-31 14:55:21 -05:00
|
|
|
return GetReleaseResult(albumName, artistMusicBrainId, cancellationToken);
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2014-06-23 12:05:19 -04:00
|
|
|
if (string.IsNullOrWhiteSpace(artistName))
|
|
|
|
{
|
|
|
|
return Task.FromResult(new ReleaseResult());
|
|
|
|
}
|
|
|
|
|
2014-01-31 14:55:21 -05:00
|
|
|
return GetReleaseResultByArtistName(albumName, artistName, cancellationToken);
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<ReleaseResult> GetReleaseResult(string albumName, string artistId, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-06-15 15:52:38 -04:00
|
|
|
var url = string.Format("/ws/2/release/?query=\"{0}\" AND arid:{1}",
|
2013-11-12 12:12:11 -05:00
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
artistId);
|
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var response = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var stream = response.Content)
|
|
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
var settings = new XmlReaderSettings()
|
2016-10-27 17:05:25 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 17:05:25 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var reader = XmlReader.Create(oReader, settings))
|
|
|
|
{
|
|
|
|
return ReleaseResult.Parse(reader).FirstOrDefault();
|
2016-10-27 17:05:25 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
|
|
|
|
{
|
2016-06-15 15:52:38 -04:00
|
|
|
var url = string.Format("/ws/2/release/?query=\"{0}\" AND artist:\"{1}\"",
|
2013-11-12 12:12:11 -05:00
|
|
|
WebUtility.UrlEncode(albumName),
|
|
|
|
WebUtility.UrlEncode(artistName));
|
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var response = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var stream = response.Content)
|
|
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
var settings = new XmlReaderSettings()
|
2016-10-27 17:05:25 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 17:05:25 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var reader = XmlReader.Create(oReader, settings))
|
|
|
|
{
|
|
|
|
return ReleaseResult.Parse(reader).FirstOrDefault();
|
2016-10-27 17:05:25 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2016-06-15 15:52:38 -04:00
|
|
|
private class ReleaseResult
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2016-06-15 15:52:38 -04:00
|
|
|
public string ReleaseId;
|
|
|
|
public string ReleaseGroupId;
|
2016-08-16 14:45:57 -04:00
|
|
|
public string Title;
|
2016-10-08 14:51:07 -04:00
|
|
|
public string Overview;
|
|
|
|
public int? Year;
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public List<ValueTuple<string, string>> Artists = new List<ValueTuple<string, string>>();
|
2017-10-28 00:20:18 -04:00
|
|
|
|
2016-10-27 17:05:25 -04:00
|
|
|
public static List<ReleaseResult> Parse(XmlReader reader)
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2016-10-27 15:03:23 -04:00
|
|
|
reader.MoveToContent();
|
2016-11-02 13:08:20 -04:00
|
|
|
reader.Read();
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
// Loop through each element
|
2016-12-03 16:46:06 -05:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-06-15 15:52:38 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-06-15 15:52:38 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "release-list":
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2016-12-03 18:57:34 -05:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-27 17:05:25 -04:00
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
2016-11-03 22:33:47 -04:00
|
|
|
return ParseReleaseList(subReader);
|
2016-10-27 17:05:25 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2016-10-27 17:05:25 -04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 15:52:38 -04:00
|
|
|
}
|
2016-10-31 14:59:58 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-06-15 15:52:38 -04:00
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2016-11-03 22:33:47 -04:00
|
|
|
return new List<ReleaseResult>();
|
2016-08-16 14:45:57 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
private static List<ReleaseResult> ParseReleaseList(XmlReader reader)
|
2016-10-08 14:51:07 -04:00
|
|
|
{
|
2016-10-27 15:03:23 -04:00
|
|
|
var list = new List<ReleaseResult>();
|
|
|
|
|
|
|
|
reader.MoveToContent();
|
2016-11-02 13:08:20 -04:00
|
|
|
reader.Read();
|
2016-10-27 15:03:23 -04:00
|
|
|
|
|
|
|
// Loop through each element
|
2016-12-03 16:46:06 -05:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-10-08 14:51:07 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-10-08 14:51:07 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "release":
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2016-12-03 18:57:34 -05:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-27 18:55:56 -04:00
|
|
|
var releaseId = reader.GetAttribute("id");
|
2016-10-27 17:05:25 -04:00
|
|
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
|
|
|
var release = ParseRelease(subReader, releaseId);
|
|
|
|
if (release != null)
|
|
|
|
{
|
|
|
|
list.Add(release);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2016-10-27 17:05:25 -04:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 14:51:07 -04:00
|
|
|
}
|
2016-10-31 14:59:58 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-10-08 14:51:07 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
return list;
|
2016-10-08 14:51:07 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
private static ReleaseResult ParseRelease(XmlReader reader, string releaseId)
|
2016-08-16 14:45:57 -04:00
|
|
|
{
|
2016-10-27 15:03:23 -04:00
|
|
|
var result = new ReleaseResult
|
2016-06-15 15:52:38 -04:00
|
|
|
{
|
2016-10-27 15:03:23 -04:00
|
|
|
ReleaseId = releaseId
|
|
|
|
};
|
2016-08-16 14:45:57 -04:00
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
reader.MoveToContent();
|
2016-10-27 17:05:25 -04:00
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
// Loop through each element
|
2016-12-03 16:46:06 -05:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-06-15 15:52:38 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-06-15 15:52:38 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "title":
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
result.Title = reader.ReadElementContentAsString();
|
|
|
|
break;
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2016-10-27 17:05:25 -04:00
|
|
|
case "date":
|
|
|
|
{
|
|
|
|
var val = reader.ReadElementContentAsString();
|
2019-01-13 15:46:33 -05:00
|
|
|
if (DateTime.TryParse(val, out var date))
|
2016-10-27 17:05:25 -04:00
|
|
|
{
|
|
|
|
result.Year = date.Year;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "annotation":
|
|
|
|
{
|
|
|
|
result.Overview = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "release-group":
|
|
|
|
{
|
|
|
|
result.ReleaseGroupId = reader.GetAttribute("id");
|
2016-11-03 22:33:47 -04:00
|
|
|
reader.Skip();
|
2017-10-28 00:20:18 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "artist-credit":
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
/*
|
|
|
|
* <artist-credit>
|
|
|
|
<name-credit>
|
|
|
|
<artist id="e225cda5-882d-4b80-b8a3-b36d7175b1ea">
|
|
|
|
<name>SARCASTIC+ZOOKEEPER</name>
|
|
|
|
<sort-name>SARCASTIC+ZOOKEEPER</sort-name>
|
|
|
|
</artist>
|
|
|
|
</name-credit>
|
|
|
|
</artist-credit>
|
|
|
|
*/
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
|
|
|
var artist = ParseArtistCredit(subReader);
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
if (!string.IsNullOrEmpty(artist.Item1))
|
2017-10-28 00:20:18 -04:00
|
|
|
{
|
|
|
|
result.Artists.Add(artist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-27 17:05:25 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2016-10-27 18:55:56 -04:00
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
2016-10-27 17:05:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
2016-06-15 15:52:38 -04:00
|
|
|
}
|
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
return result;
|
2016-06-15 15:52:38 -04:00
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private static ValueTuple<string, string> ParseArtistCredit(XmlReader reader)
|
2017-10-28 00:20:18 -04: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":
|
|
|
|
{
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
|
|
|
return ParseArtistNameCredit(subReader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
return new ValueTuple<string, string>();
|
2017-10-28 00:20:18 -04:00
|
|
|
}
|
|
|
|
|
2019-01-25 15:52:10 -05:00
|
|
|
private static (string, string) ParseArtistNameCredit(XmlReader reader)
|
2017-10-28 00:20:18 -04: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":
|
|
|
|
{
|
|
|
|
var id = reader.GetAttribute("id");
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
|
|
|
return ParseArtistArtistCredit(subReader, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 15:52:10 -05:00
|
|
|
return (null, null);
|
2017-10-28 00:20:18 -04:00
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private static ValueTuple<string, string> ParseArtistArtistCredit(XmlReader reader, string artistId)
|
2017-10-28 00:20:18 -04: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;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
return new ValueTuple<string, string>(name, artistId);
|
2017-10-28 00:20:18 -04:00
|
|
|
}
|
|
|
|
|
2017-03-15 15:57:18 -04:00
|
|
|
private async Task<string> GetReleaseIdFromReleaseGroupId(string releaseGroupId, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var url = string.Format("/ws/2/release?release-group={0}", releaseGroupId);
|
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var response = await GetMusicBrainzResponse(url, true, true, cancellationToken).ConfigureAwait(false))
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var stream = response.Content)
|
|
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
2017-03-15 15:57:18 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
var settings = new XmlReaderSettings()
|
2017-03-15 15:57:18 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2017-03-15 15:57:18 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var reader = XmlReader.Create(oReader, settings))
|
|
|
|
{
|
|
|
|
var result = ReleaseResult.Parse(reader).FirstOrDefault();
|
2017-10-20 12:16:56 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
if (result != null)
|
|
|
|
{
|
|
|
|
return result.ReleaseId;
|
2017-03-15 15:57:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-11-12 12:12:11 -05: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 15:57:18 -04:00
|
|
|
private async Task<string> GetReleaseGroupFromReleaseId(string releaseEntryId, CancellationToken cancellationToken)
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2016-06-15 15:52:38 -04:00
|
|
|
var url = string.Format("/ws/2/release-group/?query=reid:{0}", releaseEntryId);
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
using (var response = await GetMusicBrainzResponse(url, false, cancellationToken).ConfigureAwait(false))
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var stream = response.Content)
|
|
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
2016-06-15 16:14:04 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
var settings = new XmlReaderSettings()
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 17:05:25 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
using (var reader = XmlReader.Create(oReader, settings))
|
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
2017-10-20 12:16:56 -04:00
|
|
|
|
2019-02-01 11:43:31 -05:00
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-10-27 17:05:25 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
switch (reader.Name)
|
2016-10-27 15:03:23 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
case "release-group-list":
|
2017-10-20 12:16:56 -04:00
|
|
|
{
|
2019-02-01 11:43:31 -05:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
|
|
|
return GetFirstReleaseGroupId(subReader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
2017-10-20 12:16:56 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2019-02-01 11:43:31 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
2016-10-27 17:05:25 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2019-02-01 11:43:31 -05:00
|
|
|
return null;
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2016-06-15 16:14:04 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
private string GetFirstReleaseGroupId(XmlReader reader)
|
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
2016-11-02 13:08:20 -04:00
|
|
|
reader.Read();
|
2016-06-15 16:14:04 -04:00
|
|
|
|
2016-10-27 15:03:23 -04:00
|
|
|
// Loop through each element
|
2016-12-03 16:46:06 -05:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-06-15 16:14:04 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-06-15 16:14:04 -04:00
|
|
|
{
|
2016-10-27 17:05:25 -04:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "release-group":
|
|
|
|
{
|
|
|
|
return reader.GetAttribute("id");
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-06-15 16:14:04 -04:00
|
|
|
}
|
2016-10-31 14:59:58 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-06-15 16:14:04 -04:00
|
|
|
}
|
2016-10-27 15:03:23 -04:00
|
|
|
|
2016-06-15 16:14:04 -04:00
|
|
|
return null;
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
internal Task<HttpResponseInfo> GetMusicBrainzResponse(string url, bool isSearch, CancellationToken cancellationToken)
|
2017-03-15 15:57:18 -04:00
|
|
|
{
|
|
|
|
return GetMusicBrainzResponse(url, isSearch, false, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2013-11-12 12:12:11 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the music brainz response.
|
|
|
|
/// </summary>
|
2017-10-20 12:16:56 -04:00
|
|
|
internal async Task<HttpResponseInfo> GetMusicBrainzResponse(string url, bool isSearch, bool forceMusicBrainzProper, CancellationToken cancellationToken)
|
2013-11-12 12:12:11 -05:00
|
|
|
{
|
2019-01-03 13:51:18 -05:00
|
|
|
var urlInfo = new MbzUrl(MusicBrainzBaseUrl, 1000);
|
2016-10-31 14:59:58 -04:00
|
|
|
var throttleMs = urlInfo.throttleMs;
|
2016-06-15 15:52:38 -04:00
|
|
|
|
2016-10-31 14:59:58 -04:00
|
|
|
if (throttleMs > 0)
|
2016-06-15 15:52:38 -04:00
|
|
|
{
|
|
|
|
// MusicBrainz is extremely adamant about limiting to one request per second
|
2018-12-13 08:18:25 -05:00
|
|
|
_logger.LogDebug("Throttling MusicBrainz by {0}ms", throttleMs.ToString(CultureInfo.InvariantCulture));
|
2016-10-31 14:59:58 -04:00
|
|
|
await Task.Delay(throttleMs, cancellationToken).ConfigureAwait(false);
|
2016-06-15 15:52:38 -04:00
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2016-06-15 15:52:38 -04:00
|
|
|
url = urlInfo.url.TrimEnd('/') + url;
|
2013-11-12 12:12:11 -05:00
|
|
|
|
2014-10-11 16:38:13 -04:00
|
|
|
var options = new HttpRequestOptions
|
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-01-19 19:12:44 -05:00
|
|
|
UserAgent = _appHost.ApplicationUserAgent,
|
2016-10-31 14:59:58 -04:00
|
|
|
BufferContent = throttleMs > 0
|
2014-10-11 16:38:13 -04:00
|
|
|
};
|
2014-09-28 12:50:33 -04:00
|
|
|
|
2017-10-20 12:16:56 -04:00
|
|
|
return await _httpClient.SendAsync(options, "GET").ConfigureAwait(false);
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
|
|
|
|
2019-01-13 15:31:14 -05:00
|
|
|
public int Order => 0;
|
2014-02-19 23:53:15 -05:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2016-06-15 15:52:38 -04:00
|
|
|
|
|
|
|
internal class MbzUrl
|
|
|
|
{
|
2019-01-03 13:51:18 -05:00
|
|
|
internal MbzUrl(string url, int throttleMs)
|
|
|
|
{
|
|
|
|
this.url = url;
|
|
|
|
this.throttleMs = throttleMs;
|
|
|
|
}
|
|
|
|
|
2016-06-15 15:52:38 -04:00
|
|
|
public string url { get; set; }
|
|
|
|
public int throttleMs { get; set; }
|
|
|
|
}
|
2013-11-12 12:12:11 -05:00
|
|
|
}
|
2018-12-28 10:48:26 -05:00
|
|
|
}
|