def album_metadata_callback(sp, album): print album.name+"\n" uris = [] for track in album.disc[0].track: uris.append(SpotifyUtil.gid2uri("track", track.gid)) sp.track_uri(SpotifyUtil.gid2id(track.gid), "mp3160", track_uri_callback) sp.metadata_request(uris, multi_track_metadata_callback)
def track_uri_callback(sp, result): global currentLid, currentTracks, currentTrackI, currentPlaylistId if "type" in result and result["type"] == 3: Logging.notice("Track is not available. Skipping.") do_next_queue(sp) return #We need to "end" the previous track if currentTrackI > 0: #Get the last track currentTrack = currentTracks[currentTrackI - 1] trackUri = SpotifyUtil.id2uri("track", SpotifyUtil.gid2id(currentTrack.gid)) sp.track_end(currentLid, 0, 97, trackUri, sp.username, currentPlaylistId, None) currentTrack = currentTracks[currentTrackI] trackUri = SpotifyUtil.id2uri("track", SpotifyUtil.gid2id(currentTrack.gid)) lid = result["lid"] url = result["uri"] currentLid = lid print "Got URL for: " + currentTrack.name + " - " + currentTrack.artist[0].name sp.track_event(lid, 3, 0, None) sp.track_progress(lid, 500, 97, sp.username, currentPlaylistId, trackUri, None) sp.track_event(lid, 4, 500, None) sp.send_command("sp/echo", ["h"]) sp.send_command("sp/log", [30, 1, "heartbeat", 77, 77, 2, False]) time.sleep(1) do_next_queue(sp)
def objectFromID(self, object_type, ids): try: uris = [SpotifyUtil.id2uri(object_type, id) for id in ids] except: uris = SpotifyUtil.id2uri(object_type, ids) return self.objectFromURI(uris, asArray=True)
def objectFromInternalObj(self, object_type, objs, nameOnly=False): if nameOnly: return ", ".join([obj.name for obj in objs]) try: uris = [SpotifyUtil.gid2uri(object_type, obj.gid) for obj in objs] except: uris = SpotifyUtil.gid2uri(object_type, objs.gid) return self.objectFromURI(uris, asArray=True)
def do_next_queue(sp): global currentTracks, currentTrackI currentTrackI += 1 if currentTrackI >= len(currentTracks): print("Done!") return track = currentTracks[currentTrackI] trackId = SpotifyUtil.gid2id(track.gid) trackUri = SpotifyUtil.id2uri("track", trackId) sp.track_uri(trackId, "mp3160", track_uri_callback)
def album_callback(sp, album): print album.name+"\n" uris = [] for track in album.disc[0].track: uris.append(SpotifyUtil.gid2uri("track", track.gid)) sp.metadata_request(uris, track_callback)
def getImages(self): if self.obj != None and self.obj.attributes.picture != None: images = {} size = 300 image_url = Spotify.imageFromId(SpotifyUtil.gid2id(self.obj.attributes.picture), size) if image_url != None: images[size] = image_url return images return None
def playlist_callback(sp, playlist): print playlist.attributes.name+"\n" uris = [] for track in playlist.contents.items: if SpotifyUtil.get_uri_type(track.uri) != "track": continue uris.append(track.uri) print len(uris) sp.metadata_request(uris, multi_track_metadata_callback)
def get(self, uri): cache = { "track": self.track_cache, "album": self.album_cache, "artist": self.artist_cache, } uri_type = SpotifyUtil.get_uri_type(uri) if uri_type not in cache: return False
def getImages(self): if self.obj != None and self.obj.attributes.picture != None: images = {} size = 300 image_url = Spotify.imageFromId( SpotifyUtil.gid2id(self.obj.attributes.picture), size) if image_url != None: images[size] = image_url return images return None
def removeTracks(self, tracks): tracks = [tracks] if type(tracks) != list else tracks uris = [] for track in tracks: if track.replaced: uris.append(SpotifyUtil.gid2uri("track", track.old_obj.gid)) else: uris.append(self.getURI()) self.spotify.api.playlist_remove_track(self.getURI(), uris) self.reload_refs()
def isAvailable(self, country=None): country = self.spotify.api.country if country is None else country new_obj = self.spotify.api.recurse_alternatives(self.obj, country=country) if not new_obj: return False else: # invalidate cache self._Cache__cache = {} if not new_obj.HasField("name"): new_obj = self.spotify.api.metadata_request(SpotifyUtil.gid2uri("track", new_obj.gid)) self.old_obj = self.obj self.obj = new_obj self.replaced = True return True
def isAvailable(self, country=None): country = self.spotify.api.country if country is None else country new_obj = self.spotify.api.recurse_alternatives(self.obj, country=country) if not new_obj: return False else: # invalidate cache self._Cache__cache = {} if not new_obj.HasField("name"): new_obj = self.spotify.api.metadata_request( SpotifyUtil.gid2uri("track", new_obj.gid)) self.old_obj = self.obj self.obj = new_obj self.replaced = True return True
def imagesFromArray(image_objs, must_convert_to_id=True): images = {} for image_obj in image_objs: size = image_obj.width if size <= 60: size = 60 elif size <= 160: size = 160 elif size <= 300: size = 300 elif size <= 320: size = 320 elif size <= 640: size = 640 image_id = SpotifyUtil.gid2id(image_obj.file_id) if must_convert_to_id else image_obj.file_id image_url = Spotify.imageFromId(image_id, size) if image_url != None: images[size] = image_url return images
def imagesFromArray(image_objs, must_convert_to_id=True): images = {} for image_obj in image_objs: size = image_obj.width if size <= 60: size = 60 elif size <= 160: size = 160 elif size <= 300: size = 300 elif size <= 320: size = 320 elif size <= 640: size = 640 image_id = SpotifyUtil.gid2id( image_obj.file_id) if must_convert_to_id else image_obj.file_id image_url = Spotify.imageFromId(image_id, size) if image_url != None: images[size] = image_url return images
def getURI(self): return SpotifyUtil.gid2uri(self.uri_type, self.obj.gid)
def getID(self): return SpotifyUtil.gid2id(self.obj.gid)
def objectFromURI(self, uris, asArray=False): with self.global_lock: if not self.logged_in(): return False uris = [uris] if type(uris) != list else uris if len(uris) == 0: return [] if asArray else None uri_type = SpotifyUtil.get_uri_type(uris[0]) if not uri_type: return [] if asArray else None elif uri_type == "playlist": if len(uris) == 1: obj = self.api.playlist_request(uris[0]) results = [SpotifyPlaylist(self, uri=uris[0], obj=obj)] if False != obj else [] else: thread_results = {} jobs = [] for index in range(0, len(uris)): jobs.append((self, uris[index], thread_results, index)) def work_function(spotify, uri, results, index): obj = self.api.playlist_request(uri) if False != obj: results[index] = SpotifyPlaylist(self, uri=uri, obj=obj) Spotify.doWorkerQueue(work_function, jobs) results = [v for k, v in thread_results.items()] elif uri_type in ["track", "album", "artist"]: results = [] uris = [uri for uri in uris if not SpotifyUtil.is_local(uri)] start = 0 finish = 100 uris_to_ask = uris[start:finish] while len(uris_to_ask) > 0: objs = self.api.metadata_request(uris_to_ask) objs = [objs] if type(objs) != list else objs failed_requests = len([obj for obj in objs if obj == False or obj == None]) if failed_requests > 0: print failed_requests, "metadata requests failed" objs = [obj for obj in objs if obj != False and obj != None] if uri_type == "track": tracks = [SpotifyTrack(self, obj=obj) for obj in objs] results.extend([track for track in tracks if False == self.AUTOREPLACE_TRACKS or track.isAvailable()]) elif uri_type == "album": results.extend([SpotifyAlbum(self, obj=obj) for obj in objs]) elif uri_type == "artist": results.extend([SpotifyArtist(self, obj=obj) for obj in objs]) start = finish finish = finish + 100 uris_to_ask = uris[start:finish] else: return [] if asArray else None if not asArray: if len(results) == 1: results = results[0] elif len(results) == 0: return [] if asArray else None return results
def update(self, node=None): self.uri = SpotifyUtil.id2uri('track', self.id)
def update(self, node=None): self.uri = SpotifyUtil.id2uri('artist', self.id)
def update(self, node=None): self.uri = SpotifyUtil.id2uri('album', self.id) if node: self.update_covers(node)
def getAlbumURI(self): return SpotifyUtil.gid2uri('album', self.obj.album.gid)
def is_track_uri_valid(self, track_uri): return SpotifyUtil.is_track_uri_valid(track_uri)
def objectFromURI(self, uris, asArray=False): with self.global_lock: if not self.logged_in(): return False uris = [uris] if type(uris) != list else uris if len(uris) == 0: return [] if asArray else None uri_type = SpotifyUtil.get_uri_type(uris[0]) if not uri_type: return [] if asArray else None elif uri_type == "playlist": if len(uris) == 1: obj = self.api.playlist_request(uris[0]) results = [SpotifyPlaylist(self, uri=uris[0], obj=obj) ] if False != obj else [] else: thread_results = {} jobs = [] for index in range(0, len(uris)): jobs.append((self, uris[index], thread_results, index)) def work_function(spotify, uri, results, index): obj = self.api.playlist_request(uri) if False != obj: results[index] = SpotifyPlaylist(self, uri=uri, obj=obj) Spotify.doWorkerQueue(work_function, jobs) results = [v for k, v in thread_results.items()] elif uri_type in ["track", "album", "artist"]: results = [] uris = [uri for uri in uris if not SpotifyUtil.is_local(uri)] start = 0 finish = 100 uris_to_ask = uris[start:finish] while len(uris_to_ask) > 0: objs = self.api.metadata_request(uris_to_ask) objs = [objs] if type(objs) != list else objs failed_requests = len( [obj for obj in objs if obj == False or obj == None]) if failed_requests > 0: print failed_requests, "metadata requests failed" objs = [ obj for obj in objs if obj != False and obj != None ] if uri_type == "track": tracks = [SpotifyTrack(self, obj=obj) for obj in objs] results.extend([ track for track in tracks if False == self.AUTOREPLACE_TRACKS or track.isAvailable() ]) elif uri_type == "album": results.extend( [SpotifyAlbum(self, obj=obj) for obj in objs]) elif uri_type == "artist": results.extend( [SpotifyArtist(self, obj=obj) for obj in objs]) start = finish finish = finish + 100 uris_to_ask = uris[start:finish] else: return [] if asArray else None if not asArray: if len(results) == 1: results = results[0] elif len(results) == 0: return [] if asArray else None return results