apis.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. const request = require('request'),
  3. config = require('config'),
  4. async = require('async'),
  5. utils = require('../utils'),
  6. logger = require('../logger'),
  7. hooks = require('./hooks');
  8. module.exports = {
  9. /**
  10. * Fetches a list of songs from Youtubes API
  11. *
  12. * @param session
  13. * @param query - the query we'll pass to youtubes api
  14. * @param cb
  15. * @return {{ status: String, data: Object }}
  16. */
  17. searchYoutube: (session, query, cb) => {
  18. const params = [
  19. 'part=snippet',
  20. `q=${encodeURIComponent(query)}`,
  21. `key=${config.get('apis.youtube.key')}`,
  22. 'type=video',
  23. 'maxResults=15'
  24. ].join('&');
  25. async.waterfall([
  26. (next) => {
  27. request(`https://www.googleapis.com/youtube/v3/search?${params}`, next);
  28. },
  29. (res, body, next) => {
  30. next(null, JSON.parse(body));
  31. }
  32. ], (err, data) => {
  33. if (err) {
  34. err = utils.getError(err);
  35. logger.error("APIS_SEARCH_YOUTUBE", `Searching youtube failed with query "${query}". "${err}"`);
  36. return cb({status: 'failure', message: err});
  37. }
  38. logger.success("APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  39. return cb({ status: 'success', data });
  40. });
  41. },
  42. /**
  43. * Gets Spotify data
  44. *
  45. * @param session
  46. * @param title - the title of the song
  47. * @param artist - an artist for that song
  48. * @param cb
  49. */
  50. getSpotifySongs: hooks.adminRequired((session, title, artist, cb, userId) => {
  51. async.waterfall([
  52. (next) => {
  53. utils.getSongsFromSpotify(title, artist, next);
  54. }
  55. ], (songs) => {
  56. logger.success('APIS_GET_SPOTIFY_SONGS', `User "${userId}" got Spotify songs for title "${title}" successfully.`);
  57. cb({status: 'success', songs: songs});
  58. });
  59. }),
  60. /**
  61. * Joins a room
  62. *
  63. * @param session
  64. * @param page - the room to join
  65. * @param cb
  66. */
  67. joinRoom: (session, page, cb) => {
  68. if (page === 'home') {
  69. utils.socketJoinRoom(session.socketId, page);
  70. }
  71. cb({});
  72. },
  73. /**
  74. * Joins an admin room
  75. *
  76. * @param session
  77. * @param page - the admin room to join
  78. * @param cb
  79. */
  80. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  81. if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics') {
  82. utils.socketJoinRoom(session.socketId, `admin.${page}`);
  83. }
  84. cb({});
  85. }),
  86. /**
  87. * Returns current date
  88. *
  89. * @param session
  90. * @param cb
  91. */
  92. ping: (session, cb) => {
  93. cb({date: Date.now()});
  94. }
  95. };