123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- const CoreClass = require("../core.js");
- const config = require("config"),
- async = require("async");
- let apiResults = {
- access_token: "",
- token_type: "",
- expires_in: 0,
- expires_at: 0,
- scope: "",
- };
- class SpotifyModule extends CoreClass {
- constructor() {
- super("spotify");
- }
- initialize() {
- return new Promise((resolve, reject) => {
- this.cache = this.moduleManager.modules["cache"];
- this.utils = this.moduleManager.modules["utils"];
- const client = config.get("apis.spotify.client");
- const secret = config.get("apis.spotify.secret");
- const OAuth2 = require("oauth").OAuth2;
- this.SpotifyOauth = new OAuth2(
- client,
- secret,
- "https://accounts.spotify.com/",
- null,
- "api/token",
- null
- );
- async.waterfall(
- [
- (next) => {
- this.setStage(2);
- this.cache
- .runJob("HGET", { table: "api", key: "spotify" })
- .then((data) => next(null, data))
- .catch(next);
- },
- (data, next) => {
- this.setStage(3);
- if (data) apiResults = data;
- next();
- },
- ],
- async (err) => {
- if (err) {
- err = await this.utils.runJob("GET_ERROR", {
- error: err,
- });
- reject(new Error(err));
- } else {
- resolve();
- }
- }
- );
- });
- }
- GET_TOKEN(payload) {
- return new Promise((resolve, reject) => {
- if (Date.now() > apiResults.expires_at) {
- this.runJob("REQUEST_TOKEN").then(() => {
- resolve(apiResults.access_token);
- });
- } else resolve(apiResults.access_token);
- });
- }
- REQUEST_TOKEN(payload) {
- //cb
- return new Promise((resolve, reject) => {
- async.waterfall(
- [
- (next) => {
- this.log(
- "INFO",
- "SPOTIFY_REQUEST_TOKEN",
- "Requesting new Spotify token."
- );
- this.SpotifyOauth.getOAuthAccessToken(
- "",
- { grant_type: "client_credentials" },
- next
- );
- },
- (access_token, refresh_token, results, next) => {
- apiResults = results;
- apiResults.expires_at =
- Date.now() + results.expires_in * 1000;
- this.cache
- .runJob("HSET", {
- table: "api",
- key: "spotify",
- value: apiResults,
- stringifyJson: true,
- })
- .finally(() => next());
- },
- ],
- () => {
- resolve();
- }
- );
- });
- }
- }
- module.exports = new SpotifyModule();
|