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 objectFromURI(self, uris): if self.logged_in() == False: return False uris = [uris] if type(uris) != list else uris uri_type = SpotifyUtil.get_uri_type(uris[0]) if uri_type == False: return None elif uri_type == "playlist": results = [SpotifyPlaylist(self, uri=uri) for uri in uris] elif uri_type in ["track", "album", "artist"]: uris = [uri for uri in uris if not SpotifyUtil.is_local(uri)] objs = self.api.metadata_request(uris) objs = [objs] if type(objs) != list else objs if uri_type == "track": results = [SpotifyTrack(self, obj=obj) for obj in objs] elif uri_type == "album": results = [SpotifyAlbum(self, obj=obj) for obj in objs] elif uri_type == "artist": results = [SpotifyArtist(self, obj=obj) for obj in objs] else: return None if len(results) == 1: return results[0] else: return results
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 objectFromURI(self, uris, asArray = False): if self.logged_in() == False: 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 uri_type == False: return None elif uri_type == "playlist": if len(uris) == 1: results = [SpotifyPlaylist(self, uri=uris[0])] 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): results[index] = SpotifyPlaylist(spotify, uri=uri) Spotify.doWorkerQueue(work_function, jobs) results = [v for k, v in thread_results.items()] elif uri_type in ["track", "album", "artist"]: uris = [uri for uri in uris if not SpotifyUtil.is_local(uri)] objs = self.api.metadata_request(uris) objs = [objs] if type(objs) != list else objs failed_requests = len([obj for obj in objs if obj == False]) if failed_requests > 0: print failed_requests,"metadata requests failed" objs = [obj for obj in objs if obj != False] if uri_type == "track": tracks = [SpotifyTrack(self, obj=obj) for obj in objs] results = [track for track in tracks if self.AUTOREPLACE_TRACKS == False or track.isAvailable()] elif uri_type == "album": results = [SpotifyAlbum(self, obj=obj) for obj in objs] elif uri_type == "artist": results = [SpotifyArtist(self, obj=obj) for obj in objs] else: return None if asArray == False: if len(results) == 1: results = results[0] elif len(results) == 0: return None return results
def album_callback(sp, album): print album.name + " - " + album.artist[0].name + "\n" uris = [ SpotifyUtil.gid2uri("track", track.gid) for track in album.disc[0].track ] sp.metadata_request(uris, track_callback)
def imagesFromArray(image_objs): images = {} for image_obj in image_objs: size = str(image_obj.width) images[size] = "https://d3rt1990lpmkn.cloudfront.net/" + size + "/" + SpotifyUtil.gid2id(image_obj.file_id) return images
def display_playlist(playlist): print playlist.attributes.name+"\n" track_uris = [track.uri for track in playlist.contents.items if SpotifyUtil.get_uri_type(track.uri) != "local"] tracks = sp.metadata_request(track_uris) for track in tracks: print track.name print "\n"
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 display_playlist(playlist): print playlist.attributes.name+"\n" if playlist.length > 0: track_uris = [track.uri for track in playlist.contents.items if not SpotifyUtil.is_local(track.uri)] tracks = sp.metadata_request(track_uris) for track in tracks: print track.name else: print "no tracks" print "\n"
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): new_obj = self.spotify.api.recurse_alternatives(self.obj) if new_obj == False: return False else: # invalidate cache self._Cache__cache = {} if new_obj.HasField("name") == False: 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 album_callback(sp, album): print album.name+" - "+album.artist[0].name+"\n" uris = [SpotifyUtil.gid2uri("track", track.gid) for track in album.disc[0].track] sp.metadata_request(uris, track_callback)
def getURI(self): return SpotifyUtil.gid2uri(self.uri_type, self.obj.gid)
def getID(self): return SpotifyUtil.gid2id(self.obj.gid)
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 update(self, node=None): self.uri = SpotifyUtil.id2uri('track', self.id)
print "\n" if action == "track": uri = sys.argv[4] if len(sys.argv) > 4 else "spotify:track:3IKSCoHEblCE60IKr4SVNd" track = sp.metadata_request(uri) print track.name elif action == "album": uri = sys.argv[4] if len(sys.argv) > 4 else "spotify:album:3OmHoatMS34vM7ZKb4WCY3" album = sp.metadata_request(uri) print album.name+" - "+album.artist[0].name+"\n" uris = [SpotifyUtil.gid2uri("track", track.gid) for track in album.disc[0].track] tracks = sp.metadata_request(uris) for track in tracks: print track.name elif action == "playlists": username = sys.argv[4] if len(sys.argv) > 4 else sp.username playlist_uris = [playlist.uri for playlist in sp.playlists_request(username).contents.items] playlists = [sp.playlist_request (playlist_uri) for playlist_uri in playlist_uris] for playlist in playlists: display_playlist(playlist) elif action == "playlist": uri = sys.argv[4] if len(sys.argv) > 4 else "spotify:user:topsify:playlist:1QM1qz09ZzsAPiXphF1l4S"
print "\n" if action == "track": uri = sys.argv[4] if len(sys.argv) > 4 else "spotify:track:3IKSCoHEblCE60IKr4SVNd" track = sp.metadata_request(uri) print track.name elif action == "album": uri = sys.argv[4] if len(sys.argv) > 4 else "spotify:album:3OmHoatMS34vM7ZKb4WCY3" album = sp.metadata_request(uri) print album.name+" - "+album.artist[0].name+"\n" uris = [SpotifyUtil.gid2uri("track", track.gid) for track in album.disc[0].track] tracks = sp.metadata_request(uris) for track in tracks: print track.name elif action == "playlists": username = sys.argv[4] if len(sys.argv) > 4 else sp.username playlist_uris = [playlist.uri for playlist in sp.playlists_request(username).contents.items] playlists = [sp.playlist_request(playlist_uri) for playlist_uri in playlist_uris] for playlist in playlists: display_playlist(playlist) elif action == "playlist": uri = sys.argv[4] if len(sys.argv) > 4 else "spotify:user:topsify:playlist:1QM1qz09ZzsAPiXphF1l4S"