the-playlist-bot

0.1.2 • Public • Published

The Playlist Bot

A bot that curates a playlist on Spotify based on tracks from a given subreddit.

👉👉👉 LIVE DEMO: Link to the "SENSATIONAL CHEESECAKE" Spotify playlist.

The demo uses tracks from /r/HipHopHeads. It's deployed as a Google Cloud App Engine app with cron jobs routinely updating the playlist.

Getting Started

Installation

Run the following command in your console of choice:

npm i the-playlist-bot

The following peer dependencies are required:

npm i snoowrap spotify-web-api-node

Usage

const { Playlist, SubredditListing } = require("the-playlist-bot");
 
// Described in the next section...
const CONFIG  = { /* ... */ },
      BUCKETS = { /* ... */ };
 
const p = new Playlist(CONFIG, BUCKETS);
 
p.getTracksFromSubreddit({ filter: "[FRESH]",               // Uses posts that contain the given string. 
                           sort: SubredditListing.SORT.TOP, // SORT also has HOT and NEW.
                           time: SubredditListing.TIME.DAY, // TIME also has HOUR, WEEK, MONTH, YEAR, and ALL.
                           limit: 1})                       // Set the number of tracks to return (at most).                  
 .then(tracks => p.pushToBucket(0, ...tracks)) 
 // Tracks are objects representing results. They have an "image" (link to album cover) and a "uri" (spotify URI) property. 
 // p.pushToBucket(n, ...tracks) adds the tracks to the given bucket. Check the next section for details.
 
 .then(() => p.update());
 // Replaces the old tracks with the new ones.

The right credentials

You can get the right credentials by creating an app for:

  1. Spotify
  2. Reddit

The CONFIG object should look like this:

{
    REDDIT: {
        USER_AGENT:    "<platform>:<app ID>:<version string> (by /u/<reddit username>)",
        CLIENT_ID:     "14 char long id",
        CLIENT_SECRET: "27 char long secret",
        REFRESH_TOKEN: "your refresh token",
        SUB:           "subredditname"
    },
    SPOTIFY: {      
        PLAYLIST_ID:   "22 char long playlist id",
        CLIENT_ID:     "32 char long client id",
        CLIENT_SECRET: "a 32 char long secret",
        ACCESS_TOKEN:  "your access token",
        REFRESH_TOKEN: "another refresh token"
    }
}

The playlist is an array of "buckets". Buckets are parts of the playlist that define their own track positions and contain their own Spotify URIs. This allows you to fetch tracks from Reddit in a customized way and add those tracks to a customized portion of the playlist.

Buckets are FIFO, meaning that new tracks are added to the top while the oldest tracks are removed.

For example, you can get new posts and add those to a bucket that only affects the first, second, and fourth track in the playlist. BUCKETS could look like this:

[ 
    [
        [0, 2], [4,5]
    ],
    [
        [3,4]
    ]
]

The resulting playlist will look like this:

  1. 1st track from first bucket
  2. 2nd track from first bucket
  3. 1st track from second bucket
  4. 3rd track from first bucket

Playlist objects can be serialized. Calling JSON.stringify on a playlist object returns a JSON that can be used as the BUCKETS argument.

Dependents (0)

Package Sidebar

Install

npm i the-playlist-bot

Weekly Downloads

3

Version

0.1.2

License

UNLICENSED

Unpacked Size

12.4 kB

Total Files

8

Last publish

Collaborators

  • alexandermladenovic