Added Recommendation Endpoints

This commit is contained in:
Johnny @PC 2016-04-01 13:23:11 +02:00
parent 7bb0cc6391
commit 8c0d765eef
8 changed files with 271 additions and 0 deletions

View File

@ -4,6 +4,7 @@ using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
@ -68,6 +69,15 @@ namespace SpotifyAPI.Example
return;
}
TuneableTrack asd = new TuneableTrack
{
Acousticness = 0.0029f
};
List<string> artists = new List<string>() { "0daugAjUgbJSqdlyYNwIbT" };
Recommendations reco = _spotify.GetRecommendations(target:asd, artistSeed:artists);
RecommendationSeedGenres genres = _spotify.GetRecommendationSeedsGenres();
authButton.Enabled = false;
_profile = _spotify.GetPrivateProfile();

View File

@ -88,6 +88,9 @@
<Compile Include="Web\Models\FullArtist.cs" />
<Compile Include="Web\Models\FullTrack.cs" />
<Compile Include="Web\Models\NewAlbumReleases.cs" />
<Compile Include="Web\Models\Recommendations.cs" />
<Compile Include="Web\Models\RecommendationSeed .cs" />
<Compile Include="Web\Models\RecommendationSeedGenres.cs" />
<Compile Include="Web\Models\SearchItem.cs" />
<Compile Include="Web\Models\PrivateProfile.cs" />
<Compile Include="Web\Models\GeneralModels.cs" />
@ -106,6 +109,7 @@
<Compile Include="Web\Models\Paging.cs" />
<Compile Include="Web\Enums\Scope.cs" />
<Compile Include="Web\Enums\SearchType.cs" />
<Compile Include="Web\Models\TuneableTrack.cs" />
<Compile Include="Web\SimpleHttpServer.cs" />
<Compile Include="Web\SpotifyWebAPI.cs" />
<Compile Include="Web\SpotifyWebBuilder.cs" />

View File

@ -0,0 +1,25 @@
using Newtonsoft.Json;
namespace SpotifyAPI.Web.Models
{
public class RecommendationSeed
{
[JsonProperty("afterFilteringSize")]
public int AfterFilteringSize { get; set; }
[JsonProperty("afterRelinkingSize")]
public int AfterRelinkingSize { get; set; }
[JsonProperty("href")]
public string Href { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("initialPoolSize")]
public int InitialPoolSize { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
namespace SpotifyAPI.Web.Models
{
public class RecommendationSeedGenres : BasicModel
{
[JsonProperty("genres")]
public List<string> Genres { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace SpotifyAPI.Web.Models
{
public class Recommendations : BasicModel
{
[JsonProperty("seeds")]
public List<RecommendationSeed> Seeds { get; set; }
[JsonProperty("tracks")]
public List<SimpleTrack> Tracks { get; set; }
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
namespace SpotifyAPI.Web.Models
{
public class TuneableTrack
{
[String("acousticness")]
public float? Acousticness { get; set; }
[String("danceability")]
public float? Danceability { get; set; }
[String("duration_ms")]
public int? DurationMs { get; set; }
[String("energy")]
public float? Energy { get; set; }
[String("instrumentalness")]
public float? Instrumentalness { get; set; }
[String("key")]
public int? Key { get; set; }
[String("liveness")]
public float? Liveness { get; set; }
[String("loudness")]
public float? Loudness { get; set; }
[String("mode")]
public int? Mode { get; set; }
[String("popularity")]
public int? Popularity { get; set; }
[String("speechiness")]
public float? Speechiness { get; set; }
[String("tempo")]
public float? Tempo { get; set; }
[String("time_signature")]
public int? TimeSignature { get; set; }
[String("valence")]
public float? Valence { get; set; }
public string BuildUrlParams(string prefix)
{
List<string> urlParams = new List<string>();
foreach (PropertyInfo info in GetType().GetProperties())
{
object value = info.GetValue(this);
string name = info.GetCustomAttribute<StringAttribute>()?.Text;
if(name == null || value == null)
continue;
if (value is float)
urlParams.Add($"{prefix}_{name}={((float)value).ToString(CultureInfo.InvariantCulture)}");
else
urlParams.Add($"{prefix}_{name}={value}");
}
if (urlParams.Count > 0)
return "&" + string.Join("&", urlParams);
return "";
}
}
}

View File

@ -10,6 +10,7 @@ using System.Threading.Tasks;
namespace SpotifyAPI.Web
{
// ReSharper disable once InconsistentNaming
public sealed class SpotifyWebAPI : IDisposable
{
[Obsolete("This Property will be removed soon. Please use SpotifyWebBuilder.APIBase")]
@ -461,6 +462,82 @@ namespace SpotifyAPI.Web
return await DownloadDataAsync<CategoryPlaylist>(_builder.GetCategoryPlaylists(categoryId, country, limit, offset));
}
/// <summary>
/// Create a playlist-style listening experience based on seed artists, tracks and genres.
/// </summary>
/// <param name="artistSeed">A comma separated list of Spotify IDs for seed artists.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="genreSeed">A comma separated list of any genres in the set of available genre seeds.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="trackSeed">A comma separated list of Spotify IDs for a seed track.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="target">Tracks with the attribute values nearest to the target values will be preferred.</param>
/// <param name="min">For each tunable track attribute, a hard floor on the selected track attributes value can be provided</param>
/// <param name="max">For each tunable track attribute, a hard ceiling on the selected track attributes value can be provided</param>
/// <param name="limit">The target size of the list of recommended tracks. Default: 20. Minimum: 1. Maximum: 100.
/// For seeds with unusually small pools or when highly restrictive filtering is applied, it may be impossible to generate the requested number of recommended tracks.
/// </param>
/// <param name="market">An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking.
/// Because min_*, max_* and target_* are applied to pools before relinking, the generated results may not precisely match the filters applied.</param>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public Recommendations GetRecommendations(List<string> artistSeed = null, List<string> genreSeed = null, List<string> trackSeed = null,
TuneableTrack target = null, TuneableTrack min = null, TuneableTrack max = null, int limit = 20, string market = "")
{
return DownloadData<Recommendations>(_builder.GetRecommendations(artistSeed, genreSeed, trackSeed, target, min, max, limit, market));
}
/// <summary>
/// Create a playlist-style listening experience based on seed artists, tracks and genres asynchronously.
/// </summary>
/// <param name="artistSeed">A comma separated list of Spotify IDs for seed artists.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="genreSeed">A comma separated list of any genres in the set of available genre seeds.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="trackSeed">A comma separated list of Spotify IDs for a seed track.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="target">Tracks with the attribute values nearest to the target values will be preferred.</param>
/// <param name="min">For each tunable track attribute, a hard floor on the selected track attributes value can be provided</param>
/// <param name="max">For each tunable track attribute, a hard ceiling on the selected track attributes value can be provided</param>
/// <param name="limit">The target size of the list of recommended tracks. Default: 20. Minimum: 1. Maximum: 100.
/// For seeds with unusually small pools or when highly restrictive filtering is applied, it may be impossible to generate the requested number of recommended tracks.
/// </param>
/// <param name="market">An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking.
/// Because min_*, max_* and target_* are applied to pools before relinking, the generated results may not precisely match the filters applied.</param>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public async Task<Recommendations> GetRecommendationsAsync(List<string> artistSeed = null, List<string> genreSeed = null, List<string> trackSeed = null,
TuneableTrack target = null, TuneableTrack min = null, TuneableTrack max = null, int limit = 20, string market = "")
{
return await DownloadDataAsync<Recommendations>(_builder.GetRecommendations(artistSeed, genreSeed, trackSeed, target, min, max, limit, market));
}
/// <summary>
/// Retrieve a list of available genres seed parameter values for recommendations.
/// </summary>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public RecommendationSeedGenres GetRecommendationSeedsGenres()
{
return DownloadData<RecommendationSeedGenres>(_builder.GetRecommendationSeedsGenres());
}
/// <summary>
/// Retrieve a list of available genres seed parameter values for recommendations asynchronously.
/// </summary>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public async Task<RecommendationSeedGenres> GetRecommendationSeedsGenresAsync()
{
return await DownloadDataAsync<RecommendationSeedGenres>(_builder.GetRecommendationSeedsGenres());
}
#endregion Browse
#region Follow

View File

@ -284,6 +284,61 @@ namespace SpotifyAPI.Web
return builder.ToString();
}
/// <summary>
/// Create a playlist-style listening experience based on seed artists, tracks and genres.
/// </summary>
/// <param name="artistSeed">A comma separated list of Spotify IDs for seed artists.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="genreSeed">A comma separated list of any genres in the set of available genre seeds.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="trackSeed">A comma separated list of Spotify IDs for a seed track.
/// Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres.
/// </param>
/// <param name="target">Tracks with the attribute values nearest to the target values will be preferred.</param>
/// <param name="min">For each tunable track attribute, a hard floor on the selected track attributes value can be provided</param>
/// <param name="max">For each tunable track attribute, a hard ceiling on the selected track attributes value can be provided</param>
/// <param name="limit">The target size of the list of recommended tracks. Default: 20. Minimum: 1. Maximum: 100.
/// For seeds with unusually small pools or when highly restrictive filtering is applied, it may be impossible to generate the requested number of recommended tracks.
/// </param>
/// <param name="market">An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking.
/// Because min_*, max_* and target_* are applied to pools before relinking, the generated results may not precisely match the filters applied.</param>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public string GetRecommendations(List<string> artistSeed = null, List<string> genreSeed = null, List<string> trackSeed = null,
TuneableTrack target = null, TuneableTrack min = null, TuneableTrack max = null, int limit = 20, string market = "")
{
limit = Math.Min(100, limit);
StringBuilder builder = new StringBuilder($"{APIBase}/recommendations");
builder.Append("?limit=" + limit);
if (artistSeed?.Count > 0)
builder.Append("&seed_artists=" + string.Join(",", artistSeed));
if (genreSeed?.Count > 0)
builder.Append("&seed_genres=" + string.Join(",", genreSeed));
if (trackSeed?.Count > 0)
builder.Append("&seed_tracks=" + string.Join(",", trackSeed));
if (target != null)
builder.Append(target.BuildUrlParams("target"));
if (min != null)
builder.Append(min.BuildUrlParams("min"));
if (max != null)
builder.Append(max.BuildUrlParams("max"));
if (!string.IsNullOrEmpty(market))
builder.Append("&market=" + market);
return builder.ToString();
}
/// <summary>
/// Retrieve a list of available genres seed parameter values for recommendations.
/// </summary>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public string GetRecommendationSeedsGenres()
{
return $"{APIBase}/recommendations/available-genre-seeds";
}
#endregion Browse
#region Follow