apis.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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) => {
  54. async.waterfall([
  55. (next) => {
  56. utils.getSongsFromSpotify(title, artist, next);
  57. }
  58. ], (songs) => {
  59. logger.success('APIS_GET_SPOTIFY_SONGS', `User "${session.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, page, cb) => {
  71. async.waterfall([
  72. (next) => {
  73. const params = [
  74. `q=${encodeURIComponent(query)}`,
  75. `per_page=20`,
  76. `page=${page}`
  77. ].join('&');
  78. const options = {
  79. url: `https://api.discogs.com/database/search?${params}`,
  80. headers: {
  81. "User-Agent": "Request",
  82. "Authorization": `Discogs key=${config.get("apis.discogs.client")}, secret=${config.get("apis.discogs.secret")}`
  83. }
  84. };
  85. request(options, (err, res, body) => {
  86. if (err) next(err);
  87. body = JSON.parse(body);
  88. next(null, body);
  89. if (body.error) next(body.error);
  90. });
  91. }
  92. ], async (err, body) => {
  93. if (err) {
  94. err = await utils.getError(err);
  95. logger.error("APIS_SEARCH_DISCOGS", `Searching discogs failed with query "${query}". "${err}"`);
  96. return cb({status: 'failure', message: err});
  97. }
  98. logger.success('APIS_SEARCH_DISCOGS', `User "${session.userId}" searched Discogs succesfully for query "${query}".`);
  99. cb({status: 'success', results: body.results, pages: body.pagination.pages});
  100. });
  101. }),
  102. /**
  103. * Joins a room
  104. *
  105. * @param session
  106. * @param page - the room to join
  107. * @param cb
  108. */
  109. joinRoom: (session, page, cb) => {
  110. if (page === 'home') {
  111. utils.socketJoinRoom(session.socketId, page);
  112. }
  113. cb({});
  114. },
  115. /**
  116. * Joins an admin room
  117. *
  118. * @param session
  119. * @param page - the admin room to join
  120. * @param cb
  121. */
  122. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  123. if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics' || page === 'punishments') {
  124. utils.socketJoinRoom(session.socketId, `admin.${page}`);
  125. }
  126. cb({});
  127. }),
  128. /**
  129. * Returns current date
  130. *
  131. * @param session
  132. * @param cb
  133. */
  134. ping: (session, cb) => {
  135. cb({date: Date.now()});
  136. }
  137. };