forked from museofficial/muse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththird-party.ts
40 lines (34 loc) · 1.19 KB
/
third-party.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import {inject, injectable} from 'inversify';
import SpotifyWebApi from 'spotify-web-api-node';
import Youtube from 'youtube.ts';
import pRetry from 'p-retry';
import {TYPES} from '../types.js';
import Config from './config.js';
@injectable()
export default class ThirdParty {
readonly youtube: Youtube;
readonly spotify: SpotifyWebApi;
private spotifyTokenTimerId?: NodeJS.Timeout;
constructor(@inject(TYPES.Config) config: Config) {
// Library is transpiled incorrectly
// eslint-disable-next-line
this.youtube = new ((Youtube as any).default)(config.YOUTUBE_API_KEY);
this.spotify = new SpotifyWebApi({
clientId: config.SPOTIFY_CLIENT_ID,
clientSecret: config.SPOTIFY_CLIENT_SECRET,
});
void this.refreshSpotifyToken();
}
cleanup() {
if (this.spotifyTokenTimerId) {
clearTimeout(this.spotifyTokenTimerId);
}
}
private async refreshSpotifyToken() {
await pRetry(async () => {
const auth = await this.spotify.clientCredentialsGrant();
this.spotify.setAccessToken(auth.body.access_token);
this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken.bind(this), (auth.body.expires_in / 2) * 1000);
}, {retries: 5});
}
}