From 390f1653327e15248c0b0181b338c6a14d04732a Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 27 May 2013 11:02:16 -0400 Subject: [PATCH] #280 - avoid an extra request to last fm by taking data from the MusicArtist entity --- .../UserLibrary/ArtistsService.cs | 25 ------ .../HttpClientManager/HttpClientManager.cs | 90 +------------------ MediaBrowser.Common/Net/HttpRequestOptions.cs | 11 +-- .../Entities/Audio/Artist.cs | 5 -- .../Providers/Music/LastfmAlbumProvider.cs | 1 - .../Music/LastfmArtistByNameProvider.cs | 69 +++++++++++++- .../Providers/Music/LastfmArtistProvider.cs | 8 +- .../Providers/Music/LastfmHelper.cs | 16 ++-- MediaBrowser.Model/Querying/ArtistsQuery.cs | 5 -- 9 files changed, 88 insertions(+), 142 deletions(-) diff --git a/MediaBrowser.Api/UserLibrary/ArtistsService.cs b/MediaBrowser.Api/UserLibrary/ArtistsService.cs index 16fabffee..fc981d0de 100644 --- a/MediaBrowser.Api/UserLibrary/ArtistsService.cs +++ b/MediaBrowser.Api/UserLibrary/ArtistsService.cs @@ -20,11 +20,6 @@ namespace MediaBrowser.Api.UserLibrary [Api(Description = "Gets all artists from a given item, folder, or the entire library")] public class GetArtists : GetItemsByName { - /// - /// Filter by artists that are on tour, or not - /// - /// null if [is on tour] contains no value, true if [is on tour]; otherwise, false. - public bool? IsOnTour { get; set; } } /// @@ -155,26 +150,6 @@ namespace MediaBrowser.Api.UserLibrary return ToOptimizedResult(result); } - /// - /// Filters the items. - /// - /// The request. - /// The items. - /// IEnumerable{BaseItem}. - protected override IEnumerable FilterItems(GetItemsByName request, IEnumerable items) - { - items = base.FilterItems(request, items); - - var getArtists = (GetArtists) request; - - if (getArtists.IsOnTour.HasValue) - { - items = items.OfType().Where(i => i.IsOnTour == getArtists.IsOnTour.Value); - } - - return items; - } - /// /// Gets all items. /// diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 1d7b4a4f3..0734aade9 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -1,5 +1,4 @@ using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Model.Logging; @@ -36,7 +35,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager private readonly IApplicationPaths _appPaths; private readonly IJsonSerializer _jsonSerializer; - private readonly FileSystemRepository _cacheRepository; /// /// Initializes a new instance of the class. @@ -63,8 +61,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager _logger = logger; _jsonSerializer = jsonSerializer; _appPaths = appPaths; - - _cacheRepository = new FileSystemRepository(Path.Combine(_appPaths.CachePath, "downloads")); } /// @@ -119,62 +115,10 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager { ValidateParams(options.Url, options.CancellationToken); - HttpResponseInfo cachedInfo = null; - - var urlHash = options.Url.GetMD5().ToString(); - var cachedInfoPath = _cacheRepository.GetResourcePath(urlHash + ".js"); - var cachedReponsePath = _cacheRepository.GetResourcePath(urlHash + ".dat"); - - if (options.EnableResponseCache) - { - try - { - cachedInfo = _jsonSerializer.DeserializeFromFile(cachedInfoPath); - } - catch (FileNotFoundException) - { - - } - - if (cachedInfo != null) - { - var now = DateTime.UtcNow; - - var isCacheValid = cachedInfo.Expires.HasValue ? cachedInfo.Expires.Value > now : - !cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag) && (now - cachedInfo.RequestDate).TotalDays < 5; - - if (isCacheValid) - { - _logger.Debug("Cache is still valid for {0}", options.Url); - - try - { - return GetCachedResponse(cachedReponsePath); - } - catch (FileNotFoundException) - { - - } - } - } - } - options.CancellationToken.ThrowIfCancellationRequested(); using (var message = GetHttpRequestMessage(options)) { - if (options.EnableResponseCache && cachedInfo != null) - { - if (!string.IsNullOrEmpty(cachedInfo.Etag)) - { - message.Headers.Add("If-None-Match", cachedInfo.Etag); - } - else if (cachedInfo.LastModified.HasValue) - { - message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value); - } - } - if (options.ResourcePool != null) { await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false); @@ -188,38 +132,9 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager var response = await GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression).SendAsync(message, HttpCompletionOption.ResponseContentRead, options.CancellationToken).ConfigureAwait(false); - if (options.EnableResponseCache) - { - if (response.StatusCode != HttpStatusCode.NotModified) - { - EnsureSuccessStatusCode(response); - } + EnsureSuccessStatusCode(response); - options.CancellationToken.ThrowIfCancellationRequested(); - - cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response); - - if (response.StatusCode == HttpStatusCode.NotModified) - { - _logger.Debug("Server indicates not modified for {0}. Returning cached result.", options.Url); - - return GetCachedResponse(cachedReponsePath); - } - - if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || - (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow)) - { - await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false); - - return GetCachedResponse(cachedReponsePath); - } - } - else - { - EnsureSuccessStatusCode(response); - - options.CancellationToken.ThrowIfCancellationRequested(); - } + options.CancellationToken.ThrowIfCancellationRequested(); return await response.Content.ReadAsStreamAsync().ConfigureAwait(false); } @@ -247,7 +162,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager } } } - } /// diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs index 7fb621b01..5579eb1ce 100644 --- a/MediaBrowser.Common/Net/HttpRequestOptions.cs +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -19,7 +19,7 @@ namespace MediaBrowser.Common.Net /// /// The accept header. public string AcceptHeader { get; set; } - + /// /// Gets or sets the cancellation token. /// @@ -45,13 +45,14 @@ namespace MediaBrowser.Common.Net public IProgress Progress { get; set; } /// - /// Gets or sets a value indicating whether [enable response caching]. + /// Gets or sets a value indicating whether [enable HTTP compression]. /// - /// true if [enable response caching]; otherwise, false. - public bool EnableResponseCache { get; set; } - + /// true if [enable HTTP compression]; otherwise, false. public bool EnableHttpCompression { get; set; } + /// + /// Initializes a new instance of the class. + /// public HttpRequestOptions() { EnableHttpCompression = true; diff --git a/MediaBrowser.Controller/Entities/Audio/Artist.cs b/MediaBrowser.Controller/Entities/Audio/Artist.cs index 6052a277e..0fe8cb671 100644 --- a/MediaBrowser.Controller/Entities/Audio/Artist.cs +++ b/MediaBrowser.Controller/Entities/Audio/Artist.cs @@ -15,10 +15,5 @@ namespace MediaBrowser.Controller.Entities.Audio return "Artist-" + Name; } - /// - /// Gets or sets a value indicating whether this instance is on tour. - /// - /// true if this instance is on tour; otherwise, false. - public bool IsOnTour { get; set; } } } diff --git a/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs index 0216c990f..77cd2033d 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmAlbumProvider.cs @@ -113,7 +113,6 @@ namespace MediaBrowser.Controller.Providers.Music Url = url, ResourcePool = LastfmResourcePool, CancellationToken = cancellationToken, - EnableResponseCache = true, EnableHttpCompression = false }).ConfigureAwait(false)) diff --git a/MediaBrowser.Controller/Providers/Music/LastfmArtistByNameProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmArtistByNameProvider.cs index 72ee76598..e1590a577 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmArtistByNameProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmArtistByNameProvider.cs @@ -3,8 +3,13 @@ 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.Serialization; +using System; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Controller.Providers.Music { @@ -14,13 +19,14 @@ namespace MediaBrowser.Controller.Providers.Music public class LastfmArtistByNameProvider : LastfmArtistProvider { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The json serializer. /// The HTTP client. /// The log manager. /// The configuration manager. /// The provider manager. + /// The library manager. public LastfmArtistByNameProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, ILibraryManager libraryManager) : base(jsonSerializer, httpClient, logManager, configurationManager, providerManager, libraryManager) { @@ -47,5 +53,66 @@ namespace MediaBrowser.Controller.Providers.Music { return item is Artist; } + + /// + /// Gets the provider version. + /// + /// The provider version. + protected override string ProviderVersion + { + get + { + return "7"; + } + } + + /// + /// Fetches the lastfm data. + /// + /// The item. + /// The music brainz id. + /// The cancellation token. + /// Task. + protected override async Task FetchLastfmData(BaseItem item, string musicBrainzId, CancellationToken cancellationToken) + { + var artist = (Artist)item; + + // See if we can avoid an http request by finding the matching MusicArtist entity + var musicArtist = FindMusicArtist(artist, LibraryManager); + + if (musicArtist != null) + { + Logger.Info("Found MusicArtist"); + LastfmHelper.ProcessArtistData(musicArtist, artist); + } + else + { + await base.FetchLastfmData(item, musicBrainzId, cancellationToken).ConfigureAwait(false); + } + } + + + /// + /// Finds the music artist. + /// + /// The artist. + /// The library manager. + /// MusicArtist. + private static MusicArtist FindMusicArtist(Artist artist, ILibraryManager libraryManager) + { + var musicBrainzId = artist.GetProviderId(MetadataProviders.Musicbrainz); + + return libraryManager.RootFolder.RecursiveChildren + .OfType() + .FirstOrDefault(i => + { + if (!string.IsNullOrWhiteSpace(musicBrainzId) && string.Equals(musicBrainzId, i.GetProviderId(MetadataProviders.Musicbrainz), StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + return false; + }); + } } } diff --git a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs index e03ee38d7..38475317e 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmArtistProvider.cs @@ -31,7 +31,7 @@ namespace MediaBrowser.Controller.Providers.Music /// /// The _library manager /// - private readonly ILibraryManager _libraryManager; + protected readonly ILibraryManager LibraryManager; /// /// Initializes a new instance of the class. @@ -46,7 +46,7 @@ namespace MediaBrowser.Controller.Providers.Music : base(jsonSerializer, httpClient, logManager, configurationManager) { _providerManager = providerManager; - _libraryManager = libraryManager; + LibraryManager = libraryManager; LocalMetaFileName = LastfmHelper.LocalArtistMetaFileName; } @@ -104,7 +104,7 @@ namespace MediaBrowser.Controller.Providers.Music /// System.String. private string FindIdFromMusicArtistEntity(BaseItem item) { - var artist = _libraryManager.RootFolder.RecursiveChildren.OfType() + var artist = LibraryManager.RootFolder.RecursiveChildren.OfType() .FirstOrDefault(i => string.Compare(i.Name, item.Name, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0); return artist != null ? artist.GetProviderId(MetadataProviders.Musicbrainz) : null; @@ -126,7 +126,6 @@ namespace MediaBrowser.Controller.Providers.Music Url = url, ResourcePool = LastfmResourcePool, CancellationToken = cancellationToken, - EnableResponseCache = true, EnableHttpCompression = false }).ConfigureAwait(false)) @@ -243,7 +242,6 @@ namespace MediaBrowser.Controller.Providers.Music Url = url, ResourcePool = LastfmResourcePool, CancellationToken = cancellationToken, - EnableResponseCache = true, EnableHttpCompression = false }).ConfigureAwait(false)) diff --git a/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs b/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs index 163d2ed53..d8bbfb00d 100644 --- a/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs +++ b/MediaBrowser.Controller/Providers/Music/LastfmHelper.cs @@ -33,15 +33,17 @@ namespace MediaBrowser.Controller.Providers.Music { AddTags(artist, data.tags); } - - var entity = artist as Artist; - - if (entity != null) - { - entity.IsOnTour = string.Equals(data.ontour, "1"); - } } + public static void ProcessArtistData(MusicArtist source, Artist target) + { + target.PremiereDate = source.PremiereDate; + target.ProductionYear = source.ProductionYear; + target.Tags = source.Tags.ToList(); + target.Overview = source.Overview; + target.ProductionLocations = source.ProductionLocations.ToList(); + } + public static void ProcessAlbumData(BaseItem item, LastfmAlbum data) { if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid); diff --git a/MediaBrowser.Model/Querying/ArtistsQuery.cs b/MediaBrowser.Model/Querying/ArtistsQuery.cs index 4d52eaf4b..471757196 100644 --- a/MediaBrowser.Model/Querying/ArtistsQuery.cs +++ b/MediaBrowser.Model/Querying/ArtistsQuery.cs @@ -6,10 +6,5 @@ namespace MediaBrowser.Model.Querying /// public class ArtistsQuery : ItemsByNameQuery { - /// - /// Filter by artists that are on tour, or not - /// - /// null if [is on tour] contains no value, true if [is on tour]; otherwise, false. - public bool? IsOnTour { get; set; } } }