Spotify Listening Stats
This project uses the Spotipy library in Google Colab to analyse personal Spotify listening data and provide headline statistics on listening activity.
Features
- Retrieves your top tracks and artists over different time ranges
- Calculates average audio features (danceability, energy, etc.) of your most-played tracks
- Generates a breakdown of your listening habits by genre
- Visualizes listening trends over time
Prerequisites
- Python 3.7+ (I used Google Colab)
- A Spotify account
- Spotify Developer credentials (Client ID and Client Secret)
Setup
- Clone this repository:
- Set up your Spotify Developer account:
- Go to the Spotify Developer Dashboard
- Create a new app
- Note your Client ID and Client Secret
- Add http://localhost:8888/callback as a Redirect URI in your app settings
- Set environment variables with your Spotify credentials: Do not share your API client ID or client secret on Github
To handle this, i've used files with enviroment variables (.env) and utilised a .gitignore file to ensure sensitive information isnt included. I've then set and called them as variables within the python code.
Create a .env file and store in a secure place. See these instructions how to create a .env file: https://github.com/RubyNixx/spotify_analysis_using_spotipy/blob/main/Steps_to_create_env_file.md
-
Run the main python script in google colab
-
On first run, you'll be prompted to authorise the app to access your Spotify data. Follow the link provided to complete the authorisation process.
-
Sample Outputs:
The Spotipy library offers several key features for interacting with the Spotify Web API:
-
Full API access: Spotipy provides access to all endpoints of the Spotify Web API, allowing you to retrieve a wide range of music data
-
User authorisation support: The library supports both the Authorization Code flow and the Client Credentials flow for user authentication
-
Lightweight and easy to use: Spotipy is designed to be a lightweight Python wrapper for the Spotify Web API, making it simple to integrate into your projects
-
Comprehensive data retrieval: You can fetch information about artists, albums, tracks, playlists, user profiles, and more
-
Search functionality: Spotipy allows you to search for items in the Spotify catalog
-
Playlist management: You can create, modify, and delete playlists, as well as add or remove tracks
-
User library interactions: The library enables you to access and modify a user's saved tracks and albums
-
Audio features and analysis: You can retrieve audio features and detailed audio analysis for tracks
-
Personalisation: Spotipy provides access to personalized data such as a user's top artists and tracks
-
Follow/unfollow functionality: You can manage following/unfollowing artists, users, and playlists
-
Browse and recommendations: The library allows you to access Spotify's browse and recommendation features
-
Error handling: Spotipy includes built-in error handling for API requests
More information can be found here: Spotipy API documentation: https://spotipy.readthedocs.io/en/2.24.0/ Note: This will help with understanding the API & how to connect to it. I've included a clause in the python code to help work with the API in a google colab notebook.
Spotipy githib repo: https://github.com/spotipy-dev/spotipy
Spotipy scopes: https://developer.spotify.com/documentation/web-api/concepts/scopes#user-top-read
Stack overflow thread that helped with audio features: https://stackoverflow.com/questions/38823403/scrape-deep-audio-features-using-spotipy-python-library
Exploring features of Spotify playlists - data collection code: https://orda.shef.ac.uk/articles/code/Exploring_features_of_Spotify_playlists_data_collection_code/19107998
Other github repo inspiration utilising the API: Using approaches likes PCA to reduce dimensions of data: https://stmorse.github.io/journal/spotify-api.html
Some examples of how others have used it: https://github.com/spotipy-dev/spotipy-examples/blob/main/showcases.ipynb