Added new endpoint "recently played"

This commit is contained in:
Jonas Dellinger 2017-03-02 22:28:55 +01:00
parent 281b684f5f
commit 3ebb446745
9 changed files with 98 additions and 3 deletions

View File

@ -37,6 +37,8 @@ namespace SpotifyAPI.Example
return; return;
} }
var test = _spotify.GetUsersRecentlyPlayedTracks();
authButton.Enabled = false; authButton.Enabled = false;
_profile = _spotify.GetPrivateProfile(); _profile = _spotify.GetPrivateProfile();
@ -108,7 +110,8 @@ namespace SpotifyAPI.Example
8000, 8000,
"26d287105e31491889f3cd293d85bfea", "26d287105e31491889f3cd293d85bfea",
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead | Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative); Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative |
Scope.UserReadRecentlyPlayed);
try try
{ {

View File

@ -90,6 +90,7 @@
<Compile Include="Web\Models\FullArtist.cs" /> <Compile Include="Web\Models\FullArtist.cs" />
<Compile Include="Web\Models\FullTrack.cs" /> <Compile Include="Web\Models\FullTrack.cs" />
<Compile Include="Web\Models\NewAlbumReleases.cs" /> <Compile Include="Web\Models\NewAlbumReleases.cs" />
<Compile Include="Web\Models\PlayHistory.cs" />
<Compile Include="Web\Models\Recommendations.cs" /> <Compile Include="Web\Models\Recommendations.cs" />
<Compile Include="Web\Models\RecommendationSeed .cs" /> <Compile Include="Web\Models\RecommendationSeed .cs" />
<Compile Include="Web\Models\RecommendationSeedGenres.cs" /> <Compile Include="Web\Models\RecommendationSeedGenres.cs" />

View File

@ -45,6 +45,9 @@ namespace SpotifyAPI.Web.Enums
UserTopRead = 4096, UserTopRead = 4096,
[String("playlist-read-collaborative")] [String("playlist-read-collaborative")]
PlaylistReadCollaborative = 8192 PlaylistReadCollaborative = 8192,
[String("user-read-recently-played")]
UserReadRecentlyPlayed = 16384
} }
} }

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace SpotifyAPI.Web.Models namespace SpotifyAPI.Web.Models
{ {
public class CursorPaging<T> public class CursorPaging<T> : BasicModel
{ {
[JsonProperty("href")] [JsonProperty("href")]
public string Href { get; set; } public string Href { get; set; }

View File

@ -137,5 +137,23 @@ namespace SpotifyAPI.Web.Models
{ {
[JsonProperty("after")] [JsonProperty("after")]
public string After { get; set; } public string After { get; set; }
[JsonProperty("before")]
public string Before { get; set; }
}
public class Context
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("href")]
public string Href { get; set; }
[JsonProperty("external_urls")]
public Dictionary<string, string> ExternalUrls { get; set; }
[JsonProperty("uri")]
public string Uri { get; set; }
} }
} }

View File

@ -0,0 +1,17 @@
using System;
using Newtonsoft.Json;
namespace SpotifyAPI.Web.Models
{
public class PlayHistory : BasicModel
{
[JsonProperty("track")]
public SimpleTrack Track { get; set; }
[JsonProperty("played_at")]
public DateTime PlayedAt { get; set; }
[JsonProperty("context")]
public Context Context { get; set; }
}
}

View File

@ -1240,6 +1240,34 @@ namespace SpotifyAPI.Web
return DownloadDataAsync<Paging<FullArtist>>(_builder.GetUsersTopArtists(timeRange, limit, offest)); return DownloadDataAsync<Paging<FullArtist>>(_builder.GetUsersTopArtists(timeRange, limit, offest));
} }
/// <summary>
/// Get tracks from the current users recent play history.
/// </summary>
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. </param>
/// <param name="after">A Unix timestamp in milliseconds. Returns all items after (but not including) this cursor position. If after is specified, before must not be specified.</param>
/// <param name="before">A Unix timestamp in milliseconds. Returns all items before (but not including) this cursor position. If before is specified, after must not be specified.</param>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public CursorPaging<PlayHistory> GetUsersRecentlyPlayedTracks(int limit = 20, DateTime? after = null,
DateTime? before = null)
{
return DownloadData<CursorPaging<PlayHistory>>(_builder.GetUsersRecentlyPlayedTracks(limit, after, before));
}
/// <summary>
/// Get tracks from the current users recent play history asynchronously
/// </summary>
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. </param>
/// <param name="after">A Unix timestamp in milliseconds. Returns all items after (but not including) this cursor position. If after is specified, before must not be specified.</param>
/// <param name="before">A Unix timestamp in milliseconds. Returns all items before (but not including) this cursor position. If before is specified, after must not be specified.</param>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public Task<CursorPaging<PlayHistory>> GetUsersRecentlyPlayedTracksAsync(int limit = 20, DateTime? after = null,
DateTime? before = null)
{
return DownloadDataAsync<CursorPaging<PlayHistory>>(_builder.GetUsersRecentlyPlayedTracks(limit, after, before));
}
#endregion #endregion
#region Playlists #region Playlists

View File

@ -587,6 +587,26 @@ namespace SpotifyAPI.Web
return builder.ToString(); return builder.ToString();
} }
/// <summary>
/// Get tracks from the current users recent play history.
/// </summary>
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. </param>
/// <param name="after">A Unix timestamp in milliseconds. Returns all items after (but not including) this cursor position. If after is specified, before must not be specified.</param>
/// <param name="before">A Unix timestamp in milliseconds. Returns all items before (but not including) this cursor position. If before is specified, after must not be specified.</param>
/// <returns></returns>
/// <remarks>AUTH NEEDED</remarks>
public string GetUsersRecentlyPlayedTracks(int limit = 20, DateTime? after = null, DateTime? before = null)
{
limit = Math.Min(50, limit);
StringBuilder builder = new StringBuilder($"{APIBase}/me/player/recently-played");
builder.Append("?limit=" + limit);
if (after.HasValue)
builder.Append("&after=" + after.Value.ToUnixTimeMillisecondsPoly());
if (before.HasValue)
builder.Append("&before=" + before.Value.ToUnixTimeMillisecondsPoly());
return builder.ToString();
}
#endregion #endregion
#region Playlists #region Playlists

View File

@ -22,6 +22,11 @@ namespace SpotifyAPI.Web
attributes.ToList().ForEach(element => list.Add(element.Text)); attributes.ToList().ForEach(element => list.Add(element.Text));
return string.Join(separator, list); return string.Join(separator, list);
} }
public static int ToUnixTimeMillisecondsPoly(this DateTime time)
{
return (int)time.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
}
} }
public sealed class StringAttribute : Attribute public sealed class StringAttribute : Attribute