Spotifyに「Footchy Cooks」「Sidebacks」の曲を分析してもらう①【Python】
いつも愛読ありがとう😊。音楽愛好家の悠真 Suzukiだ。
今回はSpotifyのデータを取得できるサービス「Spotify API」を利用して、「Footchy Cooks」「Sidebacks」のサブスクに公開されている曲の分析データを見てみようと思う。
Pythonで以下のコードに必要なことを入力して走らせる。
ネットで「spotify api 使い方」などで検索して、自分のIDなどを手にいれ、Pythonが使える環境を用意し、「pip install spotipy」的なことをすればSpotify APIは簡単に使える。下のコードは書き換えて使って欲しい。曲のIDは曲のURLの「https://open.spotify.com/track/」の次がIDである。
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import csv
#spotify
client_id = '自分のID'
client_secret = '自分のSECRET'
client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials(client_id, client_secret)
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
ids = [曲のIDをリストで]
list = []
list.append(['name', 'song', 'acoustic', 'dance', 'loud', 'energy', 'inst', 'positive'])
for id in ids:
s_d = spotify.track(id)
s_f = spotify.audio_features(id)[0]
s_a = spotify.audio_analysis(id)
list.append([s_d['artists'][0]['name'], s_d['name'], s_f['acousticness'], s_f['danceability'], s_f['loudness'], s_f['energy'], s_f['instrumentalness'], s_f['valence']])
with open('data.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(list)
そして得られたのが以下の表だ
これだけではわからないので、次回はこれらの数値に似た曲を検索する。
この記事が気に入ったらサポートをしてみませんか?