How to Build Your Own Spotify Wrapped with Python, Spotipy and Glide Apps

How to create your own Spotify Wrapped with APIs

What We’ll Build

Tech Stack

Requirements

Getting Our Top Tracks

Authorization

Using Spotipy

$ pip install spotipy
import requests
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import pandas as pd
SPOTIPY_CLIENT_ID=’your-client-id’
SPOTIPY_CLIENT_SECRET=’your-client-secret’
SPOTIPY_REDIRECT_URI=’your-redirect-uri’
SCOPE = “user-top-read”
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID, client_secret=SPOTIPY_CLIENT_SECRET, redirect_uri=SPOTIPY_REDIRECT_URI, scope=SCOPE))
results = sp.current_user_top_tracks()
top_tracks_short = sp.current_user_top_tracks(limit=10, offset=0, time_range=”short_term”)

Getting Track Information

def get_track_ids(time_frame):
track_ids = []
for song in time_frame[‘items’]:
track_ids.append(song[‘id’])
return track_ids
track_ids = getTrackIDs(top_tracks_long)
track_ids
def get_track_features(id):
meta = sp.track(id)
# meta
name = meta[‘name’]
album = meta[‘album’][‘name’]
artist = meta[‘album’][‘artists’][0][‘name’]
spotify_url = meta[‘external_urls’][‘spotify’]
album_cover = meta[‘album’][‘images’][0][‘url’]
track_info = [name, album, artist, spotify_url, album_cover]
return track_info
# loop over track ids 
tracks = []
for i in range(len(track_ids)):
time.sleep(.5)
track = get_track_features(track_ids[i])
tracks.append(track)
# create dataset
df = pd.DataFrame(tracks, columns = [‘name’, ‘album’, ‘artist’, ‘spotify_url’, ‘album_cover’])
def convert_to_df(track_ids):
# loop over track ids
tracks = []
for i in range(len(track_ids)):
time.sleep(.5)
track = get_track_features(track_ids[i])
tracks.append(track)
# create dataset
df = pd.DataFrame(tracks, columns = [‘name’, ‘album’, ‘artist’, ‘spotify_url’, ‘album_cover’])
# save to CSV
df.to_csv(f’{today}-{time_period}.csv’)
time_ranges = [‘short_term’, ‘medium_term’, ‘long_term’]
for time_period in time_ranges:
top_tracks = sp.current_user_top_tracks(limit=20, offset=0, time_range=time_period)
track_ids = getTrackIDs(top_tracks)
convert_to_df(track_ids)

Inserting Data Into Google Sheets

$ pip install gspread
{
“type”: “service_account”,
“project_id”: “api-project-XXX”,
“private_key_id”: “2cd … ba4”,
“private_key”: “ — — -BEGIN PRIVATE KEY — — -\nNrDyLw … jINQh/9\n — — -END PRIVATE KEY — — -\n”,
“client_email”: “473000000000-yoursisdifferent@developer.gserviceaccount.com”,
“client_id”: “473 … hd.apps.googleusercontent.com”,

}
$ import gspread
gc = gspread.service_account(filename=’my-spotify-wrapped-test-ad510490b281.json’)
sh = gc.open(“My Spotify Wrapped”)
worksheet = sh.worksheet(“short_term”)
val = worksheet.acell(‘B5’).value
print(val)
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
# insert into sheetdef insert_to_gsheet(track_ids):
# loop over track ids
tracks = []
for i in range(len(track_ids)):
time.sleep(.5)
track = get_track_features(track_ids[i])
tracks.append(track)
# create dataset
df = pd.DataFrame(tracks, columns = [‘name’, ‘album’, ‘artist’, ‘spotify_url’, ‘album_cover’])
# insert into google sheet
gc = gspread.service_account(filename=’my-spotify-wrapped-test-ad510490b281.json’)
sh = gc.open(‘My Spotify Wrapped’)
worksheet = sh.worksheet(f’{time_period}’)
worksheet.update([df.columns.values.tolist()] + df.values.tolist())
print(‘Done’)
time_ranges = [‘short_term’, ‘medium_term’, ‘long_term’]
for time_period in time_ranges:
top_tracks = sp.current_user_top_tracks(limit=20, offset=0, time_range=time_period)
track_ids = get_track_ids(top_tracks)
insert_to_gsheet(track_ids)

Creating an App With Glide

Wrapping Up

May or May Not Work for the CIA.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store