Exemple #1
0
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)
Exemple #2
0
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)
Exemple #3
0
    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 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)
Exemple #5
0
    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 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)
Exemple #7
0
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)
Exemple #8
0
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)
Exemple #9
0
 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
Exemple #10
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)
    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
Exemple #13
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 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()
Exemple #15
0
    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()
Exemple #16
0
    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
Exemple #18
0
    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
Exemple #20
0
 def getURI(self):
     return SpotifyUtil.gid2uri(self.uri_type, self.obj.gid)
Exemple #21
0
 def getID(self):
     return SpotifyUtil.gid2id(self.obj.gid)
Exemple #22
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
Exemple #23
0
 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 getURI(self):
     return SpotifyUtil.gid2uri(self.uri_type, self.obj.gid)
 def getAlbumURI(self):
     return SpotifyUtil.gid2uri('album', self.obj.album.gid)
Exemple #28
0
 def getAlbumURI(self):
     return SpotifyUtil.gid2uri('album', self.obj.album.gid)
 def getID(self):
     return SpotifyUtil.gid2id(self.obj.gid)
Exemple #30
0
    def update(self, node=None):
        self.uri = SpotifyUtil.id2uri('album', self.id)

        if node:
            self.update_covers(node)
 def is_track_uri_valid(self, track_uri):
     return SpotifyUtil.is_track_uri_valid(track_uri)
Exemple #32
0
 def is_track_uri_valid(self, track_uri):
     return SpotifyUtil.is_track_uri_valid(track_uri)
 def update(self, node=None):
     self.uri = SpotifyUtil.id2uri('track', self.id)
Exemple #34
0
 def update(self, node=None):
     self.uri = SpotifyUtil.id2uri('artist', self.id)
    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