Skip to content

Commit

Permalink
Added support for My Top Tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
plamere committed Mar 13, 2016
1 parent 22bb4a2 commit 2570fc2
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
41 changes: 40 additions & 1 deletion server/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
{ "name" : "all", "value" : 4 },
],

"time_range": [
{ "name": "short term", "value": "short_term" },
{ "name": "medium term", "value": "medium_term" },
{ "name": "long term", "value": "long_term" }
],

"range_attributes" : [
{
"name" : "artist popularity",
Expand Down Expand Up @@ -981,7 +987,10 @@
"description": "produces a list of tracks from the current user's saved albums",

"help" : """ This component will generate a stream of tracks from the
current user's saved albums
current user's saved albums.
<br>
<span class="label label-warning"> Warning </span> This component may fail if you have a large number
of saved albums. Someday, this will be fixed.
""",
"title": "My Saved albums",
"params": { }
Expand All @@ -995,10 +1004,40 @@

"help" : """ This component will generate a stream of tracks from the
current user's saved tracks
<br>
<span class="label label-warning"> Warning </span> This component may fail if you have a large number
of saved tracks. Someday, this will be fixed.
""",
"title": "My Saved tracks",
"params": { }
},
{
"name" : "MyTopTracks",
"class": plugs.MyTopTracks,
"type" : "source",
"display": "my top tracks",
"description": "produces a list of the current user's recent top tracks",

"help" : """ This component will generate a stream of tracks from the
current user's recent top tracks. As a user's behavior is likely to
shift over time, this the top tracks are available over three time
spans:
<ul>
<li><b>short term</b> - the last month or so </li>
<li><b>medium term</b> - the last half year or so </li>
<li><b>long term</b> - the last several years </li>
</ul>
""",
"title": "My $time_range Top tracks",
"params": {
"time_range" : {
"type": "time_range",
"optional": True,
"default": "medium_term",
"description": "the time period of interest"
}
}
},
{
"name" : "MyFollowedArtists",
"class": plugs.MyFollowedArtists,
Expand Down
52 changes: 51 additions & 1 deletion server/plugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,52 @@ def next_track(self):
else:
return None

class MyTopTracks(object):
''' returns the your top tracks for a given perioed
:param time_range time_range - Over what time frame are the tracks are
returned Valid-values: short_term, medium_term, long_term
'''
def __init__(self, time_range):
self.name = 'My Top Tracks'
self.time_range = time_range
self.buffer = None

def next_track(self):
if self.buffer == None:
self.buffer = []
try:
sp = get_spotify()
limit = 50
offset = 0
total = 50

while offset < total:
results = sp.current_user_top_tracks(time_range = self.time_range,
limit = limit, offset = offset)
for item in results['items']:
track = item
if track and 'id' in track:
self.buffer.append(track['id'])
spotify_plugs._add_track(self.name, track)
else:
raise pbl.engine.PBLException(self, 'bad track')
offset += limit
total = results['total']
# print self.name, len(self.buffer), offset, total
except spotipy.SpotifyException as e:
raise pbl.engine.PBLException(self, e.msg)

if len(self.buffer) > 0:
tid = self.buffer.pop(0)
# print 'ret', self.name, tid
return tid
else:
# print 'ret', self.name, 'empty'
return None



def now():
return datetime.datetime.now()
Expand Down Expand Up @@ -1360,7 +1406,7 @@ def parse_date(sdate):
print src.name
pbl.show_source(src)

if True:
if False:
print 'std'
src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
pbl.show_source(src)
Expand All @@ -1380,3 +1426,7 @@ def parse_date(sdate):
src = pbl.PlaylistSource('trap music', uri = 'spotify:user:spotify:playlist:4Ha7Qja6HY3AgvNBgWz87p')
src = TextFilter(src, '-', True, False)
pbl.show_source(src)

if True:
src = MyTopTracks(time_range='short_term')
pbl.show_source(src)
2 changes: 2 additions & 0 deletions web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ function loginWithSpotifyForAuth() {
scopes += " playlist-modify-public playlist-modify-private"
scopes += " user-library-read"
scopes += " user-follow-read"
scopes += " user-follow-read"
scopes += " user-top-read"

var url = 'https://accounts.spotify.com/authorize?client_id=' + client_id +
'&response_type=code&show_dialog=false' +
Expand Down

0 comments on commit 2570fc2

Please sign in to comment.