apis.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = require("../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. [
  28. (next) => {
  29. request(
  30. `https://www.googleapis.com/youtube/v3/search?${params}`,
  31. next
  32. );
  33. },
  34. (res, body, next) => {
  35. next(null, JSON.parse(body));
  36. },
  37. ],
  38. async (err, data) => {
  39. console.log(data.error);
  40. if (err || data.error) {
  41. if (!err) err = data.error.message;
  42. err = await utils.runJob("GET_ERROR", { error: err });
  43. console.log(
  44. "ERROR",
  45. "APIS_SEARCH_YOUTUBE",
  46. `Searching youtube failed with query "${query}". "${err}"`
  47. );
  48. return cb({ status: "failure", message: err });
  49. }
  50. console.log(
  51. "SUCCESS",
  52. "APIS_SEARCH_YOUTUBE",
  53. `Searching YouTube successful with query "${query}".`
  54. );
  55. return cb({ status: "success", data });
  56. }
  57. );
  58. },
  59. /**
  60. * Gets Spotify data
  61. *
  62. * @param session
  63. * @param title - the title of the song
  64. * @param artist - an artist for that song
  65. * @param cb
  66. */
  67. getSpotifySongs: hooks.adminRequired((session, title, artist, cb) => {
  68. async.waterfall(
  69. [
  70. (next) => {
  71. utils
  72. .runJob("GET_SONGS_FROM_SPOTIFY", { title, artist })
  73. .then((songs) => next(null, songs))
  74. .catch(next);
  75. },
  76. ],
  77. (songs) => {
  78. console.log(
  79. "SUCCESS",
  80. "APIS_GET_SPOTIFY_SONGS",
  81. `User "${session.userId}" got Spotify songs for title "${title}" successfully.`
  82. );
  83. cb({ status: "success", songs: songs });
  84. }
  85. );
  86. }),
  87. /**
  88. * Gets Discogs data
  89. *
  90. * @param session
  91. * @param query - the query
  92. * @param cb
  93. */
  94. searchDiscogs: hooks.adminRequired((session, query, page, cb) => {
  95. async.waterfall(
  96. [
  97. (next) => {
  98. const params = [
  99. `q=${encodeURIComponent(query)}`,
  100. `per_page=20`,
  101. `page=${page}`,
  102. ].join("&");
  103. const options = {
  104. url: `https://api.discogs.com/database/search?${params}`,
  105. headers: {
  106. "User-Agent": "Request",
  107. Authorization: `Discogs key=${config.get(
  108. "apis.discogs.client"
  109. )}, secret=${config.get("apis.discogs.secret")}`,
  110. },
  111. };
  112. request(options, (err, res, body) => {
  113. if (err) next(err);
  114. body = JSON.parse(body);
  115. next(null, body);
  116. if (body.error) next(body.error);
  117. });
  118. },
  119. ],
  120. async (err, body) => {
  121. if (err) {
  122. err = await utils.runJob("GET_ERROR", { error: err });
  123. console.log(
  124. "ERROR",
  125. "APIS_SEARCH_DISCOGS",
  126. `Searching discogs failed with query "${query}". "${err}"`
  127. );
  128. return cb({ status: "failure", message: err });
  129. }
  130. console.log(
  131. "SUCCESS",
  132. "APIS_SEARCH_DISCOGS",
  133. `User "${session.userId}" searched Discogs succesfully for query "${query}".`
  134. );
  135. cb({
  136. status: "success",
  137. results: body.results,
  138. pages: body.pagination.pages,
  139. });
  140. }
  141. );
  142. }),
  143. /**
  144. * Joins a room
  145. *
  146. * @param session
  147. * @param page - the room to join
  148. * @param cb
  149. */
  150. joinRoom: (session, page, cb) => {
  151. if (page === "home") {
  152. utils.runJob("SOCKET_JOIN_ROOM", {
  153. socketId: session.socketId,
  154. room: page,
  155. });
  156. }
  157. cb({});
  158. },
  159. /**
  160. * Joins an admin room
  161. *
  162. * @param session
  163. * @param page - the admin room to join
  164. * @param cb
  165. */
  166. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  167. if (
  168. page === "queue" ||
  169. page === "songs" ||
  170. page === "stations" ||
  171. page === "reports" ||
  172. page === "news" ||
  173. page === "users" ||
  174. page === "statistics" ||
  175. page === "punishments"
  176. ) {
  177. utils.runJob("SOCKET_JOIN_ROOM", {
  178. socketId: session.socketId,
  179. room: `admin.${page}`,
  180. });
  181. }
  182. cb({});
  183. }),
  184. /**
  185. * Returns current date
  186. *
  187. * @param session
  188. * @param cb
  189. */
  190. ping: (session, cb) => {
  191. cb({ date: Date.now() });
  192. },
  193. };