jellyfin/MediaBrowser.Controller/Providers/Music/LastfmBaseProvider.cs

319 lines
11 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Net;
2013-03-03 10:07:13 -07:00
using MediaBrowser.Common.Net;
2013-03-03 22:43:06 -07:00
using MediaBrowser.Controller.Configuration;
2013-03-03 10:07:13 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Controller.Providers.Music
{
class LastfmProviderException : ApplicationException
{
public LastfmProviderException(string msg)
: base(msg)
{
}
}
/// <summary>
/// Class MovieDbProvider
/// </summary>
public abstract class LastfmBaseProvider : BaseMetadataProvider
{
2013-03-04 08:33:54 -07:00
protected static readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(5, 5);
2013-03-03 10:07:13 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="LastfmBaseProvider" /> class.
/// </summary>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="httpClient">The HTTP client.</param>
2013-03-03 22:43:06 -07:00
/// <param name="logManager">The log manager.</param>
/// <param name="configurationManager">The configuration manager.</param>
2013-03-03 10:07:13 -07:00
/// <exception cref="System.ArgumentNullException">jsonSerializer</exception>
2013-03-03 22:43:06 -07:00
protected LastfmBaseProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(logManager, configurationManager)
2013-03-03 10:07:13 -07:00
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (httpClient == null)
{
throw new ArgumentNullException("httpClient");
}
JsonSerializer = jsonSerializer;
HttpClient = httpClient;
}
2013-03-03 22:43:06 -07:00
/// <summary>
/// Gets the json serializer.
/// </summary>
/// <value>The json serializer.</value>
protected IJsonSerializer JsonSerializer { get; private set; }
/// <summary>
/// Gets the HTTP client.
/// </summary>
/// <value>The HTTP client.</value>
protected IHttpClient HttpClient { get; private set; }
/// <summary>
/// The name of the local json meta file for this item type
/// </summary>
protected string LocalMetaFileName { get; set; }
/// <summary>
/// If we save locally, refresh if they delete something
/// </summary>
protected override bool RefreshOnFileSystemStampChange
{
get
{
return ConfigurationManager.Configuration.SaveLocalMeta;
}
}
2013-03-03 10:07:13 -07:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Second; }
}
/// <summary>
/// Gets a value indicating whether [requires internet].
/// </summary>
/// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
public override bool RequiresInternet
{
get
{
return true;
}
}
protected const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
protected static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
2013-03-03 10:07:13 -07:00
/// <summary>
/// Determines whether [has local meta] [the specified item].
2013-03-03 10:07:13 -07:00
/// </summary>
/// <param name="item">The item.</param>
/// <returns><c>true</c> if [has local meta] [the specified item]; otherwise, <c>false</c>.</returns>
protected bool HasLocalMeta(BaseItem item)
2013-03-03 10:07:13 -07:00
{
return item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName);
}
/// <summary>
/// Fetches the items data.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="cancellationToken"></param>
/// <returns>Task.</returns>
protected virtual async Task FetchData(BaseItem item, CancellationToken cancellationToken)
{
var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
if (id != null)
2013-03-03 10:07:13 -07:00
{
2013-03-05 09:48:17 -07:00
Logger.Debug("LastfmProvider - getting info for {0}", item.Name);
cancellationToken.ThrowIfCancellationRequested();
item.SetProviderId(MetadataProviders.Musicbrainz, id);
await FetchLastfmData(item, id, cancellationToken).ConfigureAwait(false);
}
else
{
Logger.Info("LastfmProvider could not find " + item.Name + ". Check name on Last.fm.");
2013-03-03 10:07:13 -07:00
}
2013-03-03 10:07:13 -07:00
}
protected abstract Task<string> FindId(BaseItem item, CancellationToken cancellationToken);
protected abstract Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken);
/// <summary>
/// Encodes an URL.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>System.String.</returns>
protected static string UrlEncode(string name)
{
return WebUtility.UrlEncode(name);
}
2013-03-03 10:07:13 -07:00
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (item.DontFetchMeta) return false;
2013-03-03 22:43:06 -07:00
if (ConfigurationManager.Configuration.SaveLocalMeta && HasFileSystemStampChanged(item, providerInfo))
2013-03-03 10:07:13 -07:00
{
//If they deleted something from file system, chances are, this item was mis-identified the first time
item.SetProviderId(MetadataProviders.Musicbrainz, null);
Logger.Debug("LastfmProvider reports file system stamp change...");
return true;
}
if (providerInfo.LastRefreshStatus == ProviderRefreshStatus.CompletedWithErrors)
{
Logger.Debug("LastfmProvider for {0} - last attempt had errors. Will try again.", item.Path);
return true;
}
var downloadDate = providerInfo.LastRefreshed;
2013-03-03 22:43:06 -07:00
if (ConfigurationManager.Configuration.MetadataRefreshDays == -1 && downloadDate != DateTime.MinValue)
2013-03-03 10:07:13 -07:00
{
return false;
}
if (DateTime.Today.Subtract(item.DateCreated).TotalDays > 180 && downloadDate != DateTime.MinValue)
return false; // don't trigger a refresh data for item that are more than 6 months old and have been refreshed before
2013-03-03 22:43:06 -07:00
if (DateTime.Today.Subtract(downloadDate).TotalDays < ConfigurationManager.Configuration.MetadataRefreshDays) // only refresh every n days
2013-03-03 10:07:13 -07:00
return false;
2013-03-03 22:43:06 -07:00
Logger.Debug("LastfmProvider - " + item.Name + " needs refresh. Download date: " + downloadDate + " item created date: " + item.DateCreated + " Check for Update age: " + ConfigurationManager.Configuration.MetadataRefreshDays);
2013-03-03 10:07:13 -07:00
return true;
}
/// <summary>
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
/// </summary>
/// <param name="item">The item.</param>
/// <param name="force">if set to <c>true</c> [force].</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>Task{System.Boolean}.</returns>
protected override async Task<bool> FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken)
{
if (item.DontFetchMeta)
{
Logger.Info("LastfmProvider - Not fetching because requested to ignore " + item.Name);
return false;
}
cancellationToken.ThrowIfCancellationRequested();
2013-03-03 22:43:06 -07:00
if (!ConfigurationManager.Configuration.SaveLocalMeta || !HasLocalMeta(item) || (force && !HasLocalMeta(item)))
2013-03-03 10:07:13 -07:00
{
try
{
await FetchData(item, cancellationToken).ConfigureAwait(false);
SetLastRefreshed(item, DateTime.UtcNow);
}
catch (LastfmProviderException)
{
SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors);
}
return true;
}
Logger.Debug("LastfmProvider not fetching because local meta exists for " + item.Name);
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
}
2013-03-03 10:07:13 -07:00
#region Result Objects
2013-03-03 10:07:13 -07:00
public class LastfmStats
{
public string listeners { get; set; }
public string playcount { get; set; }
}
2013-03-03 13:55:30 -07:00
public class LastfmTag
{
public string name { get; set; }
public string url { get; set; }
}
2013-03-03 13:55:30 -07:00
2013-03-04 07:34:00 -07:00
public class LastfmTags
{
public List<LastfmTag> tag { get; set; }
}
2013-03-03 13:55:30 -07:00
public class LastfmFormationInfo
{
public string yearfrom { get; set; }
public string yearto { get; set; }
}
2013-03-03 13:55:30 -07:00
public class LastFmBio
{
public string published { get; set; }
public string summary { get; set; }
public string content { get; set; }
public string placeformed { get; set; }
public string yearformed { get; set; }
public List<LastfmFormationInfo> formationlist { get; set; }
}
2013-03-03 10:07:13 -07:00
public class LastfmArtist
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public string streamable { get; set; }
public string ontour { get; set; }
public LastfmStats stats { get; set; }
public List<LastfmArtist> similar { get; set; }
public LastfmTags tags { get; set; }
public LastFmBio bio { get; set; }
}
2013-03-03 10:07:13 -07:00
2013-03-05 09:48:17 -07:00
public class LastfmAlbum
{
public string name { get; set; }
public string artist { get; set; }
public string id { get; set; }
public string mbid { get; set; }
public string releasedate { get; set; }
public int listeners { get; set; }
public int playcount { get; set; }
public LastfmTags toptags { get; set; }
public LastFmBio wiki { get; set; }
}
public class LastfmGetAlbumResult
{
public LastfmAlbum album { get; set; }
}
public class LastfmGetArtistResult
{
public LastfmArtist artist { get; set; }
}
public class Artistmatches
{
public List<LastfmArtist> artist { get; set; }
2013-03-03 10:07:13 -07:00
}
public class LastfmArtistSearchResult
{
public Artistmatches artistmatches { get; set; }
}
public class LastfmArtistSearchResults
{
public LastfmArtistSearchResult results { get; set; }
}
#endregion
2013-03-03 10:07:13 -07:00
}