Ejemplo n.º 1
0
 def get_album(self, album_id):
     """
         Return spotify album as SearchItem
         @param album id as str
         @return SearchItem
     """
     try:
         s = Lio.File.new_for_uri("https://api.spotify.com/v1/"
                                  "albums/%s" % album_id)
         (status, data, tag) = s.load_contents(self._cancel)
         if status:
             decode = json.loads(data.decode('utf-8'))
             album_item = SearchItem()
             album_item.name = decode['name']
             album_item.cover = decode['images'][0]['url']
             album_item.smallcover = decode['images'][2]['url']
             album_item.ex_id = album_id
             for item in decode['tracks']['items']:
                 track_item = SearchItem()
                 track_item.is_track = True
                 track_item.name = item['name']
                 track_item.album = album_item.name
                 try:
                     track_item.year = decode[
                                             'release_date'][:4]
                 except:
                     pass  # May be missing
                 track_item.tracknumber = int(
                                           item['track_number'])
                 track_item.discnumber = int(
                                            item['disc_number'])
                 track_item.duration = int(
                                     item['duration_ms']) / 1000
                 for artist in item['artists']:
                     track_item.artists.append(artist['name'])
                 if not album_item.artists:
                     album_item.artists = track_item.artists
                 album_item.subitems.append(track_item)
             return album_item
     except Exception as e:
         print("SpotifySearch::get_album:", e)
     return None
Ejemplo n.º 2
0
 def get_album(self, album_id):
     """
         Return spotify album as SearchItem
         @param album id as str
         @return SearchItem
     """
     try:
         s = Lio.File.new_for_uri("https://api.spotify.com/v1/"
                                  "albums/%s" % album_id)
         (status, data, tag) = s.load_contents(self._cancel)
         if status:
             decode = json.loads(data.decode("utf-8"))
             album_item = SearchItem()
             album_item.name = decode["name"]
             album_item.cover = decode["images"][0]["url"]
             album_item.smallcover = decode["images"][2]["url"]
             album_item.ex_id = album_id
             for item in decode["tracks"]["items"]:
                 track_item = SearchItem()
                 track_item.is_track = True
                 track_item.name = item["name"]
                 track_item.album = album_item.name
                 try:
                     track_item.year = decode[
                                             "release_date"][:4]
                 except:
                     pass  # May be missing
                 track_item.tracknumber = int(
                                           item["track_number"])
                 track_item.discnumber = int(
                                            item["disc_number"])
                 track_item.duration = int(
                                     item["duration_ms"]) / 1000
                 for artist in item["artists"]:
                     track_item.artists.append(artist["name"])
                 if not album_item.artists:
                     album_item.artists = track_item.artists
                 album_item.subitems.append(track_item)
             return album_item
     except Exception as e:
         print("SpotifySearch::get_album:", e)
     return None
Ejemplo n.º 3
0
    def __get_artists(self, name):
        """
            Get albums for artists name
            @param name as str
        """
        try:
            # Read album list
            formated = GLib.uri_escape_string(name, None,
                                              True).replace(' ', '+')
            s = Gio.File.new_for_uri("https://api.spotify.com/v1/search?q=%s"
                                     "&type=artist" % formated)
            (status, data, tag) = s.load_contents(self._cancel)
            if status:
                decode = json.loads(data.decode('utf-8'))
                # For each album, get cover and tracks
                artists = []
                for item in decode['artists']['items']:
                    album_items = []
                    artist_id = item['id']
                    if item['name'].lower() in artists:
                        continue
                    artists.append(item['name'].lower())
                    s = Gio.File.new_for_uri("https://api.spotify.com/"
                                             "v1/artists/%s/albums" %
                                             artist_id)
                    (status, data, tag) = s.load_contents(self._cancel)
                    if status:
                        decode = json.loads(data.decode('utf-8'))
                        albums = []
                        for item in decode['items']:
                            if item['name'].lower() in albums:
                                continue
                            album_item = SearchItem()
                            album_item.name = album_item.album_name = item[
                                'name']
                            albums.append(album_item.name.lower())
                            album_item.cover = item['images'][0]['url']
                            album_item.smallcover = item['images'][2]['url']
                            album_items.append(album_item)
                            album_item.ex_id = item['id']

                    for album_item in album_items:
                        s = Gio.File.new_for_uri("https://api.spotify.com/v1/"
                                                 "albums/%s" %
                                                 album_item.ex_id)
                        (status, data, tag) = s.load_contents(self._cancel)
                        if status:
                            decode = json.loads(data.decode('utf-8'))
                            for item in decode['tracks']['items']:
                                track_item = SearchItem()
                                track_item.is_track = True
                                track_item.name = item['name']
                                track_item.album = album_item.name
                                try:
                                    track_item.year = decode[
                                        'release_date'][:4]
                                except:
                                    pass  # May be missing
                                track_item.tracknumber = int(
                                    item['track_number'])
                                track_item.discnumber = int(
                                    item['disc_number'])
                                track_item.duration = int(
                                    item['duration_ms']) / 1000
                                for artist in item['artists']:
                                    track_item.artists.append(artist['name'])
                                if not album_item.artists:
                                    album_item.artists = track_item.artists
                                album_item.subitems.append(track_item)
                        self._items.append(album_item)
                        GLib.idle_add(self.emit, 'item-found')
        except Exception as e:
            print("SpotifySearch::albums(): %s" % e)
Ejemplo n.º 4
0
    def __get_artists(self, name):
        """
            Get albums for artists name
            @param name as str
        """
        try:
            # Read album list
            formated = GLib.uri_escape_string(name, None, True).replace(
                                                                      ' ', '+')
            s = Lio.File.new_for_uri("https://api.spotify.com/v1/search?q=%s"
                                     "&type=artist" % formated)
            (status, data, tag) = s.load_contents(self._cancel)
            if status:
                decode = json.loads(data.decode('utf-8'))
                # For each album, get cover and tracks
                artists = []
                for item in decode['artists']['items']:
                    album_items = []
                    artist_id = item['id']
                    if item['name'].lower() in artists:
                        continue
                    artists.append(item['name'].lower())
                    s = Lio.File.new_for_uri("https://api.spotify.com/"
                                             "v1/artists/%s/albums" %
                                             artist_id)
                    (status, data, tag) = s.load_contents(self._cancel)
                    if status:
                        decode = json.loads(data.decode('utf-8'))
                        albums = []
                        for item in decode['items']:
                            if item['name'].lower() in albums:
                                continue
                            album_item = SearchItem()
                            album_item.name = item['name']
                            albums.append(album_item.name.lower())
                            album_item.cover = item['images'][0]['url']
                            album_item.smallcover = item['images'][2]['url']
                            album_items.append(album_item)
                            album_item.ex_id = item['id']

                    for album_item in album_items:
                        s = Lio.File.new_for_uri("https://api.spotify.com/v1/"
                                                 "albums/%s" %
                                                 album_item.ex_id)
                        (status, data, tag) = s.load_contents(self._cancel)
                        if status:
                            decode = json.loads(data.decode('utf-8'))
                            for item in decode['tracks']['items']:
                                track_item = SearchItem()
                                track_item.is_track = True
                                track_item.name = item['name']
                                track_item.album = album_item
                                try:
                                    track_item.year = decode[
                                                            'release_date'][:4]
                                except:
                                    pass  # May be missing
                                track_item.tracknumber = int(
                                                          item['track_number'])
                                track_item.discnumber = int(
                                                           item['disc_number'])
                                track_item.duration = int(
                                                    item['duration_ms']) / 1000
                                for artist in item['artists']:
                                    track_item.artists.append(artist['name'])
                                if not album_item.artists:
                                    album_item.artists = track_item.artists
                                album_item.subitems.append(track_item)
                        self._items.append(album_item)
                        GLib.idle_add(self.emit, 'item-found')
        except Exception as e:
            print("SpotifySearch::albums(): %s" % e)