This repository contains a Python script that uses the Spotipy library to interact with the Spotify Web API. The script retrieves information about an artist's top tracks and stores it in a SQLite database.
Before running the script, you'll need to have the following installed:
- Python 3: The script is written in Python 3 and requires it to be installed on your system.
- Spotipy: This is a Python library for the Spotify Web API. It provides a simple interface for interacting with the API and retrieving data.
- pycountry: This is a Python library that provides ISO country, subdivision, language, currency and script definitions and their translations.
You'll also need to set up a Spotify Developer account and register an application to obtain a client_id
and client_secret
. These values should be set as environment variables named SPOTIFY_CLIENT_ID
and SPOTIFY_CLIENT_SECRET
, respectively.
To install the required libraries, you can use pip
by running the following command:
pip install spotipy pycountry
To run the script, navigate to the directory containing the script and run the following command:
python3 spotify_api.py
Replace spotify_api.py
with the name of the script file.
The script will prompt you to enter the name of an artist. After entering the artist's name, the script will search for the artist on Spotify and retrieve their top tracks. The track information will be stored in a SQLite database named song_data.db
.
The database contains a table named songs
with columns for various track attributes such as artist_name
, track_name
, danceability
, energy
, etc. The script will insert data into this table for each of the artist's top tracks.
- The first few lines of the code import the necessary libraries:
os
,sqlite3
,spotipy
,SpotifyClientCredentials
fromspotipy.oauth2
, andpycountry
. - The next few lines set up the client credentials for accessing the Spotify Web API using the
SpotifyClientCredentials
class from thespotipy.oauth2
module. Theclient_id
andclient_secret
are retrieved from environment variables and passed to theSpotifyClientCredentials
constructor. Aspotipy.Spotify
object is then created using theclient_credentials_manager
parameter. - The database name is set to
"song_data.db"
. - The code checks if the database file exists. If it doesn't, a new SQLite database file is created and a table named
songs
is created in the database with columns for various track attributes. - A connection to the database is established and a cursor object is created.
- The user is prompted to enter the name of an artist.
- The code searches for the artist on Spotify using the
search
method of thespotipy.Spotify
object and retrieves the artist's Spotify ID. - The code retrieves information about the artist's albums and top tracks using various methods of the
spotipy.Spotify
object such asartist_albums
,album
, andartist_top_tracks
. - For each of the artist's top tracks, the code retrieves audio features and popularity information using methods such as
audio_features
andtrack
. - The track information is inserted into the
songs
table in the database if it doesn't already exist. - The track name and audio features are printed to the console.
- Finally, the database connection is closed.
Contributions are welcome! If you have any ideas for new features or improvements, feel free to open an issue or submit a pull request.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.