apis.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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) => {
  74. next(null, songs);
  75. })
  76. .catch(next);
  77. },
  78. ],
  79. (songs) => {
  80. console.log(
  81. "SUCCESS",
  82. "APIS_GET_SPOTIFY_SONGS",
  83. `User "${session.userId}" got Spotify songs for title "${title}" successfully.`
  84. );
  85. cb({ status: "success", songs: songs });
  86. }
  87. );
  88. }),
  89. /**
  90. * Gets Discogs data
  91. *
  92. * @param session
  93. * @param query - the query
  94. * @param cb
  95. */
  96. searchDiscogs: hooks.adminRequired((session, query, page, cb) => {
  97. async.waterfall(
  98. [
  99. (next) => {
  100. const params = [
  101. `q=${encodeURIComponent(query)}`,
  102. `per_page=20`,
  103. `page=${page}`,
  104. ].join("&");
  105. const options = {
  106. url: `https://api.discogs.com/database/search?${params}`,
  107. headers: {
  108. "User-Agent": "Request",
  109. Authorization: `Discogs key=${config.get(
  110. "apis.discogs.client"
  111. )}, secret=${config.get("apis.discogs.secret")}`,
  112. },
  113. };
  114. request(options, (err, res, body) => {
  115. if (err) next(err);
  116. body = JSON.parse(body);
  117. next(null, body);
  118. if (body.error) next(body.error);
  119. });
  120. },
  121. ],
  122. async (err, body) => {
  123. if (err) {
  124. err = await utils.runJob("GET_ERROR", { error: err });
  125. console.log(
  126. "ERROR",
  127. "APIS_SEARCH_DISCOGS",
  128. `Searching discogs failed with query "${query}". "${err}"`
  129. );
  130. return cb({ status: "failure", message: err });
  131. }
  132. console.log(
  133. "SUCCESS",
  134. "APIS_SEARCH_DISCOGS",
  135. `User "${session.userId}" searched Discogs succesfully for query "${query}".`
  136. );
  137. cb({
  138. status: "success",
  139. results: body.results,
  140. pages: body.pagination.pages,
  141. });
  142. }
  143. );
  144. }),
  145. /**
  146. * Joins a room
  147. *
  148. * @param session
  149. * @param page - the room to join
  150. * @param cb
  151. */
  152. joinRoom: (session, page, cb) => {
  153. if (page === "home") {
  154. utils.runJob("SOCKET_JOIN_ROOM", {
  155. socketId: session.socketId,
  156. room: page,
  157. });
  158. }
  159. cb({});
  160. },
  161. /**
  162. * Joins an admin room
  163. *
  164. * @param session
  165. * @param page - the admin room to join
  166. * @param cb
  167. */
  168. joinAdminRoom: hooks.adminRequired((session, page, cb) => {
  169. if (
  170. page === "queue" ||
  171. page === "songs" ||
  172. page === "stations" ||
  173. page === "reports" ||
  174. page === "news" ||
  175. page === "users" ||
  176. page === "statistics" ||
  177. page === "punishments"
  178. ) {
  179. utils.runJob("SOCKET_JOIN_ROOM", {
  180. socketId: session.socketId,
  181. room: `admin.${page}`,
  182. });
  183. }
  184. cb({});
  185. }),
  186. /**
  187. * Returns current date
  188. *
  189. * @param session
  190. * @param cb
  191. */
  192. ping: (session, cb) => {
  193. cb({ date: Date.now() });
  194. },
  195. };