2021-09-20 01:50:25 +02:00
|
|
|
import {inject, injectable} from 'inversify';
|
|
|
|
import SpotifyWebApi from 'spotify-web-api-node';
|
2021-09-20 04:04:34 +02:00
|
|
|
import Youtube from 'youtube.ts/dist/youtube.js';
|
|
|
|
import {TYPES} from '../types.js';
|
|
|
|
import Config from './config.js';
|
2021-09-20 01:50:25 +02:00
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export default class ThirdParty {
|
|
|
|
readonly youtube: Youtube;
|
|
|
|
readonly spotify: SpotifyWebApi;
|
|
|
|
|
|
|
|
private spotifyTokenTimerId?: NodeJS.Timeout;
|
|
|
|
|
|
|
|
constructor(@inject(TYPES.Config) config: Config) {
|
2021-09-20 04:04:34 +02:00
|
|
|
// Library is transpiled incorrectly
|
|
|
|
this.youtube = new ((Youtube as any).default)(config.YOUTUBE_API_KEY);
|
2021-09-20 01:50:25 +02:00
|
|
|
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() {
|
|
|
|
const auth = await this.spotify.clientCredentialsGrant();
|
|
|
|
this.spotify.setAccessToken(auth.body.access_token);
|
|
|
|
|
|
|
|
this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken, (auth.body.expires_in / 2) * 1000);
|
|
|
|
}
|
|
|
|
}
|