diff options
| author | Max Isom <[email protected]> | 2021-09-19 19:50:25 -0400 |
|---|---|---|
| committer | Max Isom <[email protected]> | 2021-09-19 19:50:25 -0400 |
| commit | efcdeb78c8b690bc544dac1ed0be96a6693bcff6 (patch) | |
| tree | d4a453f92d928613c2f4209af7475190a093321d /src/services/third-party.ts | |
| parent | 79e7e88fab1ce05cf84abeba2e05300a93b4759c (diff) | |
| download | muse-efcdeb78c8b690bc544dac1ed0be96a6693bcff6.tar.xz muse-efcdeb78c8b690bc544dac1ed0be96a6693bcff6.zip | |
Reorg third party services & config
Diffstat (limited to 'src/services/third-party.ts')
| -rw-r--r-- | src/services/third-party.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/services/third-party.ts b/src/services/third-party.ts new file mode 100644 index 0000000..00b9269 --- /dev/null +++ b/src/services/third-party.ts @@ -0,0 +1,36 @@ +import {inject, injectable} from 'inversify'; +import SpotifyWebApi from 'spotify-web-api-node'; +import Youtube from 'youtube.ts'; +import {TYPES} from '../types'; +import Config from './config'; + +@injectable() +export default class ThirdParty { + readonly youtube: Youtube; + readonly spotify: SpotifyWebApi; + + private spotifyTokenTimerId?: NodeJS.Timeout; + + constructor(@inject(TYPES.Config) config: Config) { + this.youtube = new Youtube(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() { + const auth = await this.spotify.clientCredentialsGrant(); + this.spotify.setAccessToken(auth.body.access_token); + + this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken, (auth.body.expires_in / 2) * 1000); + } +} |
