Automatically post music from a Spotify playlist to Reddit!
Any tracks added to a user-specified playlist will be periodically posted to a subreddit of choice. BeetBot pulls all relevant info about a track from Spotify, then uses a string comparison algorithm (npm string-similarity) to find a matching YouTube link, and posts it to Reddit using title formatting that is the convention for most music subreddits: Artist - Title [genre] (year)
. Posts occur on a scheduled interval (default 8 hours) and include optional "fuzzing," or randomized delay to make post times appear more organic.
Don't worry about accidentally adding songs you've already posted; BeetBot will remember that for you and skip them automatically! It will also keep a list of songs for which it failed to find a reliable match on YouTube (see notPosted.json
), in case you want to post those manually later.
Built with NodeJS and the Spotify, Google, and Reddit APIs.
To set up this script, run the following commands in your terminal:
git clone https://github.com/aberrator9/BeetBot.git
npm install
node beetbot.js
This will install all Node dependencies and create some required files, as well as a /logs
directory, and an .env
file with some default keys that we will assign values in the following steps.
- Create a Spotify app. During setup, set the Redirect URI to http://localhost:8888/callback, and check the "Web API" box.
- Navigate to your app's settings, and copy the Client Id and Client Secret values to
S_CLIENT_ID
andS_CLIENT_SECRET
in the.env
file. - In your terminal, run the following from the project directory:
node authorize-spotify.js
- Follow the link provided in the terminal (http://localhost:8888/login) and authorize your Spotify app by clicking "AGREE."
- Back in the terminal, copy the access_token and refresh_token to
.env
file'sS_ACCESS_TOKEN
andS_REFRESH TOKEN
fields respectively. - Create the Spotify playlist you intend to use for posting (for example, "Post to Reddit"), and navigate to it in a browser. The url will look like this:
https://open.spotify.com/playlist/PLAYLIST_ID
Copy the value in place of PLAYLIST_ID to the S_PLAYLIST
key in the .env
.
- Log into the Google Cloud console and create a new project.
- Click the Enable APIs and Services button, then scroll down and click on YouTube Data API v3. On the next page, click Enable.
- On the YouTube Data API v3 page, click Create credentials. On the next page, select Public Data, and click Next. Copy the API key value then click Done. Paste the API key in the
Y_API_KEY
field in the.env
.
- Copy the Reddit username and password for the account you wish to use to the
R_USER
andR_PASS
values in the.env
. - Go to the Reddit API page and create a new app, pasting the following into the redirect URI field:
https://not-an-aardvark.github.io/reddit-oauth-helper/
- On the Reddit app preferences page, locate your app's Client Id. This should be a code written under the name of your app, as highlighted in this screenshot:
- Copy this value, along with the secret value from the preference page to the
R_CLIENT_ID
andR_CLIENT_SECRET
fields of the.env
file. - Open Reddit OAuth Helper, copy the previous values to the matching fields, check your desired scopes (you can just check all), check Permanent, and click Generate tokens.
(Note: The Reddit OAuth Helper page may not work on certain browsers; if it says "loading..." for more than a few seconds, try another browser.)
- When prompted, click Allow to connect the app to your Reddit account.
- Back on the Reddit OAuth Helper page, copy the newly generated Refresh token and Access token values to the
R_REFRESH_TOKEN
andR_ACCESS_TOKEN
fields in the.env
.
- Set your target subreddit and post interval in the
.env
file. - Start the script with
node beetbot.js
, and it will run continuously, attempting to post every time the posting interval has elapsed.
Optionally, use something like Forever to ensure that the script runs forever and is restarted in the event of a crash.
To install and run the script using Forever, run the following commands:
npm install forever
forever start beetbot.js
(Remember to include the operation of this script in your will, in case it continues to run after you have passed away.)
That's it (phew)! Now any time you add a song to your Spotify playlist, it will get posted to Reddit.
Happy posting!