#280 - avoid an extra request to last fm by taking data from the MusicArtist entity

This commit is contained in:
Luke Pulverenti 2013-05-27 11:02:16 -04:00
parent 2d0c51071a
commit 390f165332
9 changed files with 88 additions and 142 deletions

View File

@ -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
{
/// <summary>
/// Filter by artists that are on tour, or not
/// </summary>
/// <value><c>null</c> if [is on tour] contains no value, <c>true</c> if [is on tour]; otherwise, <c>false</c>.</value>
public bool? IsOnTour { get; set; }
}
/// <summary>
@ -155,26 +150,6 @@ namespace MediaBrowser.Api.UserLibrary
return ToOptimizedResult(result);
}
/// <summary>
/// Filters the items.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="items">The items.</param>
/// <returns>IEnumerable{BaseItem}.</returns>
protected override IEnumerable<BaseItem> FilterItems(GetItemsByName request, IEnumerable<BaseItem> items)
{
items = base.FilterItems(request, items);
var getArtists = (GetArtists) request;
if (getArtists.IsOnTour.HasValue)
{
items = items.OfType<Artist>().Where(i => i.IsOnTour == getArtists.IsOnTour.Value);
}
return items;
}
/// <summary>
/// Gets all items.
/// </summary>

View File

@ -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;
/// <summary>
/// Initializes a new instance of the <see cref="HttpClientManager" /> class.
@ -63,8 +61,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
_logger = logger;
_jsonSerializer = jsonSerializer;
_appPaths = appPaths;
_cacheRepository = new FileSystemRepository(Path.Combine(_appPaths.CachePath, "downloads"));
}
/// <summary>
@ -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<HttpResponseInfo>(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
}
}
}
}
/// <summary>

View File

@ -19,7 +19,7 @@ namespace MediaBrowser.Common.Net
/// </summary>
/// <value>The accept header.</value>
public string AcceptHeader { get; set; }
/// <summary>
/// Gets or sets the cancellation token.
/// </summary>
@ -45,13 +45,14 @@ namespace MediaBrowser.Common.Net
public IProgress<double> Progress { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [enable response caching].
/// Gets or sets a value indicating whether [enable HTTP compression].
/// </summary>
/// <value><c>true</c> if [enable response caching]; otherwise, <c>false</c>.</value>
public bool EnableResponseCache { get; set; }
/// <value><c>true</c> if [enable HTTP compression]; otherwise, <c>false</c>.</value>
public bool EnableHttpCompression { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="HttpRequestOptions"/> class.
/// </summary>
public HttpRequestOptions()
{
EnableHttpCompression = true;

View File

@ -15,10 +15,5 @@ namespace MediaBrowser.Controller.Entities.Audio
return "Artist-" + Name;
}
/// <summary>
/// Gets or sets a value indicating whether this instance is on tour.
/// </summary>
/// <value><c>true</c> if this instance is on tour; otherwise, <c>false</c>.</value>
public bool IsOnTour { get; set; }
}
}

View File

@ -113,7 +113,6 @@ namespace MediaBrowser.Controller.Providers.Music
Url = url,
ResourcePool = LastfmResourcePool,
CancellationToken = cancellationToken,
EnableResponseCache = true,
EnableHttpCompression = false
}).ConfigureAwait(false))

View File

@ -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
{
/// <summary>
/// Initializes a new instance of the <see cref="LastfmArtistByNameProvider"/> class.
/// Initializes a new instance of the <see cref="LastfmArtistByNameProvider" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="httpClient">The HTTP client.</param>
/// <param name="logManager">The log manager.</param>
/// <param name="configurationManager">The configuration manager.</param>
/// <param name="providerManager">The provider manager.</param>
/// <param name="libraryManager">The library manager.</param>
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;
}
/// <summary>
/// Gets the provider version.
/// </summary>
/// <value>The provider version.</value>
protected override string ProviderVersion
{
get
{
return "7";
}
}
/// <summary>
/// Fetches the lastfm data.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="musicBrainzId">The music brainz id.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
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);
}
}
/// <summary>
/// Finds the music artist.
/// </summary>
/// <param name="artist">The artist.</param>
/// <param name="libraryManager">The library manager.</param>
/// <returns>MusicArtist.</returns>
private static MusicArtist FindMusicArtist(Artist artist, ILibraryManager libraryManager)
{
var musicBrainzId = artist.GetProviderId(MetadataProviders.Musicbrainz);
return libraryManager.RootFolder.RecursiveChildren
.OfType<MusicArtist>()
.FirstOrDefault(i =>
{
if (!string.IsNullOrWhiteSpace(musicBrainzId) && string.Equals(musicBrainzId, i.GetProviderId(MetadataProviders.Musicbrainz), StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
});
}
}
}

View File

@ -31,7 +31,7 @@ namespace MediaBrowser.Controller.Providers.Music
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager;
protected readonly ILibraryManager LibraryManager;
/// <summary>
/// Initializes a new instance of the <see cref="LastfmArtistProvider"/> 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
/// <returns>System.String.</returns>
private string FindIdFromMusicArtistEntity(BaseItem item)
{
var artist = _libraryManager.RootFolder.RecursiveChildren.OfType<MusicArtist>()
var artist = LibraryManager.RootFolder.RecursiveChildren.OfType<MusicArtist>()
.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))

View File

@ -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);

View File

@ -6,10 +6,5 @@ namespace MediaBrowser.Model.Querying
/// </summary>
public class ArtistsQuery : ItemsByNameQuery
{
/// <summary>
/// Filter by artists that are on tour, or not
/// </summary>
/// <value><c>null</c> if [is on tour] contains no value, <c>true</c> if [is on tour]; otherwise, <c>false</c>.</value>
public bool? IsOnTour { get; set; }
}
}