Spotify API (Get an Artist's Albums) (pagination) - helloMinji/chatbot_spotify GitHub Wiki
-
artist id๊ฐ ํ์!
: Search ARI request ๊ฐ์์ id ๊ฐ์ ธ์ค๊ธฐ -
paging object
- href: requestํ url
- item: request data list โญ
- limit
- next: ๋ค์ ์ ๋ณด๋ฅผ ์ํ url โญ
- offset: ์์์
- total: ์ ์ฒด ์จ๋ฒ ์
Spotify Albums API
r = requests.get("https://api.spotify.com/v1/artists/3Nrfpe0tUJi4K4DXYWgMUX/albums", headers=headers)
# 3Nrfpe0tUJi4K4DXYWgMUX : id๊ฐ
raw = json.loads(r.text)
total = raw['total']
offset = raw['offset']
limit = raw['limit']
next = raw['next']
albums = []
albums.extend(raw['items']) # ํ์ฌ๊น์ง ๊ฐ์ ธ์จ item์ albums ๋ฆฌ์คํธ์ ์ถ๊ฐ
limit default๊ฐ 20์ด๋ฏ๋ก, ๊ทธ๋ณด๋ค ๋ง์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํด์๋ ์๋์ ๊ณผ์ ์ด ํ์ํ๋ค.
## 100๊ฐ ์ถ์ถํ๋ ๊ฒฝ์ฐ
count = 0
while count < 100 or not next:
r = requests.get(raw['next'], headers=headers) # ์์ ๋ค๋ฅธ r. ์๋ก์ด ์์ฒญ์ ํ๊ธฐ ๋๋ฌธ์ offset๋ถํฐ ์๋ก ์ถ์ถ!
raw = json.loads(r.text)
next = raw['next']
print(next)
albums.extend(raw['items'])
count = len(albums)
print(len(albums))