Occasionally I wanted to do programming related to my hobby, so this time I tried to extract artist and song information using the spotify API. It was my first time to hit the API in Python, but it was easy to do so I will leave it as a memo.
After creating a Spotify account, register the application with Spotify for developer.
Dashboard-> Create an app-> Name and description description-> Create. Make a note of the Client ID and Client Secret for later use.
This time I want to call the API in Python, so install spotipy.
$ pip install spotipy
Next, write the code appropriately while looking at the Official spotipy reference.
main.py
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
client_id = 'Client id'
client_secret = 'Client secret'
spotify_client_credentials = spotipy.oauth2.SpotifyClientCredentials(client_id, client_secret)
spotify = spotipy.Spotify(client_credentials_manager=spotify_client_credentials)
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()
The above code will extract the top 10 songs from Led Zeppelin. When I run it ...
track : Stairway to Heaven - Remaster
audio : https://p.scdn.co/mp3-preview/8226164717312bc411f8635580562d67e191a754?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69
()
track : Whole Lotta Love - 1990 Remaster
audio : https://p.scdn.co/mp3-preview/ce11b19a4d2de9976d7626df0717d0073863909c?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273fc4f17340773c6c3579fea0d
()
track : Immigrant Song - Remaster
audio : https://p.scdn.co/mp3-preview/8455599677a13017978dcd3f4b210937f0a16bcb?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b27390a50cfe99a4c19ff3cbfbdb
()
track : Black Dog - Remaster
audio : https://p.scdn.co/mp3-preview/9b76619fd9d563a48d38cc90ca00c3008327b52e?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69
()
track : Ramble On - 1990 Remaster
audio : https://p.scdn.co/mp3-preview/83383aceb01ea27b0bffdedfaebe55e29b33aca2?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273fc4f17340773c6c3579fea0d
()
track : Kashmir - 1990 Remaster
audio : https://p.scdn.co/mp3-preview/13b2c47c98132f80a5ce12d8a4ceb192ba7a4345?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b2732abc2d86a442efb6cc631d0a
()
track : Good Times Bad Times - 1993 Remaster
audio : https://p.scdn.co/mp3-preview/c1f024eb57b569b926c8e68cab0a6056dc7d9654?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b2736f2f499c1df1f210c9b34b32
()
track : Rock and Roll - Remaster
audio : https://p.scdn.co/mp3-preview/e7ea8a13f7caf6942c5447e9cd96aac2a076d85a?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69
()
track : Going to California - Remaster
audio : https://p.scdn.co/mp3-preview/4bdae56c6a9f7a8ec42b753cb7bea2c77ec68f1e?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273c8a11e48c91a982d086afc69
()
track : All My Love - 1990 Remaster
audio : https://p.scdn.co/mp3-preview/d03424a544dcc5102703ebb081fd321dc5567b32?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273b66922383836fd1f0973018f
()
Top 10 was displayed firmly.
Next is how to find out the id of your favorite artist.
This time, I will select my favorite band and check it out.
As a procedure
main.py
.
.
.
name = 'the cabs'
result = spotify.search(q='artist:' + name, type='artist')
artist_id = result['artists']['items'][0]['id'] #The id of the band with the best name match in the search results
print(artist_id)
Run the above code.
5PalnqYJTpnO5wt00jf0um
I was able to get it.
Please rewrite the obtained id after lz_uri
.
By the way, if you try to output with result ['artists'] ['items'] [0]
, it will look like this.
{
'genres': ['japanese emo', 'japanese math rock'],
'name': 'the cabs',
'external_urls': {'spotify':'https://open.spotify.com/artist/5PalnqYJTpnO5wt00jf0um'},
'popularity': 31,
'uri': 'spotify:artist:5PalnqYJTpnO5wt00jf0um',
'href': 'https://api.spotify.com/v1/artists/5PalnqYJTpnO5wt00jf0um',
'followers': {'total': 13436, 'href': None},
'images': [{'url': 'https://i.scdn.co/image/ab67616d0000b2739140de0bace4c7ad18772317',
'width': 640, 'height': 640}, {'url': 'https://i.scdn.co/image/ab67616d00001e029140de0bace4c7ad18772317','width': 300, 'height': 300}, {'url': 'https://i.scdn.co/image/ab67616d000048519140de0bace4c7ad18772317', 'width': 64, 'height': 64}],
'type': 'artist',
'id': '5PalnqYJTpnO5wt00jf0um'
}
If you use this, it seems that you can extract not only id but also genre and popularity.
Finally, after rewriting to the id obtained earlier, try to output Top10 again.
track : nigastu no heitai
audio : https://p.scdn.co/mp3-preview/f2d4ff9b80143f7f32a141f6ab7bff4defe73a0b?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b2739140de0bace4c7ad18772317
()
track : no future for us
audio : https://p.scdn.co/mp3-preview/6cff89f323cd347550291c205711bdfde1aa809a?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b2739140de0bace4c7ad18772317
()
track : kilche no rasen
audio : https://p.scdn.co/mp3-preview/a9db4caf8ffaddca0bdfa3c42a16daabe296fbf3?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273d78469daa324d40bc7c6a588
()
track : anschluss
audio : https://p.scdn.co/mp3-preview/a3f0f761d94b026c9d9f21f6c6ef27050aaf4368?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b27371b964ec832c791e2e880428
()
track : smile lazuro
audio : https://p.scdn.co/mp3-preview/401a013906f025a6c5a2077195bec8eee7da6ec3?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b27371b964ec832c791e2e880428
()
track : for Charles Bronson
audio : https://p.scdn.co/mp3-preview/0d148429f2d09a78d39d00c389f7af42d2bb81d1?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b2739140de0bace4c7ad18772317
()
track : purusha
audio : https://p.scdn.co/mp3-preview/a68213bd40a599cf0b86f77bcc10b77c222a75f8?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b27371b964ec832c791e2e880428
()
track : camn aven
audio : https://p.scdn.co/mp3-preview/84b9a4c8f35fd36b8884d003ee17836265fb47f3?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273d78469daa324d40bc7c6a588
()
track : nicol kills nicolas for nicola
audio : https://p.scdn.co/mp3-preview/8f0d42c0f6ab021bef41096e89be4716727eec6c?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b2739140de0bace4c7ad18772317
()
track : skór
audio : https://p.scdn.co/mp3-preview/a5ac040f0fc0ef21bd297790bce0f08455b954e9?cid=6551068e9be94d4bb07c29cb25fa84f0
cover art: https://i.scdn.co/image/ab67616d0000b273d78469daa324d40bc7c6a588
()
It went well. Next time, I would like to look at the information in more detailed song units. Next time
A story using the API published by Spotify spotipy Official Reference
Recommended Posts