2
0

apis.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 params1 = [
  19. 'part=snippet',
  20. `q=${encodeURIComponent(query)}`,
  21. `key=${config.get('apis.youtube.key')}`,
  22. 'type=video',
  23. 'maxResults=15'
  24. ].join('&');
  25. let params2 = [
  26. 'part=contentDetails',
  27. `key=${config.get('apis.youtube.key')}`,
  28. 'fields=etag,items/contentDetails/duration'
  29. ];
  30. async.waterfall([
  31. (next) => {
  32. request(`https://www.googleapis.com/youtube/v3/search?${params1}`, next);
  33. },
  34. (res, body, next) => {
  35. next(null, JSON.parse(body));
  36. },
  37. (body, next) => {
  38. let ids = [];
  39. body.items.forEach((item) => {
  40. ids.push(item.id.videoId);
  41. });
  42. ids = ids.join(',');
  43. params2.push(`id=${ids}`);
  44. params2 = params2.join('&');
  45. request(`https://www.googleapis.com/youtube/v3/videos?${params2}`, (err, res, body2) => {
  46. next(err, body2, body);
  47. });
  48. },
  49. (durationBody, body, next) => {
  50. durationBody = JSON.parse(durationBody);
  51. body.items = body.items.map((item, index) => {
  52. let dur = durationBody.items[index].contentDetails.duration;
  53. dur = dur.replace('PT', '');
  54. let duration = 0;
  55. dur = dur.replace(/([\d]*)H/, (v, v2) => {
  56. v2 = Number(v2);
  57. duration = (v2 * 60 * 60);
  58. return '';
  59. });
  60. dur = dur.replace(/([\d]*)M/, (v, v2) => {
  61. v2 = Number(v2);
  62. duration += (v2 * 60);
  63. return '';
  64. });
  65. dur = dur.replace(/([\d]*)S/, (v, v2) => {
  66. v2 = Number(v2);
  67. duration += v2;
  68. return '';
  69. });
  70. item.duration = duration;
  71. return item;
  72. });
  73. next(null, body);
  74. }
  75. ], (err, data) => {
  76. if (err) {
  77. err = utils.getError(err);
  78. logger.error("APIS_SEARCH_YOUTUBE", `Searching youtube failed with query "${query}". "${err}"`);
  79. return cb({status: 'failure', message: err});
  80. }
  81. logger.success("APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  82. return cb({ status: 'success', data });
  83. });
  84. },
  85. /**
  86. * Gets Spotify data
  87. *
  88. * @param session
  89. * @param title - the title of the song
  90. * @param artist - an artist for that song
  91. * @param cb
  92. */
  93. getSpotifySongs: hooks.adminRequired((session, title, artist, cb, userId) => {
  94. async.waterfall([
  95. (next) => {
  96. utils.getSongsFromSpotify(title, artist, next);
  97. }
  98. ], (songs) => {
  99. logger.success('APIS_GET_SPOTIFY_SONGS', `User "${userId}" got Spotify songs for title "${title}" successfully.`);
  100. cb({status: 'success', songs: songs});
  101. });
  102. }),
  103. /**
  104. * Joins a room
  105. *
  106. * @param session
  107. * @param page - the room to join
  108. * @param cb
  109. */
  110. joinRoom: (session, page, cb) => {
  111. if (page === 'home') {
  112. utils.socketJoinRoom(session.socketId, page);
  113. }
  114. cb({});
  115. },
  116. /**
  117. * Joins an admin room
  118. *
  119. * @param session
  120. * @param page - the admin room to join
  121. * @param cb
  122. */
  123. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  124. if (page === 'queue' || page === 'songs' || page === 'stations' || page === 'reports' || page === 'news' || page === 'users' || page === 'statistics') {
  125. utils.socketJoinRoom(session.socketId, `admin.${page}`);
  126. }
  127. cb({});
  128. }),
  129. /**
  130. * Returns current date
  131. *
  132. * @param session
  133. * @param cb
  134. */
  135. ping: (session, cb) => {
  136. cb({date: Date.now()});
  137. }
  138. };