/redSpot---source-code

A series of scripts that updates Spotify playlists with the top voted songs on Reddit.

Primary LanguageJavaScript

redSpot

A series of scripts that updates Spotify playlists with the top voted songs on Reddit.

Top playlists update every day at 4:30 AM EST, the Hot playlists update every hour on the 40 minute mark. The Spotify API occassionaly returns the wrong track or doesn't find the track at all even though it's there.

If you want a playlist for a particular subreddit, let me know at akhilkalepu@protonmail.com.


reddit.com/r/music

/r/music : 🔥 {redSpot}

/r/music : 🔝 {redSpot}

reddit.com/r/listentothis

/r/listentothis : 🔥 {redSpot}

/r/listentothis : 🔝 {redSpot}

reddit.com/r/electronicmusic

/r/electronicmusic : 🔥 {redSpot}

/r/electronicmusic : 🔝 {redSpot}

reddit.com/r/hiphopheads

/r/hiphopheads : 🔥 {redSpot}

/r/hiphopheads : 🔝 {redSpot}

reddit.com/r/rock

/r/rock : 🔥 {redSpot}

/r/rock : 🔝 {redSpot}

reddit.com/r/metal

/r/metal : 🔥 {redSpot}

/r/metal : 🔝 {redSpot}

reddit.com/r/jazz

/r/jazz : 🔥 {redSpot}

/r/jazz : 🔝 {redSpot}

reddit.com/r/classicalmusic

/r/classicalmusic : 🔥 {redSpot}

/r/classicalmusic : 🔝 {redSpot}

reddit.com/r/experimentalmusic

/r/experimentalmusic : 🔥 {redSpot}

/r/experimentalmusic : 🔝 {redSpot}


Imgur


Technologies used:


Imgur

My scripts run on Heroku using the Scheduler add-on. I use Python Reddit API Wrapper to scrape the subreddits for the weekly "top" posts as well as the current "hot". The post titles are sent to a MySQL/JawsDB database with a table for each playlist. The /r/music script is also able to filter posts by the "music streaming" or "video" flair, ensuring I only scrape songs instead of news articles and self posts. Find the code in redditScrape.py.

Imgur


My initial idea was just to retreive a list of Spotify links for the Reddit posts, so I used Node Spotify API to find the matching tracks. This NPM package can only read Spotify's API, so it doesn't need any authorization.

Imgur

The script then connects to the MySQL database and goes through the list of post titles in each subreddit's table. Each title is formatted to remove unnecessary characters and strings while allowing strings stored in (parantheses) to remain. This is so tags for remixes, live performances and secondary titles are not removed.


Imgur

The resulting string is used by Web Spotify API Node for the track that best matches the post title. Searches that don't return a track are skipped. The API will occasionally return a false match. Once a track is found, its song ID is sent to another MySQL table specifically for Spotify information. Find the code in spotify.js.


Imgur

In order automate the playlist scripts, I need to get a new access token for Spotify's API every hour using a refresh token first obtained on localhost. Spotify's API requires you to manually obtain the initial access and refresh tokens, but the re-authorization process can be automated after that, as long as it's refreshed within one hour. The refresh.js script runs every 60 minutes to ensure the server can always access my playlists.


Imgur

The last step is to go through this table and add the tracks to a playlist using the song IDs. In order to do this, I use Spotify Web API Node to refresh my access token one more time before deleting all the tracks in a specified playlist, collecting the song IDs into a JavaScript array and using the API to add the respective tracks into said playlist. There is a short pause before each subreddit's script runs to ensure I don't overload the API with too many requests. Find the code in add.js.


Imgur

The final product is a self-updating playlist of the top songs on Reddit! If you have any questions, requests or suggestions, please feel free to contact me at akhilkalepu@protonmail.com.