[PYTHON] I want to analyze songs with Spotify API 1

At the beginning

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.

Next time

Registration

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.

Call the API

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.

How to get an artist id

This time, I will select my favorite band and check it out.

As a procedure

  1. Enter the name of the band you want to look up
  2. Get search results
  3. Output only id from there Write the code like this.

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

Articles that I used as a reference

A story using the API published by Spotify spotipy Official Reference

Recommended Posts

I want to analyze songs with Spotify API 2
I want to analyze songs with Spotify API 1
I want to analyze logs with Python
I want to do ○○ with Pandas
I want to debug with Python
I want to be able to analyze data with Python (Part 3)
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to pip install with PythonAnywhere
I want to play with aws with python
I tried to analyze my favorite singer (SHISHAMO) using Spotify API
I want to use MATLAB feval with python
I started to analyze
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I want to be an OREMO with setParam!
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to use ip vrf with SONiC
I want to solve APG4b with Python (Chapter 2)
I want to start over with Django's Migrate
I want to write to a file with Python
I want to convert an image to WebP with lollipop
I want to detect unauthorized login to facebook with Jubatus (1)
I want to transition with a button in flask
I want to handle optimization with python and cplex
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to split a character string with hiragana
I tried to uncover our darkness with Chatwork API
I want to AWS Lambda with Python on Mac!
I want to manually create a legend with matplotlib
[TensorFlow] I want to process windows with Ragged Tensor
[ML Ops] I want to do multi-project with Python
How to analyze with Google Colaboratory using Kaggle API
I tried to analyze J League data with Python
I want to run a quantum computer with Python
I want to bind a local variable with lambda
I want to solve Sudoku (Sudoku)
I want to remove Python's Unresolved Import Warning with vsCode
I want to use R functions easily with ipython notebook
I want to specify another version of Python with pyvenv
I want to make a blog editor with django admin
I want to start a jupyter environment with one command
[NetworkX] I want to search for nodes with specific attributes
I want to make a click macro with pyautogui (desire)
I want to change the Japanese flag to the Palau flag with Numpy
I want to color black-and-white photos of memories with GAN
I want to automatically attend online classes with Python + Selenium!
I want to make a click macro with pyautogui (outlook)
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
Create playlists of bright songs only with Spotify Web API
[Visualization] I want to draw a beautiful graph with Plotly