class App(object): uris = [ # "Holes" - "Passenger" 'spotify:track:6xYcv63zDYiEaJ1KGgysGN', 'spotify:track:77w4HJEAGzRwHTapyXjFl1', 'spotify:track:3sgebd31wwbZY8Uyda3yOC', 'spotify:track:7q3FEfKhqCF3w6Q8uTfDuH', 'spotify:track:2n9TFOPyTnN0uYdzscNA4g', 'spotify:track:08du7WH5gMBaCB9TlIpOiI', 'spotify:track:2xuw7EWGdn5nYWezDx41xu', # "Robot Rock / Oh Yeah" - "Daft Punk" 'spotify:track:5EaZLVO6pSEWIyUDsHWiSU', 'spotify:track:0q9zz6nP5izcUnfYndfVX6', 'spotify:track:5eS6pTvDNOvh2kyxeZtK3r', # "Room 24" - "Volbeat" 'spotify:track:4OugXGf671yxhci3eqVGJf', 'spotify:track:0B3ALwX9rlqegfSLoFnx4E', 'spotify:track:5DRP9X5EivS5XklFqqu9Gh' ] def __init__(self): self.sp = Spotify() def run(self): @self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD']) def on_login(): self.sp.user_info['country'] = 'NL' self.sp.metadata(self.uris, self.on_tracks) def on_tracks(self, tracks): results = [] for track in tracks: uri = track.uri if not track.is_available(): track.find_alternative() results.append((str(uri), track)) for uri, track in results: print '[%s] "%s" - "%s"' % (uri, track.name, track.artists[0].name) print '\turi:', str(track.uri) print '\tis_available:', track.is_available() for restriction in track.restrictions: print '\tallowed:', ', '.join(restriction.countries_allowed or []) print '\tforbidden:', ', '.join(restriction.countries_forbidden or []) print
def __init__(self, port=12555): self.port = port self.sp = Spotify() self.cache = {} self.current = None self.on_login = Event()
class App(object): def __init__(self): self.sp = Spotify() def run(self): @self.sp.login(os.environ["USERNAME"], os.environ["PASSWORD"]) def on_login(): self.sp.search("daft punk", count=7, callback=self.on_search) def on_search(self, result): # Artists print "artists (%s)" % result.artists_total for artist in result.artists: print '\t[%s] "%s"' % (artist.uri, artist.name) if not artist.portraits: continue for portrait in artist.portraits: print "\t\t", portrait.file_url # Albums print "albums (%s)" % result.albums_total for album in result.albums: print '\t[%s] "%s" - %s' % (album.uri, album.name, ", ".join([ar.name for ar in album.artists])) if not album.covers: continue for cover in album.covers: print "\t\t", cover.file_url # Tracks print "tracks (%s)" % result.tracks_total for track in result.tracks: print '\t[%s] "%s" - %s' % (track.uri, track.name, ", ".join([ar.name for ar in track.artists if ar.name])) # Playlists print "playlists (%s)" % result.playlists_total for playlist in result.playlists: print '\t[%s] "%s"' % (playlist.uri, playlist.name)
def __init__(self): self.sp = Spotify() self.track_uris = None self.tracks = None self.album = None self.request_num = 0
class App(object): def __init__(self): self.sp = Spotify() def run(self): @self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD']) def on_login(): self.sp.user.playlists(callback=self.on_playlists) self.sp.playlist('spotify:user:fuzeman:playlist:4AMR1hLgWrMbZkMneOqUw3', callback=self.on_playlist) def on_playlists(self, playlists): print "=" * 25 print 'length:', playlists.length print 'position:', playlists.position print 'truncated:', playlists.truncated print 'items:' for item in playlists.fetch(): print '\t"%s" (%s)' % (item.name, item.uri) print "=" * 25 def on_playlist(self, playlist): if playlist is None: print "Unable to retrieve playlist" return print playlist.name print "-" * 25 print 'length:', playlist.length print 'position:', playlist.position print 'truncated:', playlist.truncated print 'items:' for item in playlist.fetch(): print '\t"%s" (%s)' % (item.name, item.uri)
class App(object): artist_uri = 'spotify:artist:4tZwfgrHOc3mvqYlEYSvVi' # Daft Punk def __init__(self): self.sp = Spotify() self.tracks = None self.album = None def run(self): @self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD']) def on_login(): # Fetch metadata for album self.sp.metadata(self.artist_uri, self.on_artist) def on_artist(self, artist): self.sp.metadata(artist.albums[0].uri, self.on_album) def on_album(self, album): print album.name self.sp.metadata([tr.uri for tr in album.tracks], self.on_tracks) def on_tracks(self, tracks): for track in tracks: print '\t[%s] - %s' % (track.uri, track.name)
class App(object): def __init__(self): self.sp = Spotify() def run(self): @self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD']) def on_login(): self.sp.search('daft punk', count=20, callback=self.on_search) def on_search(self, result): # Artists if result.artists: print 'artists (%s)' % len(result.artists) def on_artists(artists): for artist in artists: print '\t[%s] "%s"' % (artist.uri, artist.name) self.sp.metadata([ar.uri for ar in result.artists], on_artists) # Albums if result.albums: print 'albums (%s)' % len(result.albums) def on_albums(albums): for album in albums: print '\t[%s] "%s" - %s' % (album.uri, album.name, ', '.join([ar.name for ar in album.artists])) self.sp.metadata([al.uri for al in result.albums], on_albums) # Tracks if result.tracks: print 'tracks (%s)' % len(result.tracks) def on_tracks(tracks): for track in tracks: print '\t[%s] "%s" - %s' % (track.uri, track.name, ', '.join([ar.name for ar in track.artists if ar.name])) self.sp.metadata([tr.uri for tr in result.tracks], on_tracks) # Playlists if result.playlists: print 'playlists (%s)' % len(result.playlists) for playlist in result.playlists: print '\t[%s] "%s"' % (playlist.uri, playlist.name)
class App(object): #album_uri = 'spotify:album:7u6zL7kqpgLPISZYXNTgYk' # Alive 2007 album_uri = 'spotify:album:1x4SGGPflZamzny9QXRsdi' # Tourist History def __init__(self): self.sp = Spotify() self.track_uris = None self.tracks = None self.album = None self.request_num = 0 def run(self): @self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD']) def on_login(): # Fetch metadata for album self.sp.metadata(self.album_uri, self.on_album) def on_album(self, album): self.album = album # Request track metadata self.track_uris = [tr.uri for tr in self.album.tracks] self.sp.metadata([ self.track_uris[0], self.track_uris[5], self.track_uris[3] ], self.on_tracks) def on_tracks(self, tracks): self.request_num += 1 self.tracks = tracks log.info('%s - %s', self.album.name, ', '.join([artist.name for artist in self.album.artists])) for track in self.tracks: #if not track.is_available(): # track.find_alternative() log.info('\t[%02d] (%s) %s', track.number, track.uri, track.name) if self.request_num >= 2: return self.sp.metadata([ self.track_uris[0], self.track_uris[6], self.track_uris[3], self.track_uris[4], self.track_uris[5] ], self.on_tracks)
def __init__(self): self.sp = Spotify()
class Server(object): def __init__(self, port=12555): self.port = port self.sp = Spotify() self.cache = {} self.current = None self.on_login = Event() def start(self): # Spotify self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD'], lambda: self.on_login.set()) # CherryPy cherrypy.config.update({ 'server.socket_host': '0.0.0.0', 'server.socket_port': self.port }) cherrypy.tree.mount(None, config={ '/': { 'request.dispatch': Dispatcher(self) } }) cherrypy.engine.start() def album(self, uri): self.on_login.wait() # Wait for login complete = Event() album_tracks = [] # Fetch album metadata @self.sp.metadata(uri) def on_album(album): # Fetch metadata for each track @self.sp.metadata([tr.uri for tr in album.discs[0].tracks]) def on_tracks(tracks): # Append onto 'tracks' list album_tracks.extend(tracks) # Work complete complete.set() # Wait until tracks are loaded (give up after 5 seconds) complete.wait(5) # Render template return env.get_template('album.html').render( tracks=[ {'name': track.name, 'uri': str(track.uri)} for track in album_tracks ] ) def track(self, uri): self.on_login.wait() # Wait for login # Update current if self.current: # Track changed, call finish() if uri != self.current.uri: log.debug('Changing tracks, calling finish() on previous track') self.current.finish() # Create new TrackReference (if one doesn't exist yet) if uri not in self.cache: log.debug('[%s] Creating new TrackReference' % uri) # Create new track reference self.cache[uri] = TrackReference(self, uri) # Get track reference from cache tr = self.cache[uri] # Start download tr.fetch() # Wait until track is ready tr.on_opened.wait(10) if not tr.success: self.current = None cherrypy.response.status = 500 return # Update current self.current = tr # Update headers cherrypy.response.headers['Accept-Ranges'] = 'bytes' cherrypy.response.headers['Content-Type'] = tr.response_headers.getheader('Content-Type') cherrypy.response.headers['Content-Length'] = tr.response_headers.getheader('Content-Length') # Progressively return track from buffer return self.stream(tr, 0, cherrypy.response.headers['Content-Length']) track._cp_config = {'response.stream': True} @staticmethod def stream(tr, r_start, r_end): position = r_start chunk_size_min = 6 * 1024 chunk_size_max = 10 * 1024 chunk_scale = 0 chunk_size = chunk_size_min last_progress = None while True: # Adjust chunk_size if chunk_scale < 1: chunk_scale = 2 * (float(position) / tr.stream_length) chunk_size = int(chunk_size_min + (chunk_size_max * chunk_scale)) if chunk_scale > 1: chunk_scale = 1 if position + chunk_size > r_end: chunk_size = r_end - position # Read chunk chunk = tr.read(position, chunk_size) if not chunk: log.debug('[%s] Finished at %s bytes (content-length: %s)' % (tr.uri, position, tr.stream_length)) break last_progress = log_progress(tr, ' Streaming', position, last_progress) position = position + len(chunk) # Write chunk yield chunk log.debug('[%s] Stream Complete', tr.uri) def get_track_url(self, uri): return "http://%s:%d/track/%s.mp3" % ( socket.gethostname(), self.port, uri )
def __init__(self): self.sp = Spotify() self.tracks = None self.album = None
def __init__(self): self.sp = Spotify() self.logged_in = False self.return_only = ["tracks"]
class App(object): def __init__(self): self.sp = Spotify() self.logged_in = False self.return_only = ["tracks"] def run(self, uname, pword): # @self.sp.login(os.environ['USERNAME'], os.environ['PASSWORD']) @self.sp.login(uname, pword) def on_login(): self.sp.search('daft punk', count=7, callback=self.on_search) self.logged_in = True def do_search(self, sterm): global searching searching = True try: str_sterm = str(sterm) except UnicodeEncodeError as _uee: str_sterm = "".join([c for c in sterm if curses.ascii.isascii(c)]) str_sterm = str(str_sterm) assert isinstance(str_sterm, str) self.sp.search(str_sterm, count=1, callback=self.on_search) def on_search(self, result): global searching # Artists if not self.return_only or "artists" in self.return_only: print 'artists (%s)' % result.artists_total for artist in result.artists: print '\t[%s] "%s"' % (artist.uri, artist.name) if not artist.portraits: continue for portrait in artist.portraits: print '\t\t', portrait.file_url # Albums if not self.return_only or "albums" in self.return_only: print 'albums (%s)' % result.albums_total for album in result.albums: print '\t[%s] "%s" - %s' % (album.uri, album.name, ', '.join([ar.name for ar in album.artists])) if not album.covers: continue for cover in album.covers: print '\t\t', cover.file_url # Tracks if not self.return_only or "tracks" in self.return_only: #print 'tracks (%s)' % result.tracks_total for track in result.tracks: print '\t%s [%s] "%s" - %s' % (uri_to_url(track.uri), track.uri, track.name, ', '.join([ar.name for ar in track.artists if ar.name])) # Playlists if not self.return_only or "playlists" in self.return_only: print 'playlists (%s)' % result.playlists_total for playlist in result.playlists: print '\t[%s] "%s"' % (playlist.uri, playlist.name) searching = False