How to find out if a playlist has a playlist image cover in Spotipy?

Viewed 125

As title says, im trying to see how if a playlist has a playlist image cover so that it doesn't try to load one that doesnt exist.

Here is my method:

 currentPlaylist = spotifyObject.user_playlist(username, playlistManageURI)
     
        if ['images'][0] in currentPlaylist:
            playlistCover_url = currentPlaylist['images'][0]['url']
            image = QImage()
            image.loadFromData(requests.get(playlistCover_url).content)
            self.playlistCover.setScaledContents(True)
            self.playlistCover.setPixmap(QPixmap(image))
        else:
            print('Playlist Cover doesnt exist!')

which while if a playlist that does have an image cover does load, if i try to load one with an image cover that doesnt exist, it gives me

IndexError: list index out of range

here is how currentPlaylist looks like with a playlist that does have a cover

{
    "collaborative": false,
    "description": "",
    "external_urls": {
        "spotify": "https://open.spotify.com/playlist/xxxxxxxx"
    },
    "followers": {
        "href": null,
        "total": 4
    },
    "href": "https://api.spotify.com/v1/playlists/xxxxxxxx?additional_types=track",
    "id": "xxxxxx",
    "images": [
        {
            "height": null,
            "url": "https://i.scdn.co/image/ab67706c0000bebbcab54ad44bbf6dd124838df1",
            "width": null
        }
    ],
    "name": "xxxxx",
    "owner": {
        "display_name": "xxxxx",
        "external_urls": {
            "spotify": "https://open.spotify.com/user/xxxxxxxx"
        },
        "href": "https://api.spotify.com/v1/users/xxxx",
        "id": "xxxxx",
        "type": "user",
        "uri": "spotify:user:xxxx"

and this is how it looks like without a cover (total blank playlist)

{
    "collaborative": false,
    "description": "xxxx",
    "external_urls": {
        "spotify": "https://open.spotify.com/playlist/xxxxxx"
    },
    "followers": {
        "href": null,
        "total": 0
    },
    "href": "https://api.spotify.com/v1/playlists/xxxxxx?additional_types=track",
    "id": "xxxxxx",
    "images": [],
    "name": "xxxxxx",
    "owner": {
        "display_name": "xxxxx",
        "external_urls": {
            "spotify": "https://open.spotify.com/user/xxxxx"
        },
        "href": "https://api.spotify.com/v1/users/xxxx",
        "id": "xxxxxxxx",
        "type": "user",
        "uri": "xxxxxxx"
2 Answers

If "images" always exists in currentPlaylist irrespective of whether currentPlaylist['images'] is empty or not.

if currentPlaylist['images']: 
     ...

Otherwise

if "images" in currentPlaylist and  currentPlaylist["images"]:
    ...

solved with this usage:

if currentPlaylist.get('images') == []:
            print('no image found in playlist!')
        else:
            print(['images'][0])
            playlistCover_url = currentPlaylist['images'][0]['url']
            image = QImage()
            image.loadFromData(requests.get(playlistCover_url).content)
            self.playlistCover.setScaledContents(True)
            self.playlistCover.setPixmap(QPixmap(image))
Related