@bolajiolajide/now-playing
v1.0.0
Published
A simple package to get the current playing song from a streaming platform.
Downloads
20
Maintainers
Readme
Now Playing
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.
Installation
To install the package, use your preferred package manager:
# npm
npm install @BolajiOlajide/now-playing
# yarn
yarn add @BolajiOlajide/now-playing
Usage
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
})
Storage
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
}
Providers
Spotify
You need three things.
- Spotify Client ID
- Spotify Client Secret
- Spotify Refresh Token
To get your Spotify Client ID and sercet, follow these steps:
- Log in to your Spotify Dashboard.
- Create a new app by clicking on the "Create an App" button. You can name your app whatever you like.
- After creating the app, you will see the details page for your app. Here, you can find your client ID and client secret.
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.
Contributing
We welcome contributions from the community! To contribute:
- Fork the repository.
- Create a new branch with a descriptive name.
- Make your changes and commit them with a descriptive message.
- Push your changes to your forked repository.
- Open a pull request to the main repository.