
๋จผ์ 2020์ ๋ฐ๋งค๋ ์์ ํธ๋์ ์ ๋ณด๋ฅผ ๋ฐ์์จ๋ค.
์ฝ๋
result = sp.search(q='year:2020', type='track', limit=5, offset=1)
๊ฒฐ๊ณผ

์ด ๋ 2020 ๋ฐ๋งค๋ ์จ๋ฒ์ ๋ํ ์ค์ง์ ์ธ ์ ๋ณด๋ โtrack โ itemsโ์ ๊ฒฝ๋ก๋ฅผ ํตํด ์ป์ ์ ์๋ค.

artist_name =[]
artist_id =[]
song = []
for i in range(0,1000,50):
track_results = sp.search(q='year:2020', type='track', limit=50, offset=i)
for i, t in enumerate(track_results['tracks']['items']):
album_uri = t['album']['uri']
album_info = sp.album(album_uri)
artist_name.append(album_info['artists'][0]['name'])
artist_id.append(album_info['artists'][0]['id'])
artist_song = []
for j in range(len(album_info['tracks']['items'])):
name = album_info['tracks']['items'][j]['name']
track_number = album_info['tracks']['items'][j]['track_number']
id = album_info['tracks']['items'][j]['id']
audio_feature = sp.audio_features(id)
url = album_info['tracks']['items'][j]['external_urls']['spotify']
artist_song.append({'name':name,'track_number':track_number,'id':id,'url':url,'audio_feature':audio_feature})
song.append(artist_song)
song_2020 = pd.DataFrame([artist_id,artist_name,song],index=['Id','Artist','Song']).T
print(song_2020.head())
song_2020.to_excel('song_2020.xlsx')

2020 - 2023๊น์ง ๊ณก ์ ๋ณด๋ฅผ ์์งํ๋ค!!
์๋์ ์ฝ๋๋ฅผ ํตํด audio_feature๋ฅผ ์ถ์ถํ ์ ์๋ค.
sp.audio_features('track id์
๋ ฅ')
๐ย excel ๋ถ๋ฌ์ DataFrame ์์ฑํ๋ ๊ณผ์
song_2020 = pd.read_excel('song_2020.xlsx',index_col=0) song_2020.head()
type(song_2020['Song']) # pandas.core.series.SeriesโSongโ ์ด์ ๋ค์ด์๋ ๋ฐ์ดํฐ๋ to_excel()์ ๊ณผ์ ์ ๊ฑฐ์น ๋ String์ผ๋ก ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ์ด๋ฅผ ๋ค์ ๋ฆฌ์คํธ๋ก ๋ง๋ค์ด์ผํ๋ค.
pickle์ ์ด์ฉํด์ ์ ์ฅํ๋ฉด ๋ฆฌ์คํธ์ ํํ๋ก ์ ์ฅํ ์ ์๋๋ฐ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ์์งํ๊ธฐ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ๋ฟ๋๋ฌ ์ค๊ฐ์ API ํ ๋นํฅ ์ค๋ฅ๊ฐ ๋์ ์ด์ฉ ์ ์์ด ๊ธฐ์กด ํ์ผ์ ์ด์ฉํด์ผํ์
โ ast.literal_eval()์ ์ด์ฉํด String์ List๋ก ๋ณํ์ํจ๋ค.import ast def to_list(x): return ast.literal_eval(x) song_2020['Song'] = song_2020['Song'].apply(to_list)![]()
์ด๋ ์ธ๋ฑ์ค๋ ์จ๋ฒ์ ๋ปํ๋ฉฐ ๋ฆฌ์คํธ ์์ ๋ค์ด์๋ ๊ฒ์ ์จ๋ฒ์์ ์๋ก๋ ํธ๋(๊ณก)๋ค ์ด๋ค. ์ฐ๋ฆฌ๋ ๊ณก๋ค์ id๋ฅผ ์ด์ฉํด audio_features๋ฅผ ๊ฐ์ ธ์ฌ ๊ฒ์ด๋ค.
์ด๋ song_2020[โSongโ]์ ์ธ๋ฑ์ค๋ ์จ๋ฒ์ ๋ปํ๋ฉฐ ๋ฆฌ์คํธ ์์ ๋ค์ด์๋ ๊ฒ์ ์จ๋ฒ์์ ์๋ก๋ ํธ๋(๊ณก)๋ค ์ด๋ค. ์ฐ๋ฆฌ๋ ๊ณก๋ค์ id๋ฅผ ์ด์ฉํด audio_features๋ฅผ ๊ฐ์ ธ์ฌ ๊ฒ์ด๋ค. ๋ฐ๋ณต๋ฌธ์ ํตํด ํผ์ณ๋ค์ ์ถ์ถํด์จ๋ค.
2021,2022,2023 ํ์ผ๋ ๋๊ฐ์ด ์งํ ํ ๊ฒ์ด๋ค.
# track๋ง๋ค id ์ถ์ถ
audio_features_id= []
for i in song_2020['Song']:
id_list = []
for j in i:
id = j['id']
id_list .append(id)
audio_features_id.append(id_list)
API ํ ๋๋น์ ๋ถ์กฑ์ผ๋ก audio_features๋ค์ ์ถ์ถํ๋ค๊ฐ ์ค๊ฐ์ ๋๊ธฐ๋ ์ฌ๊ณ ๊ฐ ๋ฐ์ํ๋ค.

๋ค์ํ ํด๊ฒฐ์ฑ ์ด ์๋๋ฐ ์ด๋ฏธ ์ค๋ ํ ๋น๋์ ๋ค ์จ๋ฒ๋ ค์ ๊ฐฑ์ ์ด ๋ ๋๊น์ง ๊ธฐ๋ค๋ ค์ผ ์ด ํด๊ฒฐ์ฑ ๋ค์ ์๋ํด๋ณผ ์ ์์๋ฏ ์ถ๋คโฆใ ใ ใ ใ ใ ใ ใ
Rate Limits | Spotify for Developers
์์ ํ์ด์ง๋ฅผ ๋ค์ด๊ฐ๋ณด๋ฉด ์๋์ ๊ฐ์ ๋ฌธ๊ตฌ๋ฅผ ๋ณผ ์ ์๋ค.
โถ๏ธ Spotify API๋ API์ ์์ ์ฑ์ ์ ์งํ๊ณ ์ ์๋์ ํ์ ๋๊ณ ์๋ค๊ณ ํ๋ค.Spotify์ API ์๋ ์ ํ์ 30์ด ๋์ ์ฑ์ด Spotify์ ๋ํด ์ํํ ํธ์ถ ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ณ์ฐ๋๋ฉฐ, ์ฑ์ด ์ฑ์ ์๋ ์ ํ์ ์ด๊ณผํ๋ฉด Spotify์ ์น API์์ 429 ์ค๋ฅ ์๋ต์ด ํ์๋๋ค.(๋ด๊ฐ ๋ง์ดํ๋ ๊ทธ ์ค๋ฅ!)
๋๋ฌธ์ ์๋ ์ ํ์ ์ผ๋๋ฅผ ๋๊ณ ์ฑ์ ๋น๋ํด์ผํ๋ค!!!
1๏ธโฃ ํ์ฅ ํ ๋น๋ ๋ชจ๋ ์ ์ฒญ
์ด ๋ชจ๋์ ์ฑ์๋ ์ ์ฑ์ ๊ธฐ๋ณธ ๋ชจ๋์ธ ๊ฐ๋ฐ ๋ชจ๋์ ์ฑ๋ณด๋ค ํจ์ฌ ๋์ ์๋ ์ ํ์ด ์๋ค. ์ด๋ฅผ ์ ์ฒญํ๊ธฐ ์ํด์๋ Spotify์ ํ๊ฐ๋ฅผ ๋ฐ์์ผํ๋๊ฒ ๋์๊ฒ๋ ์ด ๊ณผ์ ์ด ์ด๋ ค์ ๋คโฆ
โ ์ด ๋ฐฉ๋ฒ์ ๋ ๊ฐ์ด ์ ๋ณด๋ฅผ ์์งํ๊ธฐ ์ํด API๋ฅผ ์ฌ์ฉํ๋ ์ฌ๋๋ค๋ณด๋ค๋ ์ง์ง ์ฑ์ ๋ง๋ค ๋ชฉ์ ์ผ๋ก ์ฌ์ฉํ๋ ์ฌ๋๋ค์๊ฒ ๋ ํ์ํ ๊ฑฐ ๊ฐ๋ค.(์น์ธ ์ค๊ฐ ๊ณผ์ ์ ์ฑ์ ๊ตฌ์ฒด์ ์ธ ์ฌ์ง์ ์ฒจ๋ถํ๋ผ๋ ์ง๋ฌธ์ด ์์๊ธฐ ๋๋ฌธ!)
2๏ธโฃ ๋ฐฐ์น API ํ์ฉ
Spotify ์๋ ํ๋์ API ์์ฒญ์ผ๋ก ์ผ๊ด ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ย Get Multiple Albumsย ์๋ํฌ์ธํธ ์ ๊ฐ์ ๋ช ๊ฐ์ง API๊ฐ ์๋ค. audio_feature๋ track id๊ฐ ๋ด๊ฒจ์๋ ๋ฆฌ์คํธ๋ฅผ ์
๋ ฅํ๋ฉด id ๋ณ audio_feature๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐํํด์ค๋ค.
๐กย ๋๋ฒ์งธ ๋ฐฉ๋ฒ์ ์ฌ์ฉํด ํ ๋น๋์ ๋ชจ๋ ์ฑ์ฐ์ง ์๊ณ audio_feature๋ฅผ ๋ฐ์์ฌ ์ ์์๋ค!
song_2022 = pd.read_excel('song_2022.xlsx',index_col=0)
song_2022['Song'] = song_2022['Song'].apply(to_list)
audio_features_id= []
for i in song_2022['Song']:
id_list = []
for j in i:
id = j['id']
id_list .append(id)
audio_features_id.append(id_list)
audio_features_2022 = []
count = 0
for i in audio_features_id:
print('count : {}'.format(count))
feature = sp.audio_features(i)
audio_features_2022.append(feature)
count += 1
์ ์ฝ์์ต๋๋ค. ๊ถ๊ธํ ์ ์ด ์๋๋ฐ ํน์ error detection code ์์ฑํ์ง ์๊ณ ๋ 429 error ๋จ์ จ๋์?
except spotipy.SpotifyException as e:
SpotifyException ์ฒ๋ฆฌํด์คฌ๋๋ฐ๋ 429error๋ ์ ๋จ๊ณ *์ํ๋ก search๋ถ๋ถ์์ ๋งํ๋ค์.. token ์๊ฐ๋ ํ์ธํ๋๋ฐ ๋ง๋ฃ๋ ๊ฑด ์๋๋ผ ๋ญ๊ฐ ๋ฌธ์ ์ธ์ง ํน์ ์์๋ ๊ฒ ์์๊น ์ถ์ด ๋๊ธ ๋จ๊น๋๋ค.