Published: 2019-06-05 08:00 |
Category: Code | Tags: beautifulsoup, pity party, programming, python, requests, spotify, wdbm
After posting this and tagging WDBM on Twitter, they sent the following:
Hey Brian! We upload our playlists on Impact89fm’s Spotify every week! Thank you for the support, happy listening! ?
—Pity Party (@pityparty_wdbm) June 5, 2019
If you want to listen, just search next time ¯\_(ツ)_/¯
Python has been my programming language of choice lately. Today, I gave myself a little challenge to create a Spotify playlist from a tracklist posted to a website.
I'm a big fan of WDBM out of Michigan State University. They have a great college station that reminds of my the music scene back in Rochester, NY (what was awesome). Every week, they have a live show called Pity Party and it highlights alternative/emo/rock goodness. I try to catch the show if I can, but I often miss it because I'm not always near my computer to stream.
They post their playlist each week on their website. I fired up a Python project with BeautifulSoup and requests to get the web page data and a new (to me) library called Spotipy which gave me API access.
This happens in a couple steps. The first thing to do was scrape the web page, which is super easy with BeautifulSoup and Requests. The website uses the same format for their playlist each week:
<span class="storycontent"> <p>Track 1 - Artist 1</p> <p>Track 2 - Artist 2</p> </span>
BeautifulSoup lets me set up a quick loop to grab each of the <p> tags in a list that I can loop over.
The Spotify API allows you to search by artist and track name. If a single result is returned on the search, its ID is added to a list to post in bulk to the playlist. This is more efficient than looping each one individually.
Any track that isn't found for whatever reason is added to an errors list that is shown to the user when everythig is done. That way, they can go back and check them manually. It may be that the track doesn't exist or their was some weird punctuation or something.
Instead of taking 20 minutes to search and add each song manually, this runs in less than 10 seconds.
Here's the full script if you're interested in checking it out. The entire WDBM specialty show catalog uses the same format, so you can try it with other pages over ther.
Featured image is lightning flickr photo by Tom Gill. shared under a Creative Commons (BY-NC-ND) license.