LoginSignup
2
0

More than 1 year has passed since last update.

Spotify API をPythonでちょっと遊ぶ

Posted at

はじめに

初めまして
Spotifyを一年で750時間も使用しているヘビーユーザーです。
そんなSpotifyがAPIを出しているらしくなかなか難しそうだなとか思いながら、ようやく重い腰を上げて取り組んでみることにしました。
ついでにモチベ維持のためにQiitaも始めようというわけです。 
あとは自分のノートがわり

前置きはこの辺にして

ID取得

APIを使用するにはSpotifyに登録が必要らしいです。
まだの方はこちらからどうぞ

それじゃあ必要なClient IDとClient Secretを取得していきます。
こちらから
My Dashboard -> LOG IN -> My New app
で名前と詳細記入したらClient IDとClient Secretが表示されるはずです。
わからなかったらYoutube見てね

safariじゃ動かなかったよ

本題

じゃあちょっとだけ使います。
pip install spotipy
でspotipyをインストールして
これからいちいち自分のIDをコピペするのはだるいのでIDのフォルダを作ります。

spotify_id.py
def id():
    return client_id

def secret():
    return client_secret

client_id = 'ここに君のID'
client_secret = 'ここに君のsecret'

って感じで準備完了
それじゃあリファレンスにあったコード+自分のIDとsecretを書いてみた

test.py
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import sys
import spotify_id as si

lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'

client_id = si.id()
client_secret = si.secret()
client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials(client_id, client_secret)
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

results = spotify.artist_top_tracks(lz_uri)

for track in results['tracks'][:10]:
    print('track    : ' + track['name'])
    print('audio    : ' + track['preview_url'])
    print('cover art: ' + track['album']['images'][0]['url'])
    print()

これで情報が取れるわけですよ

track    : Stairway to Heaven - Remaster
audio    : https://p.scdn.co/mp3-preview/fc80a280376d5142c888475bd8fdcd00b4fc8d7d?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69

track    : Immigrant Song - Remaster
audio    : https://p.scdn.co/mp3-preview/e5910b86cc788766388cca1c494094e39186aa1e?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b27390a50cfe99a4c19ff3cbfbdb

track    : Whole Lotta Love - 1990 Remaster
audio    : https://p.scdn.co/mp3-preview/f3765a6c5b194302c6e7fffc31ba21e2bd616cf6?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273fc4f17340773c6c3579fea0d

track    : Black Dog - Remaster
audio    : https://p.scdn.co/mp3-preview/f82af31817a7ec198a709e68f0f0f9edfc9a7e81?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69

track    : Kashmir - Remaster
audio    : https://p.scdn.co/mp3-preview/64908f0ca685e8727a2f07615ee147d28adbc450?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273765b0617b572bdd1dbdc7d8e

track    : Ramble On - 1990 Remaster
audio    : https://p.scdn.co/mp3-preview/6b06102e8b3cf22d49834f443f7870f7f1bbdbb4?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273fc4f17340773c6c3579fea0d

track    : Rock and Roll - Remaster
audio    : https://p.scdn.co/mp3-preview/6ce49759af47a1eb104e05dac1938d79ddb15ebd?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69

track    : Going to California - Remaster
audio    : https://p.scdn.co/mp3-preview/85bbb9572e554786b320555e9bab071986fe73de?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69

track    : Good Times Bad Times - 1993 Remaster
audio    : https://p.scdn.co/mp3-preview/9d54ac8d655aebf2920de790b421ba13e3508d5b?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b2736f2f499c1df1f210c9b34b32

track    : D'yer Mak'er - Remaster
audio    : https://p.scdn.co/mp3-preview/d270345618cefadf336a0c7289601e844099265c?cid=b9e3a248dffe4f25ad39434b0610f5cb
cover art: https://i.scdn.co/image/ab67616d0000b2731816adce1d49e35d3ce9a1d1

ということで最初の記事はこの辺にします。
モチベ維持頑張るぞー

参考

こちらの記事をもとに勉強しました。ありがとうございます。
PythonでSpotify API [とにかく使ってみる編]

2
0
1

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
2
0