/BeetBot

Automatically post music from a Spotify playlist to Reddit.

Primary LanguageJavaScriptMIT LicenseMIT

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.

Setup and run locally

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.

Connect and authorize a Spotify App

  1. Create a Spotify app. During setup, set the Redirect URI to http://localhost:8888/callback, and check the "Web API" box.
  2. Navigate to your app's settings, and copy the Client Id and Client Secret values to S_CLIENT_ID and S_CLIENT_SECRET in the .env file.
  3. In your terminal, run the following from the project directory:
node authorize-spotify.js  
  1. Follow the link provided in the terminal (http://localhost:8888/login) and authorize your Spotify app by clicking "AGREE."
  2. Back in the terminal, copy the access_token and refresh_token to .env file's S_ACCESS_TOKEN and S_REFRESH TOKEN fields respectively.
  3. 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.

Connect the YouTube API

  1. Log into the Google Cloud console and create a new project.
  2. Click the Enable APIs and Services button, then scroll down and click on YouTube Data API v3. On the next page, click Enable.
  3. 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.

Connect the Reddit API

  1. Copy the Reddit username and password for the account you wish to use to the R_USER and R_PASS values in the .env.
  2. 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/
  1. 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:

reddit-client-id

  1. Copy this value, along with the secret value from the preference page to the R_CLIENT_ID and R_CLIENT_SECRET fields of the .env file.
  2. 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.)

  1. When prompted, click Allow to connect the app to your Reddit account.
  2. Back on the Reddit OAuth Helper page, copy the newly generated Refresh token and Access token values to the R_REFRESH_TOKEN and R_ACCESS_TOKEN fields in the .env.

Usage

  1. Set your target subreddit and post interval in the .env file.
  2. 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!