apis.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. const request = require("request");
  3. const config = require("config");
  4. const async = require("async");
  5. const hooks = require('./hooks');
  6. const moduleManager = require("../../index");
  7. const utils = moduleManager.modules["utils"];
  8. const logger = moduleManager.modules["logger"];
  9. module.exports = {
  10. /**
  11. * Fetches a list of songs from Youtubes API
  12. *
  13. * @param session
  14. * @param query - the query we'll pass to youtubes api
  15. * @param cb
  16. * @return {{ status: String, data: Object }}
  17. */
  18. searchYoutube: (session, query, cb) => {
  19. const params = [
  20. 'part=snippet',
  21. `q=${encodeURIComponent(query)}`,
  22. `key=${config.get('apis.youtube.key')}`,
  23. 'type=video',
  24. 'maxResults=15'
  25. ].join('&');
  26. async.waterfall([
  27. (next) => {
  28. request(`https://www.googleapis.com/youtube/v3/search?${params}`, next);
  29. },
  30. (res, body, next) => {
  31. next(null, JSON.parse(body));
  32. }
  33. ], async (err, data) => {
  34. console.log(data.error);
  35. if (err || data.error) {
  36. if (!err) err = data.error.message;
  37. err = await utils.getError(err);
  38. logger.error("APIS_SEARCH_YOUTUBE", `Searching youtube failed with query "${query}". "${err}"`);
  39. return cb({status: 'failure', message: err});
  40. }
  41. logger.success("APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  42. return cb({ status: 'success', data });
  43. });
  44. },
  45. /**
  46. * Gets Spotify data
  47. *
  48. * @param session
  49. * @param title - the title of the song
  50. * @param artist - an artist for that song
  51. * @param cb
  52. */
  53. getSpotifySongs: hooks.adminRequired((session, title, artist, cb, userId) => {
  54. async.waterfall([
  55. (next) => {
  56. utils.getSongsFromSpotify(title, artist, next);
  57. }
  58. ], (songs) => {
  59. logger.success('APIS_GET_SPOTIFY_SONGS', `User "${userId}" got Spotify songs for title "${title}" successfully.`);
  60. cb({status: 'success', songs: songs});
  61. });
  62. }),
  63. /**
  64. * Gets Discogs data
  65. *
  66. * @param session
  67. * @param query - the query
  68. * @param cb
  69. */
  70. searchDiscogs: hooks.adminRequired((session, query, cb, userId) => {
  71. async.waterfall([
  72. (next) => {
  73. const params = [
  74. `q=${encodeURIComponent(query)}`,
  75. `per_page=20`
  76. ].join('&');
  77. const options = {
  78. url: `https://api.discogs.com/database/search?${params}`,
  79. headers: {
  80. "User-Agent": "Request",
  81. "Authorization": `Discogs key=${config.get("apis.discogs.client")}, secret=${config.get("apis.discogs.secret")}`
  82. }
  83. };
  84. request(options, (err, res, body) => {
  85. if (err) next(err);
  86. body = JSON.parse(body);
  87. next(null, body.results);
  88. if (body.error) next(body.error);
  89. });
  90. }
  91. ], async (err, results) => {
  92. if (err) {
  93. err = await utils.getError(err);
  94. logger.error("APIS_SEARCH_DISCOGS", `Searching discogs failed with query "${query}". "${err}"`);
  95. return cb({status: 'failure', message: err});
  96. }
  97. logger.success('APIS_SEARCH_DISCOGS', `User "${userId}" searched Discogs succesfully for query "${query}".`);
  98. cb({status: 'success', results});
  99. });
  100. }),
  101. /**
  102. * Joins a room
  103. *
  104. * @param session
  105. * @param page - the room to join
  106. * @param cb
  107. */
  108. joinRoom: (session, page, cb) => {
  109. if (page === 'home') {
  110. utils.socketJoinRoom(session.socketId, page);
  111. }
  112. cb({});
  113. },
  114. /**
  115. * Joins an admin room
  116. *
  117. * @param session
  118. * @param page - the admin room to join
  119. * @param cb
  120. */
  121. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  122. if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics' || page === 'punishments') {
  123. utils.socketJoinRoom(session.socketId, `admin.${page}`);
  124. }
  125. cb({});
  126. }),
  127. /**
  128. * Returns current date
  129. *
  130. * @param session
  131. * @param cb
  132. */
  133. ping: (session, cb) => {
  134. cb({date: Date.now()});
  135. }
  136. };