From 46c0c3a87d2ef9b37342944fc5fe85021a5711a5 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 6 May 2013 14:34:42 -0400 Subject: [PATCH] further reduce music brainz requests --- .../Providers/Music/LastfmArtistProvider.cs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs index 578c99b1d..9117da820 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs @@ -6,6 +6,8 @@ using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Controller.Library; +using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; @@ -14,13 +16,15 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Xml; +using Mediabrowser.Model.Entities; namespace MediaBrowser.Controller.Providers.Music { public class LastfmArtistProvider : LastfmBaseProvider { private readonly IProviderManager _providerManager; - + private readonly ILibraryManager _libraryManager; + public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager) : base(jsonSerializer, httpClient, logManager, configurationManager) { @@ -28,8 +32,25 @@ namespace MediaBrowser.Controller.Providers.Music LocalMetaFileName = LastfmHelper.LocalArtistMetaFileName; } + /// + /// Finds the id. + /// + /// The item. + /// The cancellation token. + /// Task{System.String}. protected override async Task FindId(BaseItem item, CancellationToken cancellationToken) { + if (item is Artist) + { + // Since MusicArtists are refreshed first, try to find it from one of them + var id = FindIdFromMusicArtistEntity(item); + + if (!string.IsNullOrEmpty(id)) + { + return id; + } + } + // Try to find the id using last fm var result = await FindIdFromLastFm(item, cancellationToken).ConfigureAwait(false); @@ -61,6 +82,14 @@ namespace MediaBrowser.Controller.Providers.Music } } + private string FindIdFromMusicArtistEntity(BaseItem item) + { + var artist = _libraryManager.RootFolder.RecursiveChildren.OfType() + .FirstOrDefault(i => string.Equals(i.Name, item.Name, StringComparison.OrdinalIgnoreCase)); + + return artist != null ? artist.GetProviderId(MetadataProviders.Musicbrainz) : null; + } + private async Task> FindIdFromLastFm(BaseItem item, CancellationToken cancellationToken) { //Execute the Artist search against our name and assume first one is the one we want