diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj
index ad5dd9b3fd..00924675a7 100644
--- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj
+++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj
@@ -94,6 +94,7 @@
+
diff --git a/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs b/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs
index 2ec1805749..f67e2b5091 100644
--- a/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs
+++ b/MediaBrowser.Providers/Music/FanArtAlbumProvider.cs
@@ -56,7 +56,7 @@ namespace MediaBrowser.Providers.Music
/// The priority.
public override MetadataProviderPriority Priority
{
- get { return MetadataProviderPriority.Fourth; }
+ get { return MetadataProviderPriority.Fifth; }
}
///
@@ -97,7 +97,7 @@ namespace MediaBrowser.Providers.Music
{
get
{
- return "17";
+ return "18";
}
}
diff --git a/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs b/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs
index d0e807a862..3c6bc19be0 100644
--- a/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs
+++ b/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs
@@ -11,10 +11,8 @@ using MoreLinq;
using System;
using System.IO;
using System.Linq;
-using System.Text;
using System.Threading;
using System.Threading.Tasks;
-using System.Xml;
namespace MediaBrowser.Providers.Music
{
@@ -22,11 +20,6 @@ namespace MediaBrowser.Providers.Music
{
internal static LastfmAlbumProvider Current;
- ///
- /// The _music brainz resource pool
- ///
- private readonly SemaphoreSlim _musicBrainzResourcePool = new SemaphoreSlim(1, 1);
-
public LastfmAlbumProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(jsonSerializer, httpClient, logManager, configurationManager)
{
@@ -39,7 +32,7 @@ namespace MediaBrowser.Providers.Music
/// The priority.
public override MetadataProviderPriority Priority
{
- get { return MetadataProviderPriority.Third; }
+ get { return MetadataProviderPriority.Fourth; }
}
protected override string ProviderVersion
@@ -99,20 +92,6 @@ namespace MediaBrowser.Providers.Music
{
LastfmHelper.ProcessAlbumData(item, result.album);
}
-
- var releaseEntryId = item.GetProviderId(MetadataProviders.Musicbrainz);
-
- if (!string.IsNullOrEmpty(releaseEntryId))
- {
- var musicBrainzReleaseGroupId = album.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
-
- if (string.IsNullOrEmpty(musicBrainzReleaseGroupId))
- {
- musicBrainzReleaseGroupId = await GetReleaseGroupId(releaseEntryId, cancellationToken).ConfigureAwait(false);
-
- album.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, musicBrainzReleaseGroupId);
- }
- }
BaseProviderInfo data;
if (!item.ProviderData.TryGetValue(Id, out data))
@@ -241,77 +220,5 @@ namespace MediaBrowser.Providers.Music
return string.Join(string.Empty, albumArtists.OrderBy(i => i).ToArray()).GetMD5();
}
-
- ///
- /// Gets the release group id internal.
- ///
- /// The release entry id.
- /// The cancellation token.
- /// Task{System.String}.
- private async Task GetReleaseGroupId(string releaseEntryId, CancellationToken cancellationToken)
- {
- var url = string.Format("http://www.musicbrainz.org/ws/2/release-group/?query=reid:{0}", releaseEntryId);
-
- var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
-
- var ns = new XmlNamespaceManager(doc.NameTable);
- ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
- var node = doc.SelectSingleNode("//mb:release-group-list/mb:release-group/@id", ns);
-
- return node != null ? node.Value : null;
- }
- ///
- /// The _last music brainz request
- ///
- private DateTime _lastRequestDate = DateTime.MinValue;
-
- ///
- /// Gets the music brainz response.
- ///
- /// The URL.
- /// The cancellation token.
- /// Task{XmlDocument}.
- internal async Task GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
- {
- await _musicBrainzResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
-
- try
- {
- var diff = 1500 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
-
- // MusicBrainz is extremely adamant about limiting to one request per second
-
- if (diff > 0)
- {
- await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
- }
-
- _lastRequestDate = DateTime.Now;
-
- var doc = new XmlDocument();
-
- using (var xml = await HttpClient.Get(new HttpRequestOptions
- {
- Url = url,
- CancellationToken = cancellationToken,
- UserAgent = Environment.MachineName
-
- }).ConfigureAwait(false))
- {
- using (var oReader = new StreamReader(xml, Encoding.UTF8))
- {
- doc.Load(oReader);
- }
- }
-
- return doc;
- }
- finally
- {
- _lastRequestDate = DateTime.Now;
-
- _musicBrainzResourcePool.Release();
- }
- }
}
}
diff --git a/MediaBrowser.Providers/Music/LastfmArtistProvider.cs b/MediaBrowser.Providers/Music/LastfmArtistProvider.cs
index 146992039a..3113d2541f 100644
--- a/MediaBrowser.Providers/Music/LastfmArtistProvider.cs
+++ b/MediaBrowser.Providers/Music/LastfmArtistProvider.cs
@@ -164,7 +164,7 @@ namespace MediaBrowser.Providers.Music
var url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artist:\"{0}\"", UrlEncode(nameToSearch));
- var doc = await LastfmAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
+ var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
@@ -180,7 +180,7 @@ namespace MediaBrowser.Providers.Music
// Try again using the search with accent characters url
url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
- doc = await LastfmAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
+ doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
diff --git a/MediaBrowser.Providers/Music/LastfmHelper.cs b/MediaBrowser.Providers/Music/LastfmHelper.cs
index d58aa714b0..9d24875b2c 100644
--- a/MediaBrowser.Providers/Music/LastfmHelper.cs
+++ b/MediaBrowser.Providers/Music/LastfmHelper.cs
@@ -96,8 +96,6 @@ namespace MediaBrowser.Providers.Music
public static void ProcessAlbumData(BaseItem item, LastfmAlbum data)
{
- if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid);
-
var overview = data.wiki != null ? data.wiki.content : null;
if (!item.LockedFields.Contains(MetadataFields.Overview))
diff --git a/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs b/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs
new file mode 100644
index 0000000000..c01b445ca2
--- /dev/null
+++ b/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs
@@ -0,0 +1,222 @@
+using MediaBrowser.Common.Net;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.Audio;
+using MediaBrowser.Controller.Providers;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Logging;
+using System;
+using System.IO;
+using System.Net;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Xml;
+
+namespace MediaBrowser.Providers.Music
+{
+ public class MusicBrainzAlbumProvider : BaseMetadataProvider
+ {
+ internal static MusicBrainzAlbumProvider Current;
+
+ private readonly IHttpClient _httpClient;
+
+ public MusicBrainzAlbumProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IHttpClient httpClient)
+ : base(logManager, configurationManager)
+ {
+ _httpClient = httpClient;
+
+ Current = this;
+ }
+
+ public override bool Supports(BaseItem item)
+ {
+ return item is MusicAlbum;
+ }
+
+ public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
+ {
+ var releaseId = item.GetProviderId(MetadataProviders.Musicbrainz);
+ var releaseGroupId = item.GetProviderId(MetadataProviders.MusicBrainzReleaseGroup);
+
+ if (string.IsNullOrEmpty(releaseId))
+ {
+ var result = await GetReleaseResult((MusicAlbum)item, cancellationToken).ConfigureAwait(false);
+
+ if (!string.IsNullOrEmpty(result.ReleaseId))
+ {
+ releaseId = result.ReleaseId;
+ item.SetProviderId(MetadataProviders.Musicbrainz, releaseId);
+ }
+
+ if (!string.IsNullOrEmpty(result.ReleaseGroupId))
+ {
+ releaseGroupId = result.ReleaseGroupId;
+ item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, releaseGroupId);
+ }
+ }
+
+ // If we have a release Id but not a release group Id...
+ if (!string.IsNullOrEmpty(releaseId) && string.IsNullOrEmpty(releaseGroupId))
+ {
+ releaseGroupId = await GetReleaseGroupId(releaseId, cancellationToken).ConfigureAwait(false);
+
+ item.SetProviderId(MetadataProviders.MusicBrainzReleaseGroup, releaseGroupId);
+ }
+
+ SetLastRefreshed(item, DateTime.UtcNow);
+ return true;
+ }
+
+ private Task GetReleaseResult(MusicAlbum album, CancellationToken cancellationToken)
+ {
+ var artist = album.Parent;
+ var artistId = artist.GetProviderId(MetadataProviders.Musicbrainz);
+
+ if (!string.IsNullOrEmpty(artistId))
+ {
+ return GetReleaseResult(album.Name, artistId, cancellationToken);
+ }
+
+ return GetReleaseResultByArtistName(album.Name, artist.Name, cancellationToken);
+ }
+
+ private async Task GetReleaseResult(string albumName, string artistId, CancellationToken cancellationToken)
+ {
+ var url = string.Format("http://www.musicbrainz.org/ws/2/release/?query=\"{0}\" and arid:{1}",
+ WebUtility.UrlEncode(albumName),
+ artistId);
+
+ var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
+
+ return GetReleaseResult(doc);
+ }
+
+ private async Task GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
+ {
+ var url = string.Format("http://www.musicbrainz.org/ws/2/release/?query=\"{0}\" and artist:\"{1}\"",
+ WebUtility.UrlEncode(albumName),
+ WebUtility.UrlEncode(artistName));
+
+ var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
+
+ return GetReleaseResult(doc);
+ }
+
+ private ReleaseResult GetReleaseResult(XmlDocument doc)
+ {
+ var ns = new XmlNamespaceManager(doc.NameTable);
+ ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
+
+ var result = new ReleaseResult
+ {
+
+ };
+
+ var releaseIdNode = doc.SelectSingleNode("//mb:release-list/mb:release/@id", ns);
+
+ if (releaseIdNode != null)
+ {
+ result.ReleaseId = releaseIdNode.Value;
+ }
+
+ var releaseGroupIdNode = doc.SelectSingleNode("//mb:release-list/mb:release/mb:release-group/@id", ns);
+
+ if (releaseGroupIdNode != null)
+ {
+ result.ReleaseGroupId = releaseGroupIdNode.Value;
+ }
+
+ return result;
+ }
+
+ private class ReleaseResult
+ {
+ public string ReleaseId;
+ public string ReleaseGroupId;
+ }
+
+ ///
+ /// Gets the release group id internal.
+ ///
+ /// The release entry id.
+ /// The cancellation token.
+ /// Task{System.String}.
+ private async Task GetReleaseGroupId(string releaseEntryId, CancellationToken cancellationToken)
+ {
+ var url = string.Format("http://www.musicbrainz.org/ws/2/release-group/?query=reid:{0}", releaseEntryId);
+
+ var doc = await GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
+
+ var ns = new XmlNamespaceManager(doc.NameTable);
+ ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
+ var node = doc.SelectSingleNode("//mb:release-group-list/mb:release-group/@id", ns);
+
+ return node != null ? node.Value : null;
+ }
+
+ ///
+ /// The _last music brainz request
+ ///
+ private DateTime _lastRequestDate = DateTime.MinValue;
+ ///
+ /// The _music brainz resource pool
+ ///
+ private readonly SemaphoreSlim _musicBrainzResourcePool = new SemaphoreSlim(1, 1);
+
+ ///
+ /// Gets the music brainz response.
+ ///
+ /// The URL.
+ /// The cancellation token.
+ /// Task{XmlDocument}.
+ internal async Task GetMusicBrainzResponse(string url, CancellationToken cancellationToken)
+ {
+ await _musicBrainzResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
+
+ try
+ {
+ var diff = 1500 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
+
+ // MusicBrainz is extremely adamant about limiting to one request per second
+
+ if (diff > 0)
+ {
+ await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
+ }
+
+ _lastRequestDate = DateTime.Now;
+
+ var doc = new XmlDocument();
+
+ using (var xml = await _httpClient.Get(new HttpRequestOptions
+ {
+ Url = url,
+ CancellationToken = cancellationToken,
+ UserAgent = Environment.MachineName
+
+ }).ConfigureAwait(false))
+ {
+ using (var oReader = new StreamReader(xml, Encoding.UTF8))
+ {
+ doc.Load(oReader);
+ }
+ }
+
+ return doc;
+ }
+ finally
+ {
+ _lastRequestDate = DateTime.Now;
+
+ _musicBrainzResourcePool.Release();
+ }
+ }
+
+
+ public override MetadataProviderPriority Priority
+ {
+ get { return MetadataProviderPriority.Third; }
+ }
+ }
+}