spotify.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import config from "config";
  2. import async from "async";
  3. import oauth from "oauth";
  4. import CoreClass from "../core";
  5. const { OAuth2 } = oauth;
  6. let apiResults = {
  7. access_token: "",
  8. token_type: "",
  9. expires_in: 0,
  10. expires_at: 0,
  11. scope: ""
  12. };
  13. class SpotifyModule extends CoreClass {
  14. // eslint-disable-next-line require-jsdoc
  15. constructor() {
  16. super("spotify");
  17. }
  18. /**
  19. * Initialises the spotify module
  20. *
  21. * @returns {Promise} - returns promise (reject, resolve)
  22. */
  23. initialize() {
  24. return new Promise((resolve, reject) => {
  25. this.cache = this.moduleManager.modules.cache;
  26. this.utils = this.moduleManager.modules.utils;
  27. const client = config.get("apis.spotify.client");
  28. const secret = config.get("apis.spotify.secret");
  29. this.SpotifyOauth = new OAuth2(client, secret, "https://accounts.spotify.com/", null, "api/token", null);
  30. async.waterfall(
  31. [
  32. next => {
  33. this.setStage(2);
  34. this.cache
  35. .runJob("HGET", { table: "api", key: "spotify" })
  36. .then(data => {
  37. next(null, data);
  38. })
  39. .catch(next);
  40. },
  41. (data, next) => {
  42. this.setStage(3);
  43. if (data) apiResults = data;
  44. next();
  45. }
  46. ],
  47. async err => {
  48. if (err) {
  49. err = await this.utils.runJob("GET_ERROR", {
  50. error: err
  51. });
  52. reject(new Error(err));
  53. } else {
  54. resolve();
  55. }
  56. }
  57. );
  58. });
  59. }
  60. /**
  61. * Returns the request token for the Spotify api if one exists, otherwise creates a new one
  62. *
  63. * @returns {Promise} - returns a promise (resolve, reject)
  64. */
  65. GET_TOKEN() {
  66. return new Promise(resolve => {
  67. if (Date.now() > apiResults.expires_at) {
  68. this.runJob("REQUEST_TOKEN").then(() => {
  69. resolve(apiResults.access_token);
  70. });
  71. } else resolve(apiResults.access_token);
  72. });
  73. }
  74. /**
  75. * Creates a request token for the Spotify api
  76. *
  77. * @returns {Promise} - returns a promise (resolve, reject)
  78. */
  79. REQUEST_TOKEN() {
  80. return new Promise(resolve => {
  81. async.waterfall(
  82. [
  83. next => {
  84. this.log("INFO", "SPOTIFY_REQUEST_TOKEN", "Requesting new Spotify token.");
  85. this.SpotifyOauth.getOAuthAccessToken("", { grant_type: "client_credentials" }, next);
  86. },
  87. (accessToken, refreshToken, results, next) => {
  88. apiResults = results;
  89. apiResults.expires_at = Date.now() + results.expires_in * 1000;
  90. this.cache
  91. .runJob("HSET", {
  92. table: "api",
  93. key: "spotify",
  94. value: apiResults,
  95. stringifyJson: true
  96. })
  97. .finally(() => next());
  98. }
  99. ],
  100. () => resolve()
  101. );
  102. });
  103. }
  104. }
  105. export default new SpotifyModule();