Beispiel #1
0
    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 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
Beispiel #3
0
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)
Beispiel #4
0
    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 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