Skip to content

Commit 3ebb446

Browse files
committed
Added new endpoint "recently played"
1 parent 281b684 commit 3ebb446

File tree

9 files changed

+98
-3
lines changed

9 files changed

+98
-3
lines changed

SpotifyAPI.Example/WebControl.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ private async void InitialSetup()
3737
return;
3838
}
3939

40+
var test = _spotify.GetUsersRecentlyPlayedTracks();
41+
4042
authButton.Enabled = false;
4143
_profile = _spotify.GetPrivateProfile();
4244

@@ -108,7 +110,8 @@ private async void RunAuthentication()
108110
8000,
109111
"26d287105e31491889f3cd293d85bfea",
110112
Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate | Scope.UserLibraryRead |
111-
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative);
113+
Scope.UserReadPrivate | Scope.UserFollowRead | Scope.UserReadBirthdate | Scope.UserTopRead | Scope.PlaylistReadCollaborative |
114+
Scope.UserReadRecentlyPlayed);
112115

113116
try
114117
{

SpotifyAPI/SpotifyAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<Compile Include="Web\Models\FullArtist.cs" />
9191
<Compile Include="Web\Models\FullTrack.cs" />
9292
<Compile Include="Web\Models\NewAlbumReleases.cs" />
93+
<Compile Include="Web\Models\PlayHistory.cs" />
9394
<Compile Include="Web\Models\Recommendations.cs" />
9495
<Compile Include="Web\Models\RecommendationSeed .cs" />
9596
<Compile Include="Web\Models\RecommendationSeedGenres.cs" />

SpotifyAPI/Web/Enums/Scope.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public enum Scope
4545
UserTopRead = 4096,
4646

4747
[String("playlist-read-collaborative")]
48-
PlaylistReadCollaborative = 8192
48+
PlaylistReadCollaborative = 8192,
49+
50+
[String("user-read-recently-played")]
51+
UserReadRecentlyPlayed = 16384
4952
}
5053
}

SpotifyAPI/Web/Models/CursorPaging.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SpotifyAPI.Web.Models
66
{
7-
public class CursorPaging<T>
7+
public class CursorPaging<T> : BasicModel
88
{
99
[JsonProperty("href")]
1010
public string Href { get; set; }

SpotifyAPI/Web/Models/GeneralModels.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,23 @@ public class Cursor
137137
{
138138
[JsonProperty("after")]
139139
public string After { get; set; }
140+
141+
[JsonProperty("before")]
142+
public string Before { get; set; }
143+
}
144+
145+
public class Context
146+
{
147+
[JsonProperty("type")]
148+
public string Type { get; set; }
149+
150+
[JsonProperty("href")]
151+
public string Href { get; set; }
152+
153+
[JsonProperty("external_urls")]
154+
public Dictionary<string, string> ExternalUrls { get; set; }
155+
156+
[JsonProperty("uri")]
157+
public string Uri { get; set; }
140158
}
141159
}

SpotifyAPI/Web/Models/PlayHistory.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using Newtonsoft.Json;
3+
4+
namespace SpotifyAPI.Web.Models
5+
{
6+
public class PlayHistory : BasicModel
7+
{
8+
[JsonProperty("track")]
9+
public SimpleTrack Track { get; set; }
10+
11+
[JsonProperty("played_at")]
12+
public DateTime PlayedAt { get; set; }
13+
14+
[JsonProperty("context")]
15+
public Context Context { get; set; }
16+
}
17+
}

SpotifyAPI/Web/SpotifyWebAPI.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,34 @@ public Task<Paging<FullArtist>> GetUsersTopArtistsAsync(TimeRangeType timeRange
12401240
return DownloadDataAsync<Paging<FullArtist>>(_builder.GetUsersTopArtists(timeRange, limit, offest));
12411241
}
12421242

1243+
/// <summary>
1244+
/// Get tracks from the current user’s recent play history.
1245+
/// </summary>
1246+
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. </param>
1247+
/// <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>
1248+
/// <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>
1249+
/// <returns></returns>
1250+
/// <remarks>AUTH NEEDED</remarks>
1251+
public CursorPaging<PlayHistory> GetUsersRecentlyPlayedTracks(int limit = 20, DateTime? after = null,
1252+
DateTime? before = null)
1253+
{
1254+
return DownloadData<CursorPaging<PlayHistory>>(_builder.GetUsersRecentlyPlayedTracks(limit, after, before));
1255+
}
1256+
1257+
/// <summary>
1258+
/// Get tracks from the current user’s recent play history asynchronously
1259+
/// </summary>
1260+
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. </param>
1261+
/// <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>
1262+
/// <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>
1263+
/// <returns></returns>
1264+
/// <remarks>AUTH NEEDED</remarks>
1265+
public Task<CursorPaging<PlayHistory>> GetUsersRecentlyPlayedTracksAsync(int limit = 20, DateTime? after = null,
1266+
DateTime? before = null)
1267+
{
1268+
return DownloadDataAsync<CursorPaging<PlayHistory>>(_builder.GetUsersRecentlyPlayedTracks(limit, after, before));
1269+
}
1270+
12431271
#endregion
12441272

12451273
#region Playlists

SpotifyAPI/Web/SpotifyWebBuilder.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,26 @@ public string GetUsersTopArtists(TimeRangeType timeRange = TimeRangeType.MediumT
587587
return builder.ToString();
588588
}
589589

590+
/// <summary>
591+
/// Get tracks from the current user’s recent play history.
592+
/// </summary>
593+
/// <param name="limit">The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. </param>
594+
/// <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>
595+
/// <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>
596+
/// <returns></returns>
597+
/// <remarks>AUTH NEEDED</remarks>
598+
public string GetUsersRecentlyPlayedTracks(int limit = 20, DateTime? after = null, DateTime? before = null)
599+
{
600+
limit = Math.Min(50, limit);
601+
StringBuilder builder = new StringBuilder($"{APIBase}/me/player/recently-played");
602+
builder.Append("?limit=" + limit);
603+
if (after.HasValue)
604+
builder.Append("&after=" + after.Value.ToUnixTimeMillisecondsPoly());
605+
if (before.HasValue)
606+
builder.Append("&before=" + before.Value.ToUnixTimeMillisecondsPoly());
607+
return builder.ToString();
608+
}
609+
590610
#endregion
591611

592612
#region Playlists

SpotifyAPI/Web/Util.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public static string GetStringAttribute<T>(this T en, string separator) where T
2222
attributes.ToList().ForEach(element => list.Add(element.Text));
2323
return string.Join(separator, list);
2424
}
25+
26+
public static int ToUnixTimeMillisecondsPoly(this DateTime time)
27+
{
28+
return (int)time.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
29+
}
2530
}
2631

2732
public sealed class StringAttribute : Attribute

0 commit comments

Comments
 (0)