Skip to content

Instantly share code, notes, and snippets.

Embed
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 = sp.search(q=query, type='track')
# Return the ID of the track
return results['tracks']['items'][0]['id']
else:
# 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 = soup.select(".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)
try:
# Get the artist and track title as strings
artist = track[1].strip()
title = track[0].strip()
try:
# 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!")
except:
# If the track couldn't be found, add it to its own list of errors
print("Couldn't add {}!".format(track))
errors.append(track)
except:
# 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)
try:
# Batch add the tracks
# I'm manually creating a playlist right now
sp.user_playlist_add_tracks(
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):
print(e)
except Exception as e:
# If there's a spotify error, show that here.
print("These tracks had errors:")
for err in enumerate(errors):
print(err)
print(e)
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)
else:
print("Whoops, you're missing some info. Try:")
print(" python playlist.py wdbm_url spotify_username playlist_id")
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.