Support after/before in current_user_recently_played

This commit is contained in:
Wil Collins 2020-01-26 08:50:45 -05:00 committed by Stéphane Bruckert
parent 349587ca32
commit ff7c33e97d
3 changed files with 32 additions and 2 deletions

View File

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- Support for `playlist_cover_image` - Support for `playlist_cover_image`
- Support `after` and `before` parameter in `current_user_recently_played`
## [2.7.1] - 2020-01-20 ## [2.7.1] - 2020-01-20

View File

@ -759,13 +759,21 @@ class Spotify(object):
return self._get('me/top/tracks', time_range=time_range, limit=limit, return self._get('me/top/tracks', time_range=time_range, limit=limit,
offset=offset) offset=offset)
def current_user_recently_played(self, limit=50): def current_user_recently_played(self, limit=50, after=None,
before=None):
''' Get the current user's recently played tracks ''' Get the current user's recently played tracks
Parameters: Parameters:
- limit - the number of entities to return - limit - the number of entities to return
- after - unix timestamp in milliseconds. Returns all items
after (but not including) this cursor position.
Cannot be used if before is specified.
- before - unix timestamp in milliseconds. Returns all items
before (but not including) this cursor position.
Cannot be used if after is specified
''' '''
return self._get('me/player/recently-played', limit=limit) return self._get('me/player/recently-played', limit=limit, after=after,
before=before)
def current_user_saved_albums(self, limit=20, offset=0): def current_user_saved_albums(self, limit=20, offset=0):
""" Gets a list of the albums saved in the current authorized user's """ Gets a list of the albums saved in the current authorized user's

View File

@ -25,6 +25,7 @@ import sys
import unittest import unittest
import warnings import warnings
import requests import requests
from pprint import pprint # noqa
sys.path.insert(0, os.path.abspath(os.pardir)) sys.path.insert(0, os.path.abspath(os.pardir))
@ -88,6 +89,7 @@ class AuthTestSpotipy(unittest.TestCase):
'user-read-private ' 'user-read-private '
'user-top-read ' 'user-top-read '
'user-follow-modify ' 'user-follow-modify '
'user-read-recently-played '
'ugc-image-upload' 'ugc-image-upload'
) )
@ -241,6 +243,25 @@ class AuthTestSpotipy(unittest.TestCase):
items = response['items'] items = response['items']
self.assertTrue(len(items) > 0) self.assertTrue(len(items) > 0)
def test_current_user_recently_played(self):
# No cursor
res = self.spotify.current_user_recently_played()
self.assertTrue(len(res['items']) <= 50)
played_at = res['items'][0]['played_at']
# Using `before` gives tracks played before
res = self.spotify.current_user_recently_played(
before=res['cursors']['after'])
self.assertTrue(len(res['items']) <= 50)
self.assertTrue(res['items'][0]['played_at'] < played_at)
played_at = res['items'][0]['played_at']
# Using `after` gives tracks played after
res = self.spotify.current_user_recently_played(
after=res['cursors']['before'])
self.assertTrue(len(res['items']) <= 50)
self.assertTrue(res['items'][0]['played_at'] > played_at)
def test_user_playlist_ops(self): def test_user_playlist_ops(self):
sp = self.spotify sp = self.spotify
# create empty playlist # create empty playlist