A simple package to get the current playing song from a streaming platform.
Collaborative effort with @lilpolymath to create a library that can be used to display the currently playing song from a user's streaming platform.
This library is built to be generic to work with multiple providers and storage mechanisms.
To install the package, use your preferred package manager:
# npm
npm install @BolajiOlajide/now-playing
# yarn
yarn add @BolajiOlajide/now-playing
import { NowPlaying, Providers, type IStorer } from '@BolajiOlajide/now-playing'
let myCustomStorer: IStorer
const np = new NowPlaying(Providers.SPOTIFY, {
useCache: false, // (optional) default is true
cacheDuration: 30000, // (optional) in milliseconds
streamerArgs: {
clientId: 'foo',
clientSecret: 'bar',
refreshToken: 'baz',
},
storer: myCustomStorer, // (optional) custom storage implementation
})
We default to inmemory storage for saving information gotten via the API, (e.g Access tokens, Song data e.t.c).
However you can pass in your own custom storage object, provided it satisfies the IStorer
interface.
interface IStorer {
set<T>(key: string, value: T, duration: number): void
get<T>(key: string): T | undefined
delete(key: string): boolean
has(key: string): boolean
clear(): void
pruneExpiredEntries(): void
}
You need three things.
To get your Spotify Client ID and sercet, follow these steps:
You'll need these credentials to generate a refresh token for Spotify.
Spotify's access token expires every hour, so you will need a refresh token to obtain a new one automatically. To get your refresh token, refer to the following articles for a detailed guide:
Make sure to include the following scopes: "user-read-currently-playing" and "user-read-recently-played" when setting up the authorization process.
We welcome contributions from the community! To contribute: