From 3ebb446745c7acb9d83c5d90fd9fcc875ddddc39 Mon Sep 17 00:00:00 2001 From: Jonas Dellinger Date: Thu, 2 Mar 2017 22:28:55 +0100 Subject: [PATCH] Added new endpoint "recently played" --- SpotifyAPI.Example/WebControl.cs | 5 ++++- SpotifyAPI/SpotifyAPI.csproj | 1 + SpotifyAPI/Web/Enums/Scope.cs | 5 ++++- SpotifyAPI/Web/Models/CursorPaging.cs | 2 +- SpotifyAPI/Web/Models/GeneralModels.cs | 18 +++++++++++++++++ SpotifyAPI/Web/Models/PlayHistory.cs | 17 ++++++++++++++++ SpotifyAPI/Web/SpotifyWebAPI.cs | 28 ++++++++++++++++++++++++++ SpotifyAPI/Web/SpotifyWebBuilder.cs | 20 ++++++++++++++++++ SpotifyAPI/Web/Util.cs | 5 +++++ 9 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 SpotifyAPI/Web/Models/PlayHistory.cs diff --git a/SpotifyAPI.Example/WebControl.cs b/SpotifyAPI.Example/WebControl.cs index 5956f051..40a690b3 100644 --- a/SpotifyAPI.Example/WebControl.cs +++ b/SpotifyAPI.Example/WebControl.cs @@ -37,6 +37,8 @@ namespace SpotifyAPI.Example return; } + var test = _spotify.GetUsersRecentlyPlayedTracks(); + authButton.Enabled = false; _profile = _spotify.GetPrivateProfile(); @@ -108,7 +110,8 @@ namespace SpotifyAPI.Example 8000, "26d287105e31491889f3cd293d85bfea", 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 { diff --git a/SpotifyAPI/SpotifyAPI.csproj b/SpotifyAPI/SpotifyAPI.csproj index ad08e1e3..db749dd5 100644 --- a/SpotifyAPI/SpotifyAPI.csproj +++ b/SpotifyAPI/SpotifyAPI.csproj @@ -90,6 +90,7 @@ + diff --git a/SpotifyAPI/Web/Enums/Scope.cs b/SpotifyAPI/Web/Enums/Scope.cs index 1f0845f3..f451f6d0 100644 --- a/SpotifyAPI/Web/Enums/Scope.cs +++ b/SpotifyAPI/Web/Enums/Scope.cs @@ -45,6 +45,9 @@ namespace SpotifyAPI.Web.Enums UserTopRead = 4096, [String("playlist-read-collaborative")] - PlaylistReadCollaborative = 8192 + PlaylistReadCollaborative = 8192, + + [String("user-read-recently-played")] + UserReadRecentlyPlayed = 16384 } } \ No newline at end of file diff --git a/SpotifyAPI/Web/Models/CursorPaging.cs b/SpotifyAPI/Web/Models/CursorPaging.cs index 7beb83c9..cfb8be42 100644 --- a/SpotifyAPI/Web/Models/CursorPaging.cs +++ b/SpotifyAPI/Web/Models/CursorPaging.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; namespace SpotifyAPI.Web.Models { - public class CursorPaging + public class CursorPaging : BasicModel { [JsonProperty("href")] public string Href { get; set; } diff --git a/SpotifyAPI/Web/Models/GeneralModels.cs b/SpotifyAPI/Web/Models/GeneralModels.cs index 882fdd42..993323a3 100644 --- a/SpotifyAPI/Web/Models/GeneralModels.cs +++ b/SpotifyAPI/Web/Models/GeneralModels.cs @@ -137,5 +137,23 @@ namespace SpotifyAPI.Web.Models { [JsonProperty("after")] 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 ExternalUrls { get; set; } + + [JsonProperty("uri")] + public string Uri { get; set; } } } \ No newline at end of file diff --git a/SpotifyAPI/Web/Models/PlayHistory.cs b/SpotifyAPI/Web/Models/PlayHistory.cs new file mode 100644 index 00000000..ae1c28ec --- /dev/null +++ b/SpotifyAPI/Web/Models/PlayHistory.cs @@ -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; } + } +} \ No newline at end of file diff --git a/SpotifyAPI/Web/SpotifyWebAPI.cs b/SpotifyAPI/Web/SpotifyWebAPI.cs index 16f617d1..f2097de1 100644 --- a/SpotifyAPI/Web/SpotifyWebAPI.cs +++ b/SpotifyAPI/Web/SpotifyWebAPI.cs @@ -1240,6 +1240,34 @@ namespace SpotifyAPI.Web return DownloadDataAsync>(_builder.GetUsersTopArtists(timeRange, limit, offest)); } + /// + /// Get tracks from the current user’s recent play history. + /// + /// The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. + /// A Unix timestamp in milliseconds. Returns all items after (but not including) this cursor position. If after is specified, before must not be specified. + /// A Unix timestamp in milliseconds. Returns all items before (but not including) this cursor position. If before is specified, after must not be specified. + /// + /// AUTH NEEDED + public CursorPaging GetUsersRecentlyPlayedTracks(int limit = 20, DateTime? after = null, + DateTime? before = null) + { + return DownloadData>(_builder.GetUsersRecentlyPlayedTracks(limit, after, before)); + } + + /// + /// Get tracks from the current user’s recent play history asynchronously + /// + /// The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. + /// A Unix timestamp in milliseconds. Returns all items after (but not including) this cursor position. If after is specified, before must not be specified. + /// A Unix timestamp in milliseconds. Returns all items before (but not including) this cursor position. If before is specified, after must not be specified. + /// + /// AUTH NEEDED + public Task> GetUsersRecentlyPlayedTracksAsync(int limit = 20, DateTime? after = null, + DateTime? before = null) + { + return DownloadDataAsync>(_builder.GetUsersRecentlyPlayedTracks(limit, after, before)); + } + #endregion #region Playlists diff --git a/SpotifyAPI/Web/SpotifyWebBuilder.cs b/SpotifyAPI/Web/SpotifyWebBuilder.cs index e410badc..52c3ec5f 100644 --- a/SpotifyAPI/Web/SpotifyWebBuilder.cs +++ b/SpotifyAPI/Web/SpotifyWebBuilder.cs @@ -587,6 +587,26 @@ namespace SpotifyAPI.Web return builder.ToString(); } + /// + /// Get tracks from the current user’s recent play history. + /// + /// The maximum number of items to return. Default: 20. Minimum: 1. Maximum: 50. + /// A Unix timestamp in milliseconds. Returns all items after (but not including) this cursor position. If after is specified, before must not be specified. + /// A Unix timestamp in milliseconds. Returns all items before (but not including) this cursor position. If before is specified, after must not be specified. + /// + /// AUTH NEEDED + 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 #region Playlists diff --git a/SpotifyAPI/Web/Util.cs b/SpotifyAPI/Web/Util.cs index bd329255..23e0390b 100644 --- a/SpotifyAPI/Web/Util.cs +++ b/SpotifyAPI/Web/Util.cs @@ -22,6 +22,11 @@ namespace SpotifyAPI.Web attributes.ToList().ForEach(element => list.Add(element.Text)); 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