Skip to content

Commit

Permalink
Added Spotify artist radio
Browse files Browse the repository at this point in the history
  • Loading branch information
plamere committed Sep 17, 2017
1 parent dd8395f commit 84a57d9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ server {


Get spotipy, pyen and pbl from git, install
sudo pip install werkzeug
sudo pip install simplejson

(disabled leveldb in pbl to get it to work easily)
install requests security to avoid SSL warning
Expand Down
32 changes: 30 additions & 2 deletions server/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@
{
"name" : "EchoNestArtistRadio",
"class": pbl.EchoNestArtistRadio,
'disabled': True,
"type" : "source",
"display": "artist radio",

Expand All @@ -780,6 +781,26 @@
},
}
},
{
"name" : "SpotifyArtistRadio",
"class": plugs.SpotifyArtistRadio,
"type" : "source",
"display": "artist radio",

"description": "tracks by the given artist and similar artists",

"help" : """ This component generates a stream of tracks by the
given artist or similar artists """,

"title" : "$artist radio",
"params": {
"artist": {
"type" : "string",
"optional" : False,
"description": "the seed artist (as a name or artist uri)",
},
}
},
{
"name" : "EchoNestArtist",
"class": pbl.EchoNestArtistPlaylist,
Expand Down Expand Up @@ -2023,8 +2044,14 @@
def export_inventory():
inventory['types']['genre'] = get_genres()
inv = copy.deepcopy(inventory)

enabled_components = []
for component in inv['components']:
if 'disabled' in component and component['disabled']:
continue
del component['class']
enabled_components.append(component)
inv['components'] = enabled_components
return inv

def get_genres():
Expand Down Expand Up @@ -2111,7 +2138,8 @@ def check_component(comp):
check_components()

if __name__ == '__main__':
#import json
import json
#get_genres()
#print json.dumps(exported_inventory, indent=4)
print json.dumps(exported_inventory, indent=4)
print ""

58 changes: 54 additions & 4 deletions server/plugs.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ def get_user():
def get_spotify():
return pbl.spotify_plugs._get_spotify()


def get_artist_uri(artist_name):
sp = get_spotify()
if sp:
results = sp.search(artist_name, limit=5, type='artist')
if 'artists' in results and 'items' in results['artists'] and len(results['artists']['items']) > 0:
return results['artists']['items'][0]['uri']
else:
return None

class PlaylistSave(object):
''' A PBL Sink that saves the source stream of tracks to the given playlist
:param source: the source of tracks to be saved
Expand Down Expand Up @@ -895,7 +905,7 @@ def _get_uri_from_name_and_user(self, name, user):
results = get_spotify().user_playlists(user)
while results:
for playlist in results['items']:
if playlist['name'].lower() == name.lower():
if 'name' in playlist and playlist['name'] and playlist['name'].lower() == name.lower():
return playlist['uri']
if results['next']:
results = get_spotify().next(results)
Expand Down Expand Up @@ -1287,7 +1297,49 @@ def next_track(self):
# print 'ret', self.name, 'empty'
return None

class SpotifyArtistRadio(object):
''' returns tracks given a seed artist
:param seed_artist_name_or_uri the name or uri of the seed artist
'''
def __init__(self, seed_artist_name_or_uri):
self.name = 'Artist Radio'
self.seed_artist_name_or_uri = seed_artist_name_or_uri
self.buffer = None

def next_track(self):
if self.buffer == None:
self.buffer = []
try:
sp = get_spotify()

if is_uri(self.seed_artist_name_or_uri):
seed_uri = self.seed_artist_name_or_uri
else:
seed_uri = get_artist_uri(self.seed_artist_name_or_uri)

if seed_uri:
results = sp.recommendations(seed_artists=[seed_uri], limit=100)
for track in results['tracks']:
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')
except spotipy.SpotifyException as e:
raise pbl.engine.PBLException(self, e.msg)

if len(self.buffer) > 0:
tid = self.buffer.pop(0)
return tid
else:
return None


def is_uri(s):
fields = s.split(':')
return len(fields) >= 3 and fields[0] == 'spotify'

def now():
return datetime.datetime.now()
Expand All @@ -1305,13 +1357,11 @@ def parse_date(sdate):
return -1




if __name__ == '__main__':


import sys
if False:
if True:
p1 = pbl.ArtistTopTracks(name='Ravenscry')
#p2 = pbl.ArtistTopTracks(name='weezer')
#mi = MixIn(p1, p2, 2,1,1, True)
Expand Down

0 comments on commit 84a57d9

Please sign in to comment.