Как узнать, есть ли у плейлиста обложка изображения плейлиста в Spotipy?

Как говорится в заголовке, я пытаюсь понять, как если список воспроизведения имеет обложку изображения списка воспроизведения, чтобы он не пытался загрузить несуществующее.

Вот мой метод:

 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!')

в то время как если плейлист с обложкой изображения действительно загружается, если я пытаюсь загрузить его с обложкой изображения, которая не существует, это дает мне

IndexError: list index out of range

вот как выглядит currentPlaylist с плейлистом, у которого есть обложка

{
    "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"

а вот так он выглядит без обложки (полный пустой плейлист)

{
    "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"

person marceloexc    schedule 25.07.2020    source источник
comment
извините, я просто добавил их правильно, как они выглядят прямо сейчас   -  person marceloexc    schedule 27.07.2020
comment
Пожалуйста, сообщите, если это не работает.   -  person Vishesh Mangla    schedule 27.07.2020


Ответы (2)


Если изображения всегда существуют в currentPlaylist, независимо от того, является ли currentPlaylist['images'] пустым или нет.

if currentPlaylist['images']: 
     ...

В противном случае

if "images" in currentPlaylist and  currentPlaylist["images"]:
    ...
person Vishesh Mangla    schedule 27.07.2020

решено с помощью этого использования:

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))
person marceloexc    schedule 29.07.2020