Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A script to scrape The Pity Part playlist from WDBM and create a spotify playlist
import re, sys
import requests
import spotipy
import spotipy.util as util
from bs4 import BeautifulSoup
# Get the ID of the track from Spotify
def get_track_id(username, title, artist):
print("Searching for {} by {}".format(title, artist))
scope = 'playlist-modify-public'
# If you're not logged into Spotify, log in
token = util.prompt_for_user_token(username, scope)
# Now that we have a token, look for the track by building a query
if token:
sp = spotipy.Spotify(auth=token)
query = "track:" + title + " artist:" + artist
results =, type='track')
# Return the ID of the track
return results['tracks']['items'][0]['id']
# Handle the login failure
print("Can't get token for", username)
def get_the_list(url, username):
# Get the webpage text and parse it out
resp = requests.get(url)
soup = BeautifulSoup(resp.text, 'html.parser')
# Find the dig holding the track information
search =".storycontent > p")
# Create a couple of lists to hold results
track_ids = []
errors = []
# For each <p> tag in the tracklist, run through the loop
for p in range(len(search) - 1):
# Strip a pesky em-dash
track = re.split(r'\u2013|\u2014', search[p].text)
# Get the artist and track title as strings
artist = track[1].strip()
title = track[0].strip()
# Search for the track and append a succesful result to a list
track_ids.append(get_track_id(username, title, artist))
print("Added it to the playlist!")
# If the track couldn't be found, add it to its own list of errors
print("Couldn't add {}!".format(track))
# Worst case scenario. We don't want to be here.
print("Check for a problem with {} on line {}".format(track, p))
# Use your logged in account
scope = 'playlist-modify-public'
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
# Batch add the tracks
# I'm manually creating a playlist right now
username, 'your-playlist-id', track_ids)
# Print the tracks you couldn't find to the console so you can check them manually
print("The playlist was created! These tracks had problems:")
for e in enumerate(errors):
except Exception as e:
# If there's a spotify error, show that here.
print("These tracks had errors:")
for err in enumerate(errors):
if __name__ == "__main__":
if len(sys.argv) == 4:
url = sys.argv[1]
username = sys.argv[2]
playlist_id = sys.argv[3]
make_the_list(url, username, playlist_id)
print("Whoops, you're missing some info. Try:")
print(" python wdbm_url spotify_username playlist_id")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment