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

At the beginning

This is a continuation of Last time. We will look at API acquisition for each album, song items, etc.

Get album information

Use the id of any artist obtained last time.

main.py


import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

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)

artist_uri = 'spotify:artist:5PalnqYJTpnO5wt00jf0um'
results = spotify.artist_albums(artist_uri, album_type='album')
print(results)

Now you can get the album information of the artist specified by id. When I run it ...

{
		"album_type": "album",
		"name": "Regenerative landscape",
		"external_urls": {
			"spotify": "https://open.spotify.com/album/2evf6T9osScdVnhVrL7tXX"
		},
		"release_date": "2013-01-23",
		"uri": "spotify:album:2evf6T9osScdVnhVrL7tXX",
		"total_tracks": 10,
		"href": "https://api.spotify.com/v1/albums/2evf6T9osScdVnhVrL7tXX",
		"artists": [
			{
				"name": "the cabs",
				"external_urls": {
					"spotify": "https://open.spotify.com/artist/5PalnqYJTpnO5wt00jf0um"
				},
				"uri": "spotify:artist:5PalnqYJTpnO5wt00jf0um",
				"href": "https://api.spotify.com/v1/artists/5PalnqYJTpnO5wt00jf0um",
				"type": "artist",
				"id": "5PalnqYJTpnO5wt00jf0um"
			}
		],
		"images": [
			{
				"url": "https://i.scdn.co/image/ab67616d0000b273c4f2e7d76207985faf2427c5",
				"width": 640,
				"height": 640
			},
			{
				"url": "https://i.scdn.co/image/ab67616d00001e02c4f2e7d76207985faf2427c5",
				"width": 300,
				"height": 300
			},
			{
				"url": "https://i.scdn.co/image/ab67616d00004851c4f2e7d76207985faf2427c5",
				"width": 64,
				"height": 64
			}
		],
		"album_group": "album",
		"type": "album",
		"id": "2evf6T9osScdVnhVrL7tXX",
		"available_markets": [
			"JP"
		],
		"release_date_precision": "day"
	},
	{
		"album_type": "album",
		"name": "saisei no hukei",
		"external_urls": {
			"spotify": "https://open.spotify.com/album/6koCjq9UGVZFa9YjJgCLok"
		},
		"release_date": "2013-01-23",
		"uri": "spotify:album:6koCjq9UGVZFa9YjJgCLok",
		"total_tracks": 10,
		"href": "https://api.spotify.com/v1/albums/6koCjq9UGVZFa9YjJgCLok",
		"artists": [
			{
				"name": "the cabs",
				"external_urls": {
					"spotify": "https://open.spotify.com/artist/5PalnqYJTpnO5wt00jf0um"
				},
				"uri": "spotify:artist:5PalnqYJTpnO5wt00jf0um",
				"href": "https://api.spotify.com/v1/artists/5PalnqYJTpnO5wt00jf0um",
				"type": "artist",
				"id": "5PalnqYJTpnO5wt00jf0um"
			}
		],
		"images": [
			{
				"url": "https://i.scdn.co/image/ab67616d0000b27371b964ec832c791e2e880428",
				"width": 640,
				"height": 640
			},
			{
				"url": "https://i.scdn.co/image/ab67616d00001e0271b964ec832c791e2e880428",
				"width": 300,
				"height": 300
			},
			{
				"url": "https://i.scdn.co/image/ab67616d0000485171b964ec832c791e2e880428",
				"width": 64,
				"height": 64
			}
		],
		"album_group": "album",
		"type": "album",
		"id": "6koCjq9UGVZFa9YjJgCLok",
		"available_markets": [
			"AD",
			"AE",
			"AL",
			"AR",
			"AT",
			"AU",
			"BA",
			"BE",
			"BG",
			"BH",
			"BO",
			"BR",
			"BY",
			"CA",
			"CH",
			"CL",
			"CO",
			"CR",
			"CY",
			"CZ",
			"DE",
			"DK",
			"DO",
			"DZ",
			"EC",
			"EE",
			"EG",
			"ES",
			"FI",
			"FR",
			"GB",
			"GR",
			"GT",
			"HK",
			"HN",
			"HR",
			"HU",
			"ID",
			"IE",
			"IL",
			"IN",
			"IS",
			"IT",
			"JO",
			"KW",
			"KZ",
			"LB",
			"LI",
			"LT",
			"LU",
			"LV",
			"MA",
			"MC",
			"MD",
			"ME",
			"MK",
			"MT",
			"MX",
			"MY",
			"NI",
			"NL",
			"NO",
			"NZ",
			"OM",
			"PA",
			"PE",
			"PH",
			"PL",
			"PS",
			"PT",
			"PY",
			"QA",
			"RO",
			"RS",
			"RU",
			"SA",
			"SE",
			"SG",
			"SI",
			"SK",
			"SV",
			"TH",
			"TN",
			"TR",
			"TW",
			"UA",
			"US",
			"UY",
			"VN",
			"XK",
			"ZA"
		],
		"release_date_precision": "day"
	}
]

It's quite a long time, but I was able to get the album. I will look at the part that interests me.

available_markets

It seems to represent a country where you can watch. Apparently, it is described in the ISO country code. In other words, it seems that there are places where you cannot listen to it in some countries.

total_tracks

The total number of songs included in the album.

album_type

In the above result, only album is available, but there are also single and compilation, which are used to classify on the Spotify screen.

Next, let's get the music.

Acquisition of music

Use the album_id you used earlier. I look at a different album, but the way I get the id doesn't change.

main.py


.
.
.
album_id = 'spotify:album:61Xe5yDSI4IrIqPdYqXxMJ'
results = spotify.album(album_id)['tracks']['items'][1] #Get the second song of the album
print(results)
{
'is_local': False,
  'name': 'camn aven',
  'external_urls': {
      'spotify': 'https://open.spotify.com/track/7gA5tchFTCxarNIt3JGDhD'
  },
  'uri': 'spotify:track:7gA5tchFTCxarNIt3JGDhD',
  'explicit': False,
  'preview_url': 'https://p.scdn.co/mp3-preview/84b9a4c8f35fd36b8884d003ee17836265fb47f3?cid=6551068e9be94d4bb07c29cb25fa84f0', 
  'track_number': 2,
  'disc_number': 1,
  'href': 'https://api.spotify.com/v1/tracks/7gA5tchFTCxarNIt3JGDhD',
  'artists': [
    {
      'name': 'the cabs', 
    'external_urls': {
        'spotify': 'https://open.spotify.com/artist/5PalnqYJTpnO5wt00jf0um'
        },
        'uri': 'spotify:artist:5PalnqYJTpnO5wt00jf0um',
        'href': 'https://api.spotify.com/v1/artists/5PalnqYJTpnO5wt00jf0um',
        'type': 'artist',
        'id': '5PalnqYJTpnO5wt00jf0um'}],
  'duration_ms': 214000,
  'type': 'track',
  'id': '7gA5tchFTCxarNIt3JGDhD',
  'available_markets': ['JP']
}

We will look at the characteristics of the song using the id of the song obtained from here.

Get features

We will use the above track_id. It seems that it can be obtained with an object called audio_features.

main.py


.
.
.
track_id = 'spotify:album:7gA5tchFTCxarNIt3JGDhD'
results = spotify.audio_features(track_id)
print(results)

[
  {
    'track_href': 'https://api.spotify.com/v1/tracks/7gA5tchFTCxarNIt3JGDhD',
    'analysis_url': 'https://api.spotify.com/v1/audio-analysis/7gA5tchFTCxarNIt3JGDhD',
    'energy': 0.941,
    'liveness': 0.0952,
    'tempo': 100.324,
    'speechiness': 0.052,
    'uri': 'spotify:track:7gA5tchFTCxarNIt3JGDhD',
    'acousticness': 8.32e-05,
    'instrumentalness': 0.0512,
    'time_signature': 3,
    'danceability': 0.449,
    'key': 5,
    'duration_ms': 214000,
    'loudness': -2.499,
    'valence': 0.538,
    'type': 'audio_features',
    'id': '7gA5tchFTCxarNIt3JGDhD',
    'mode': 1
  }
]

I could see more detailed information about the song. There are some items that you are not familiar with even if you use spotify normally. I will post the official reference later, but I will introduce some.

energy This item is evaluated between 0 and 1. It seems that the faster, noisy, and noisy songs are highly evaluated.

time_signature Represents the time signature of a song. The song I extracted, camn aven, has a strange time signature, so I think it's probably not evaluated correctly.

instrumentalness This is an instrument (length of playing time). When it exceeds 0.5, it is close to an instrumental track.

danceability It shows how easy it is to dance. It is a mystery how to evaluate it.

At the end

This time, I analyzed it in smaller units such as music. There are quite a lot of hidden parameters, so it would be interesting to try to find a similar band using this.

Articles that I used as a reference

Get Audio Features for a Track ← Explains the parameters of the song. Get an Album

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 climb a mountain with reinforcement learning
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 tried to analyze the whole novel "Weathering with You" ☔️
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