apis.js 4.5 KB

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