apis.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. console.log(data.error);
  34. if (err || data.error) {
  35. if (!err) err = data.error.message;
  36. err = utils.getError(err);
  37. logger.error("APIS_SEARCH_YOUTUBE", `Searching youtube failed with query "${query}". "${err}"`);
  38. return cb({status: 'failure', message: err});
  39. }
  40. logger.success("APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  41. return cb({ status: 'success', data });
  42. });
  43. },
  44. /**
  45. * Gets Spotify data
  46. *
  47. * @param session
  48. * @param title - the title of the song
  49. * @param artist - an artist for that song
  50. * @param cb
  51. */
  52. getSpotifySongs: hooks.adminRequired((session, title, artist, cb, userId) => {
  53. async.waterfall([
  54. (next) => {
  55. utils.getSongsFromSpotify(title, artist, next);
  56. }
  57. ], (songs) => {
  58. logger.success('APIS_GET_SPOTIFY_SONGS', `User "${userId}" got Spotify songs for title "${title}" successfully.`);
  59. cb({status: 'success', songs: songs});
  60. });
  61. }),
  62. /**
  63. * Joins a room
  64. *
  65. * @param session
  66. * @param page - the room to join
  67. * @param cb
  68. */
  69. joinRoom: (session, page, cb) => {
  70. if (page === 'home') {
  71. utils.socketJoinRoom(session.socketId, page);
  72. }
  73. cb({});
  74. },
  75. /**
  76. * Joins an admin room
  77. *
  78. * @param session
  79. * @param page - the admin room to join
  80. * @param cb
  81. */
  82. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  83. if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics') {
  84. utils.socketJoinRoom(session.socketId, `admin.${page}`);
  85. }
  86. cb({});
  87. }),
  88. /**
  89. * Returns current date
  90. *
  91. * @param session
  92. * @param cb
  93. */
  94. ping: (session, cb) => {
  95. cb({date: Date.now()});
  96. }
  97. };