using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Music
{
///
/// Class MovieDbProvider
///
public abstract class LastfmBaseProvider : BaseMetadataProvider
{
protected static readonly SemaphoreSlim LastfmResourcePool = new SemaphoreSlim(4, 4);
///
/// Initializes a new instance of the class.
///
/// The json serializer.
/// The HTTP client.
/// The log manager.
/// The configuration manager.
/// jsonSerializer
protected LastfmBaseProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager)
: base(logManager, configurationManager)
{
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (httpClient == null)
{
throw new ArgumentNullException("httpClient");
}
JsonSerializer = jsonSerializer;
HttpClient = httpClient;
}
protected override string ProviderVersion
{
get
{
return "5";
}
}
protected override bool RefreshOnVersionChange
{
get
{
return true;
}
}
///
/// Gets the json serializer.
///
/// The json serializer.
protected IJsonSerializer JsonSerializer { get; private set; }
///
/// Gets the HTTP client.
///
/// The HTTP client.
protected IHttpClient HttpClient { get; private set; }
///
/// The name of the local json meta file for this item type
///
protected string LocalMetaFileName { get; set; }
protected virtual bool SaveLocalMeta
{
get
{
return ConfigurationManager.Configuration.SaveLocalMeta;
}
}
///
/// If we save locally, refresh if they delete something
///
protected override bool RefreshOnFileSystemStampChange
{
get
{
return SaveLocalMeta;
}
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Second; }
}
///
/// Gets a value indicating whether [requires internet].
///
/// true if [requires internet]; otherwise, false.
public override bool RequiresInternet
{
get
{
return true;
}
}
protected const string RootUrl = @"http://ws.audioscrobbler.com/2.0/?";
protected static string ApiKey = "7b76553c3eb1d341d642755aecc40a33";
///
/// Determines whether [has local meta] [the specified item].
///
/// The item.
/// true if [has local meta] [the specified item]; otherwise, false.
protected bool HasLocalMeta(BaseItem item)
{
return item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName);
}
///
/// Fetches the items data.
///
/// The item.
///
/// Task.
protected virtual async Task FetchData(BaseItem item, CancellationToken cancellationToken)
{
var id = item.GetProviderId(MetadataProviders.Musicbrainz) ?? await FindId(item, cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrWhiteSpace(id))
{
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.");
}
}
protected abstract Task FindId(BaseItem item, CancellationToken cancellationToken);
protected abstract Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken);
///
/// Encodes an URL.
///
/// The name.
/// System.String.
protected static string UrlEncode(string name)
{
return WebUtility.UrlEncode(name);
}
///
/// Fetches metadata and returns true or false indicating if any work that requires persistence was done
///
/// The item.
/// if set to true [force].
/// The cancellation token
/// Task{System.Boolean}.
public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
await FetchData(item, cancellationToken).ConfigureAwait(false);
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
}
#region Result Objects
public class LastfmStats
{
public string listeners { get; set; }
public string playcount { get; set; }
}
public class LastfmTag
{
public string name { get; set; }
public string url { get; set; }
}
public class LastfmTags
{
public List tag { get; set; }
}
public class LastfmFormationInfo
{
public string yearfrom { get; set; }
public string yearto { get; set; }
}
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 formationlist { get; set; }
}
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 similar { get; set; }
public LastfmTags tags { get; set; }
public LastFmBio bio { get; set; }
}
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 artist { get; set; }
}
public class LastfmArtistSearchResult
{
public Artistmatches artistmatches { get; set; }
}
public class LastfmArtistSearchResults
{
public LastfmArtistSearchResult results { get; set; }
}
#endregion
}