apis.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Joins a room
  65. *
  66. * @param session
  67. * @param page - the room to join
  68. * @param cb
  69. */
  70. joinRoom: (session, page, cb) => {
  71. if (page === 'home') {
  72. utils.socketJoinRoom(session.socketId, page);
  73. }
  74. cb({});
  75. },
  76. /**
  77. * Joins an admin room
  78. *
  79. * @param session
  80. * @param page - the admin room to join
  81. * @param cb
  82. */
  83. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  84. if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics') {
  85. utils.socketJoinRoom(session.socketId, `admin.${page}`);
  86. }
  87. cb({});
  88. }),
  89. /**
  90. * Returns current date
  91. *
  92. * @param session
  93. * @param cb
  94. */
  95. ping: (session, cb) => {
  96. cb({date: Date.now()});
  97. }
  98. };