0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Spotify APIを使用して1年間で一番よく聞いたアーティスト、楽曲を50人、曲ずつ調べる

Last updated at Posted at 2025-10-23

はじめに

※この記事は非公開状態で作成され、作成日は2024/12/06です。
公開日は2025/10/23となっています

背景

今年も終わりになってきていて、音楽アプリに今年よく聞いた楽曲のランキングなどが出てくると思います。そこでこう思った人はいないでしょうか。1~5位までしかランキングが表示されなくてもっとランキングを知りたい。

そういう人のために今日はそれを解決するコードを作成していきたいと思います。

環境

エディタはVScodeで、使用言語はPython3.9を用いて実行しています。
使用したライブラリは以下の通りです。

  • spotipy

事前準備

Spotify for Developerの登録、Spotifyのアカウントを持っていることが必須になります。
Spotify for Developerの登録について以下の記事が参考になります。
内部パラメータなどについても詳しく解説されています。

インポート

import spotipy
from spotipy.oauth2 import SpotifyOAuth

spotipyのライブラリを取得しています。
自分のSpotifyにアクセスするのでSpotifyOAuthを使用します。

Spotify認証

# Spotify API認証
my_id = '設定したID'
secret_key = '設定したkey'
redirect_uri='設定したURL'

# SpotifyOAuthを使用して認証を行い、spotipy.Spotifyのインスタンス作成
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=my_id, client_secret=secret_key, redirect_uri=redirect_uri, scope='user-top-read'))

ここでSpotify for Developerで登録したID、Client ID、Redirect URIを入力します。
scopeにはuser-top-readを入力します。これはSpotifyユーザーのトップアーティストやトップトラックにアクセスするための権限をリクエストするためのものです。

トップアーティストを取得

#トップアーティストを取得
results = sp.current_user_top_artists(limit=50, time_range='long_term')  
print("好きなアーティストランキング:")
for count, artist in enumerate(results['items']):
    print(f"{count + 1}. {artist['name']}")

current_user_top_artistsメソッドにアクセスしてターミナルに表示しています。

トップ曲を取得

#トップ曲を取得
results =sp.current_user_top_tracks(limit=50,time_range="long_term")
print("\n\n好きな曲ランキング:")
for count,track in enumerate(results['items']):
    print(f"{count+1}.{track['name']}")

current_user_top_tracksメソッドにアクセスしてターミナルに表示しています。

補足

上記のURLにあるようにメソッドにアクセスする際のtime_rangeの引数を変えることにより表示される期間を変更することができます。

  • long_term 約1年間
  • medium_term 約6ヶ月
  • short_term 約4週間

コード全体

import spotipy
from spotipy.oauth2 import SpotifyOAuth

# Spotify API認証
my_id = '設定したID'
secret_key = '設定したkey'
redirect_uri='設定したURL'

# SpotifyOAuthを使用して認証を行い、spotipy.Spotifyのインスタンス作成
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=my_id, client_secret=secret_key, redirect_uri=redirect_uri, scope='user-top-read'))

results = sp.current_user_top_artists(limit=50, time_range='long_term')  
print("好きなアーティストランキング:")
for count, artist in enumerate(results['items']):
    print(f"{count + 1}. {artist['name']}")

results =sp.current_user_top_tracks(limit=50,time_range="long_term")
print("\n\n好きな曲ランキング:")
for count,track in enumerate(results['items']):
    print(f"{count+1}.{track['name']}")

実行結果

50曲ずつ表示されると思います。limitの引数を変えることで表示する数を変更できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?