简体   繁体   中英

Getting the track's ID from a playlist with Spotify's API in Python

I'm trying to use the Spotify's API to get from a playlist all the track's id

I think that I just don't really know how to use json on Python as it's my first time using API's

This is the json that I get when requesting for a random playlist:

{ 
    "items":[ 
       { 
          "added_at":"2020-02-20T19:08:11Z",
          "added_by":{ 
          },
          "is_local":False,
          "primary_color":"None",
          "track":{ 
             "album":{ 
                "album_type":"single",
                "artists":[ 
                   { 
                      "external_urls":{ 
                         "spotify":"https://open.spotify.com/artist/1k5UEOU4igPC0NoHjEekha"
                      },
                      "href":"https://api.spotify.com/v1/artists/1k5UEOU4igPC0NoHjEekha",
                      "id":"1k5UEOU4igPC0NoHjEekha",
                      "name":"Milkoi",
                      "type":"artist",
                      "uri":"spotify:artist:1k5UEOU4igPC0NoHjEekha"
                   },
                   { 
                      "external_urls":{ 
                         "spotify":"https://open.spotify.com/artist/3U2oWd07HPgl60o8RBMG4P"
                      },
                      "href":"https://api.spotify.com/v1/artists/3U2oWd07HPgl60o8RBMG4P",
                      "id":"3U2oWd07HPgl60o8RBMG4P",
                      "name":"Miraie",
                      "type":"artist",
                      "uri":"spotify:artist:3U2oWd07HPgl60o8RBMG4P"
                   }
                ],
                "available_markets":[  ],
                "external_urls":{ 
                   "spotify":"https://open.spotify.com/album/69Y9i1D5TyQGxWdqFNRIhC"
                },
                "href":"https://api.spotify.com/v1/albums/69Y9i1D5TyQGxWdqFNRIhC",
                "id":"69Y9i1D5TyQGxWdqFNRIhC",
                "images":[ 
                   { 
                      "height":640,
                      "url":"https://i.scdn.co/image/4ea41b9dde13c6cb31fff8fe3c5ee90076370885",
                      "width":640
                   },
                   { 
                      "height":300,
                      "url":"https://i.scdn.co/image/6edf03567c0379d246c750147fd31a74574e4e27",
                      "width":300
                   },
                   { 
                      "height":64,
                      "url":"https://i.scdn.co/image/fbbc5cca3adbaf433f43917012939c3e2c35c5eb",
                      "width":64
                   }
                ],
                "name":"ミユキ",
                "release_date":"2018-09-30",
                "release_date_precision":"day",
                "total_tracks":1,
                "type":"album",
                "uri":"spotify:album:69Y9i1D5TyQGxWdqFNRIhC"
             },
             "artists":[ 
                { 
                   "external_urls":{ 
                      "spotify":"https://open.spotify.com/artist/1k5UEOU4igPC0NoHjEekha"
                   },
                   "href":"https://api.spotify.com/v1/artists/1k5UEOU4igPC0NoHjEekha",
                   "id":"1k5UEOU4igPC0NoHjEekha",
                   "name":"Milkoi",
                   "type":"artist",
                   "uri":"spotify:artist:1k5UEOU4igPC0NoHjEekha"
                },
                { 
                   "external_urls":{ 
                      "spotify":"https://open.spotify.com/artist/3U2oWd07HPgl60o8RBMG4P"
                   },
                   "href":"https://api.spotify.com/v1/artists/3U2oWd07HPgl60o8RBMG4P",
                   "id":"3U2oWd07HPgl60o8RBMG4P",
                   "name":"Miraie",
                   "type":"artist",
                   "uri":"spotify:artist:3U2oWd07HPgl60o8RBMG4P"
                }
             ],
             "available_markets":[  ],
             "disc_number":1,
             "duration_ms":211090,
             "episode":False,
             "explicit":False,
             "external_ids":{ 
                "isrc":"QM42K1817396"
             },
             "external_urls":{ 
                "spotify":"https://open.spotify.com/track/77xwKl9jpVLO6VmNlwGwtm"
             },
             "href":"https://api.spotify.com/v1/tracks/77xwKl9jpVLO6VmNlwGwtm",
             "id":"77xwKl9jpVLO6VmNlwGwtm",
             "is_local":False,
             "name":"ミユキ",
             "popularity":43,
             "preview_url":"https://p.scdn.co/mp3-preview/45e0b6cf4f358f5fbf6bebc1f019e67a780fa3f8?cid=2cd60e0da58b47518a61cec560d21ccd",
             "track":True,
             "track_number":1,
             "type":"track",
             "uri":"spotify:track:77xwKl9jpVLO6VmNlwGwtm"
          },
          "video_thumbnail":{ 
             "url":"None"
          }
       },
       { 
          "added_at":"2020-02-20T19:08:21Z",
          "added_by":{ 
             },
             "href":"https://api.spotify.com/v1/users/akqpr9b7ycor7uw08afmc3hx4",
             "id":"akqpr9b7ycor7uw08afmc3hx4",
             "type":"user",
             "uri":"spotify:user:akqpr9b7ycor7uw08afmc3hx4"
          },
          "is_local":False,
          "primary_color":"None",
          "track":{ 
             "album":{ 
                "album_type":"album",
                "artists":[ 
                   { 
                      "external_urls":{ 
                         "spotify":"https://open.spotify.com/artist/24HASvYQG1OvEFRWVWmOfx"
                      },
                      "href":"https://api.spotify.com/v1/artists/24HASvYQG1OvEFRWVWmOfx",
                      "id":"24HASvYQG1OvEFRWVWmOfx",
                      "name":"Kano",
                      "type":"artist",
                      "uri":"spotify:artist:24HASvYQG1OvEFRWVWmOfx"
                   }
                ],
                "available_markets":[  ],
                "external_urls":{ 
                   "spotify":"https://open.spotify.com/album/72sG7hFVmyFlxg9e7PfV0K"
                },
                "href":"https://api.spotify.com/v1/albums/72sG7hFVmyFlxg9e7PfV0K",
                "id":"72sG7hFVmyFlxg9e7PfV0K",
                "images":[ 
                   { 
                      "height":640,
                      "url":"https://i.scdn.co/image/ab67616d0000b27327dfa5f6ab057a4ec5c53235",
                      "width":640
                   },
                   { 
                      "height":300,
                      "url":"https://i.scdn.co/image/ab67616d00001e0227dfa5f6ab057a4ec5c53235",
                      "width":300
                   },
                   { 
                      "height":64,
                      "url":"https://i.scdn.co/image/ab67616d0000485127dfa5f6ab057a4ec5c53235",
                      "width":64
                   }
                ],
                "name":"rye",
                "release_date":"2018-12-19",
                "release_date_precision":"day",
                "total_tracks":14,
                "type":"album",
                "uri":"spotify:album:72sG7hFVmyFlxg9e7PfV0K"
             },
             "artists":[ 
                { 
                   "external_urls":{ 
                      "spotify":"https://open.spotify.com/artist/24HASvYQG1OvEFRWVWmOfx"
                   },
                   "href":"https://api.spotify.com/v1/artists/24HASvYQG1OvEFRWVWmOfx",
                   "id":"24HASvYQG1OvEFRWVWmOfx",
                   "name":"Kano",
                   "type":"artist",
                   "uri":"spotify:artist:24HASvYQG1OvEFRWVWmOfx"
                }
             ],
             "available_markets":[  ],
             "disc_number":2,
             "duration_ms":222249,
             "episode":False,
             "explicit":False,
             "external_ids":{ 
                "isrc":"JPTE01809900"
             },
             "external_urls":{ 
                "spotify":"https://open.spotify.com/track/6c9llTTjTcLgoHbKaJVw4f"
             },
             "href":"https://api.spotify.com/v1/tracks/6c9llTTjTcLgoHbKaJVw4f",
             "id":"6c9llTTjTcLgoHbKaJVw4f",
             "is_local":False,
             "name":"六兆年と一夜物語",
             "popularity":39,
             "preview_url":"https://p.scdn.co/mp3-preview/3421753cafdf34dc1e34bba479f048ebd613f39f?cid=2cd60e0da58b47518a61cec560d21ccd",
             "track":True,
             "track_number":3,
             "type":"track",
             "uri":"spotify:track:6c9llTTjTcLgoHbKaJVw4f"
          },
          "video_thumbnail":{ 
             "url":"None"
          },
    ],
    "limit":100,
    "next":"None",
    "offset":0,
    "previous":"None",
    "total":11
 }


The ID is located at "items" -> "track" -> "id" for each track

I'm trying to get track's id but from all the tracks of the playlist so I can put them in a list.

If someone could help me it would be cool

Thanks in advance

I'm using Python 3

What I would do is put the response string into a response dictionary like so (if you haven't already)

resp_dict = json.load(your_response_string)

Then you could loop through and append all the track ids to a new list

newList = []
for x in response_dict['items']:
    newList.append(x['track']['id'])

# To see if it worked
for item in newList:
    print(item)

Let me know if that makes sense or you need some further explanation :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM