youtube.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import mongoose from "mongoose";
  2. import async from "async";
  3. import { isAdminRequired, isLoginRequired } from "./hooks";
  4. // eslint-disable-next-line
  5. import moduleManager from "../../index";
  6. const DBModule = moduleManager.modules.db;
  7. const UtilsModule = moduleManager.modules.utils;
  8. const YouTubeModule = moduleManager.modules.youtube;
  9. export default {
  10. /**
  11. * Returns details about the YouTube quota usage
  12. *
  13. * @returns {{status: string, data: object}}
  14. */
  15. getQuotaStatus: isAdminRequired(function getQuotaStatus(session, fromDate, cb) {
  16. YouTubeModule.runJob("GET_QUOTA_STATUS", { fromDate }, this)
  17. .then(response => {
  18. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status was successful.`);
  19. return cb({ status: "success", data: { status: response.status } });
  20. })
  21. .catch(async err => {
  22. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  23. this.log("ERROR", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status failed. "${err}"`);
  24. return cb({ status: "error", message: err });
  25. });
  26. }),
  27. /**
  28. * Gets api requests, used in the admin youtube page by the AdvancedTable component
  29. *
  30. * @param {object} session - the session object automatically added by the websocket
  31. * @param page - the page
  32. * @param pageSize - the size per page
  33. * @param properties - the properties to return for each news item
  34. * @param sort - the sort object
  35. * @param queries - the queries array
  36. * @param operator - the operator for queries
  37. * @param cb
  38. */
  39. getApiRequests: isAdminRequired(async function getApiRequests(
  40. session,
  41. page,
  42. pageSize,
  43. properties,
  44. sort,
  45. queries,
  46. operator,
  47. cb
  48. ) {
  49. async.waterfall(
  50. [
  51. next => {
  52. DBModule.runJob(
  53. "GET_DATA",
  54. {
  55. page,
  56. pageSize,
  57. properties,
  58. sort,
  59. queries,
  60. operator,
  61. modelName: "youtubeApiRequest",
  62. blacklistedProperties: [],
  63. specialProperties: {},
  64. specialQueries: {}
  65. },
  66. this
  67. )
  68. .then(response => {
  69. next(null, response);
  70. })
  71. .catch(err => {
  72. next(err);
  73. });
  74. }
  75. ],
  76. async (err, response) => {
  77. if (err && err !== true) {
  78. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  79. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  80. return cb({ status: "error", message: err });
  81. }
  82. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  83. return cb({
  84. status: "success",
  85. message: "Successfully fetched YouTube api requests.",
  86. data: response
  87. });
  88. }
  89. );
  90. }),
  91. /**
  92. * Returns a specific api request
  93. *
  94. * @returns {{status: string, data: object}}
  95. */
  96. getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
  97. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  98. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  99. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  100. .then(response => {
  101. this.log(
  102. "SUCCESS",
  103. "YOUTUBE_GET_API_REQUEST",
  104. `Getting api request with id ${apiRequestId} was successful.`
  105. );
  106. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  107. })
  108. .catch(async err => {
  109. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  110. this.log(
  111. "ERROR",
  112. "YOUTUBE_GET_API_REQUEST",
  113. `Getting api request with id ${apiRequestId} failed. "${err}"`
  114. );
  115. return cb({ status: "error", message: err });
  116. });
  117. }),
  118. /**
  119. * Reset stored API requests
  120. *
  121. * @returns {{status: string, data: object}}
  122. */
  123. resetStoredApiRequests: isAdminRequired(async function resetStoredApiRequests(session, cb) {
  124. YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
  125. .then(() => {
  126. this.log(
  127. "SUCCESS",
  128. "YOUTUBE_RESET_STORED_API_REQUESTS",
  129. `Resetting stored API requests was successful.`
  130. );
  131. return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
  132. })
  133. .catch(async err => {
  134. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  135. this.log(
  136. "ERROR",
  137. "YOUTUBE_RESET_STORED_API_REQUESTS",
  138. `Resetting stored API requests failed. "${err}"`
  139. );
  140. return cb({ status: "error", message: err });
  141. });
  142. }),
  143. /**
  144. * Remove stored API requests
  145. *
  146. * @returns {{status: string, data: object}}
  147. */
  148. removeStoredApiRequest: isAdminRequired(function removeStoredApiRequest(session, requestId, cb) {
  149. YouTubeModule.runJob("REMOVE_STORED_API_REQUEST", { requestId }, this)
  150. .then(() => {
  151. this.log(
  152. "SUCCESS",
  153. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  154. `Removing stored API request "${requestId}" was successful.`
  155. );
  156. return cb({ status: "success", message: "Successfully removed stored YouTube API request" });
  157. })
  158. .catch(async err => {
  159. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  160. this.log(
  161. "ERROR",
  162. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  163. `Removing stored API request "${requestId}" failed. "${err}"`
  164. );
  165. return cb({ status: "error", message: err });
  166. });
  167. }),
  168. /**
  169. * Gets videos, used in the admin youtube page by the AdvancedTable component
  170. *
  171. * @param {object} session - the session object automatically added by the websocket
  172. * @param page - the page
  173. * @param pageSize - the size per page
  174. * @param properties - the properties to return for each news item
  175. * @param sort - the sort object
  176. * @param queries - the queries array
  177. * @param operator - the operator for queries
  178. * @param cb
  179. */
  180. getVideos: isAdminRequired(async function getVideos(
  181. session,
  182. page,
  183. pageSize,
  184. properties,
  185. sort,
  186. queries,
  187. operator,
  188. cb
  189. ) {
  190. async.waterfall(
  191. [
  192. next => {
  193. DBModule.runJob(
  194. "GET_DATA",
  195. {
  196. page,
  197. pageSize,
  198. properties,
  199. sort,
  200. queries,
  201. operator,
  202. modelName: "youtubeVideo",
  203. blacklistedProperties: [],
  204. specialProperties: {},
  205. specialQueries: {}
  206. },
  207. this
  208. )
  209. .then(response => {
  210. next(null, response);
  211. })
  212. .catch(err => {
  213. next(err);
  214. });
  215. }
  216. ],
  217. async (err, response) => {
  218. if (err && err !== true) {
  219. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  220. this.log("ERROR", "YOUTUBE_GET_VIDEOS", `Failed to get YouTube videos. "${err}"`);
  221. return cb({ status: "error", message: err });
  222. }
  223. this.log("SUCCESS", "YOUTUBE_GET_VIDEOS", `Fetched YouTube videos successfully.`);
  224. return cb({
  225. status: "success",
  226. message: "Successfully fetched YouTube videos.",
  227. data: response
  228. });
  229. }
  230. );
  231. }),
  232. /**
  233. * Get a YouTube video
  234. *
  235. * @returns {{status: string, data: object}}
  236. */
  237. getVideo: isLoginRequired(function getVideo(session, identifier, createMissing, cb) {
  238. YouTubeModule.runJob("GET_VIDEO", { identifier, createMissing }, this)
  239. .then(res => {
  240. this.log("SUCCESS", "YOUTUBE_GET_VIDEO", `Fetching video was successful.`);
  241. return cb({ status: "success", message: "Successfully fetched YouTube video", data: res.video });
  242. })
  243. .catch(async err => {
  244. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  245. this.log("ERROR", "YOUTUBE_GET_VIDEO", `Fetching video failed. "${err}"`);
  246. return cb({ status: "error", message: err });
  247. });
  248. }),
  249. /**
  250. * Remove YouTube videos
  251. *
  252. * @returns {{status: string, data: object}}
  253. */
  254. removeVideos: isAdminRequired(function removeVideos(session, videoIds, cb) {
  255. YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
  256. .then(() => {
  257. this.log("SUCCESS", "YOUTUBE_REMOVE_VIDEOS", `Removing videos was successful.`);
  258. return cb({ status: "success", message: "Successfully removed YouTube videos" });
  259. })
  260. .catch(async err => {
  261. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  262. this.log("ERROR", "YOUTUBE_REMOVE_VIDEOS", `Removing videos failed. "${err}"`);
  263. return cb({ status: "error", message: err });
  264. });
  265. }),
  266. /**
  267. * Requests a set of YouTube videos
  268. *
  269. * @param {object} session - the session object automatically added by the websocket
  270. * @param {string} url - the url of the the YouTube playlist
  271. * @param {boolean} musicOnly - whether to only get music from the playlist
  272. * @param {boolean} musicOnly - whether to return videos
  273. * @param {Function} cb - gets called with the result
  274. */
  275. requestSet: isLoginRequired(function requestSet(session, url, musicOnly, returnVideos, cb) {
  276. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  277. .then(response => {
  278. this.log(
  279. "SUCCESS",
  280. "REQUEST_SET",
  281. `Successfully imported a YouTube playlist to be requested for user "${session.userId}".`
  282. );
  283. return cb({
  284. status: "success",
  285. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  286. videos: returnVideos ? response.videos : null
  287. });
  288. })
  289. .catch(async err => {
  290. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  291. this.log(
  292. "ERROR",
  293. "REQUEST_SET",
  294. `Importing a YouTube playlist to be requested failed for user "${session.userId}". "${err}"`
  295. );
  296. return cb({ status: "error", message: err });
  297. });
  298. })
  299. };