Spotify에서 아티스트 정보를 사용해 상위 5개의 노래 정보를 가져와보겠다.
우선 해당 사이트에서 Web APP을 생성하고, Client ID 와 Client Secret Key를 발급받는다.
developer.spotify.com/documentation/web-api/
Web API | Spotify for Developers
Simply put, your app receives Spotify content through the Spotify Web API.
developer.spotify.com
일반적으로는 endpoint 주소를 가져와 https 리퀘스트를 날리지만 오늘은 파이썬 Spotify 라이브러리인 Spotipy를 사용해서 곡 정보를 가져와보겠다. pip install 커맨드로 쉽게 라이브러리 설치가 가능하다.
$ pip install spotipy
Spotipy외의 다양한 Spotify 라이브러리들은 여기에서 찾아볼 수 있다:
developer.spotify.com/documentation/web-api/libraries/
Web API Libraries | Spotify for Developers
Music, meet code. Powerful APIs, SDKs and widgets for simple and advanced applications.
developer.spotify.com
BTS의 상위 5개 트랙을 가져오는 코드다. 가수의 아이디는 웹에서 스포티파이에 접속하면 url에 포함되어있는 아이디를 가져오면 된다.
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET_ID"
# BTS
lz_uri = 'spotify:artist:3Nrfpe0tUJi4K4DXYWgMUX'
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
results = sp.artist_top_tracks(lz_uri)
# get top 5 tracks
for track in results['tracks'][:5]:
print('track : ' + track['name'])
print('audio : ' + track['preview_url'])
print('cover art: ' + track['album']['images'][0]['url'])
print()
결과:
참고 사이트:
[Spotify Web API] 특정 artist의 top 10 playlist 가져오기 / 파이썬 spotipy 라이브러리 사용
Spotify란 전세계 최대의 음원 스트리밍 서비스 그리고 spotify의 web api를 통해서 artists, tracks, playlists 등 유용한 여러 정보를 받을 수 있다. 단, 한국에선 막혀있기 때문에 브라우저의 VPN 우회기능으
jisun-rea.tistory.com