apis.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import config from "config";
  2. import async from "async";
  3. import request from "request";
  4. import { isAdminRequired } from "./hooks";
  5. import moduleManager from "../../index";
  6. const UtilsModule = moduleManager.modules.utils;
  7. const IOModule = moduleManager.modules.io;
  8. export default {
  9. /**
  10. * Fetches a list of songs from Youtubes API
  11. *
  12. * @param {object} session - user session
  13. * @param {string} query - the query we'll pass to youtubes api
  14. * @param {Function} cb - callback
  15. * @returns {{status: string, data: object}} - returns an 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. return async.waterfall(
  26. [
  27. next => {
  28. request(`https://www.googleapis.com/youtube/v3/search?${params}`, next);
  29. },
  30. (res, body, next) => {
  31. next(null, JSON.parse(body));
  32. }
  33. ],
  34. async (err, data) => {
  35. console.log(data.error);
  36. if (err || data.error) {
  37. if (!err) err = data.error.message;
  38. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  39. this.log(
  40. "ERROR",
  41. "APIS_SEARCH_YOUTUBE",
  42. `Searching youtube failed with query "${query}". "${err}"`
  43. );
  44. return cb({ status: "failure", message: err });
  45. }
  46. this.log("SUCCESS", "APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  47. return cb({ status: "success", data });
  48. }
  49. );
  50. },
  51. /**
  52. * Gets Discogs data
  53. *
  54. * @param session
  55. * @param query - the query
  56. * @param {Function} cb
  57. */
  58. searchDiscogs: isAdminRequired(function searchDiscogs(session, query, page, cb) {
  59. async.waterfall(
  60. [
  61. next => {
  62. const params = [`q=${encodeURIComponent(query)}`, `per_page=20`, `page=${page}`].join("&");
  63. const options = {
  64. url: `https://api.discogs.com/database/search?${params}`,
  65. headers: {
  66. "User-Agent": "Request",
  67. Authorization: `Discogs key=${config.get("apis.discogs.client")}, secret=${config.get(
  68. "apis.discogs.secret"
  69. )}`
  70. }
  71. };
  72. request(options, (err, res, body) => {
  73. if (err) next(err);
  74. body = JSON.parse(body);
  75. next(null, body);
  76. if (body.error) next(body.error);
  77. });
  78. }
  79. ],
  80. async (err, body) => {
  81. if (err) {
  82. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  83. this.log(
  84. "ERROR",
  85. "APIS_SEARCH_DISCOGS",
  86. `Searching discogs failed with query "${query}". "${err}"`
  87. );
  88. return cb({ status: "failure", message: err });
  89. }
  90. this.log(
  91. "SUCCESS",
  92. "APIS_SEARCH_DISCOGS",
  93. `User "${session.userId}" searched Discogs succesfully for query "${query}".`
  94. );
  95. return cb({
  96. status: "success",
  97. results: body.results,
  98. pages: body.pagination.pages
  99. });
  100. }
  101. );
  102. }),
  103. /**
  104. * Joins a room
  105. *
  106. * @param {object} session - user session
  107. * @param {string} page - the room to join
  108. * @param {Function} cb - callback
  109. */
  110. joinRoom(session, page, cb) {
  111. if (page === "home" || page.startsWith("profile-")) {
  112. IOModule.runJob("SOCKET_JOIN_ROOM", {
  113. socketId: session.socketId,
  114. room: page
  115. })
  116. .then(() => {})
  117. .catch(err => {
  118. this.log("ERROR", `Joining room failed: ${err.message}`);
  119. });
  120. }
  121. cb({});
  122. },
  123. /**
  124. * Joins an admin room
  125. *
  126. * @param {object} session - user session
  127. * @param {string} page - the admin room to join
  128. * @param {Function} cb - callback
  129. */
  130. joinAdminRoom: isAdminRequired((session, page, cb) => {
  131. if (
  132. page === "queue" ||
  133. page === "songs" ||
  134. page === "stations" ||
  135. page === "reports" ||
  136. page === "news" ||
  137. page === "users" ||
  138. page === "statistics" ||
  139. page === "punishments"
  140. ) {
  141. IOModule.runJob("SOCKET_JOIN_ROOM", {
  142. socketId: session.socketId,
  143. room: `admin.${page}`
  144. });
  145. }
  146. cb({});
  147. }),
  148. /**
  149. * Returns current date
  150. *
  151. * @param {object} session - user session
  152. * @param {Function} cb - callback
  153. */
  154. ping(session, cb) {
  155. cb({ date: Date.now() });
  156. }
  157. };