youtube.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 CacheModule = moduleManager.modules.cache;
  8. const UtilsModule = moduleManager.modules.utils;
  9. const YouTubeModule = moduleManager.modules.youtube;
  10. export default {
  11. /**
  12. * Returns details about the YouTube quota usage
  13. *
  14. * @returns {{status: string, data: object}}
  15. */
  16. getQuotaStatus: isAdminRequired(function getQuotaStatus(session, fromDate, cb) {
  17. YouTubeModule.runJob("GET_QUOTA_STATUS", { fromDate }, this)
  18. .then(response => {
  19. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status was successful.`);
  20. return cb({ status: "success", data: { status: response.status } });
  21. })
  22. .catch(async err => {
  23. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  24. this.log("ERROR", "YOUTUBE_GET_QUOTA_STATUS", `Getting quota status failed. "${err}"`);
  25. return cb({ status: "error", message: err });
  26. });
  27. }),
  28. /**
  29. * Returns YouTube quota chart data
  30. *
  31. * @param {object} session - the session object automatically added by the websocket
  32. * @param timePeriod - either hours or days
  33. * @param startDate - beginning date
  34. * @param endDate - end date
  35. * @param dataType - either usage or count
  36. * @returns {{status: string, data: object}}
  37. */
  38. getQuotaChartData: isAdminRequired(function getQuotaChartData(
  39. session,
  40. timePeriod,
  41. startDate,
  42. endDate,
  43. dataType,
  44. cb
  45. ) {
  46. YouTubeModule.runJob(
  47. "GET_QUOTA_CHART_DATA",
  48. { timePeriod, startDate: new Date(startDate), endDate: new Date(endDate), dataType },
  49. this
  50. )
  51. .then(data => {
  52. this.log("SUCCESS", "YOUTUBE_GET_QUOTA_CHART_DATA", `Getting quota chart data was successful.`);
  53. return cb({ status: "success", data });
  54. })
  55. .catch(async err => {
  56. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  57. this.log("ERROR", "YOUTUBE_GET_QUOTA_CHART_DATA", `Getting quota chart data failed. "${err}"`);
  58. return cb({ status: "error", message: err });
  59. });
  60. }),
  61. /**
  62. * Gets api requests, used in the admin youtube page by the AdvancedTable component
  63. *
  64. * @param {object} session - the session object automatically added by the websocket
  65. * @param page - the page
  66. * @param pageSize - the size per page
  67. * @param properties - the properties to return for each news item
  68. * @param sort - the sort object
  69. * @param queries - the queries array
  70. * @param operator - the operator for queries
  71. * @param cb
  72. */
  73. getApiRequests: isAdminRequired(async function getApiRequests(
  74. session,
  75. page,
  76. pageSize,
  77. properties,
  78. sort,
  79. queries,
  80. operator,
  81. cb
  82. ) {
  83. async.waterfall(
  84. [
  85. next => {
  86. DBModule.runJob(
  87. "GET_DATA",
  88. {
  89. page,
  90. pageSize,
  91. properties,
  92. sort,
  93. queries,
  94. operator,
  95. modelName: "youtubeApiRequest",
  96. blacklistedProperties: [],
  97. specialProperties: {},
  98. specialQueries: {}
  99. },
  100. this
  101. )
  102. .then(response => {
  103. next(null, response);
  104. })
  105. .catch(err => {
  106. next(err);
  107. });
  108. }
  109. ],
  110. async (err, response) => {
  111. if (err && err !== true) {
  112. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  113. this.log("ERROR", "YOUTUBE_GET_API_REQUESTS", `Failed to get YouTube api requests. "${err}"`);
  114. return cb({ status: "error", message: err });
  115. }
  116. this.log("SUCCESS", "YOUTUBE_GET_API_REQUESTS", `Fetched YouTube api requests successfully.`);
  117. return cb({
  118. status: "success",
  119. message: "Successfully fetched YouTube api requests.",
  120. data: response
  121. });
  122. }
  123. );
  124. }),
  125. /**
  126. * Returns a specific api request
  127. *
  128. * @returns {{status: string, data: object}}
  129. */
  130. getApiRequest: isAdminRequired(function getApiRequest(session, apiRequestId, cb) {
  131. if (!mongoose.Types.ObjectId.isValid(apiRequestId))
  132. return cb({ status: "error", message: "Api request id is not a valid ObjectId." });
  133. return YouTubeModule.runJob("GET_API_REQUEST", { apiRequestId }, this)
  134. .then(response => {
  135. this.log(
  136. "SUCCESS",
  137. "YOUTUBE_GET_API_REQUEST",
  138. `Getting api request with id ${apiRequestId} was successful.`
  139. );
  140. return cb({ status: "success", data: { apiRequest: response.apiRequest } });
  141. })
  142. .catch(async err => {
  143. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  144. this.log(
  145. "ERROR",
  146. "YOUTUBE_GET_API_REQUEST",
  147. `Getting api request with id ${apiRequestId} failed. "${err}"`
  148. );
  149. return cb({ status: "error", message: err });
  150. });
  151. }),
  152. /**
  153. * Reset stored API requests
  154. *
  155. * @returns {{status: string, data: object}}
  156. */
  157. resetStoredApiRequests: isAdminRequired(async function resetStoredApiRequests(session, cb) {
  158. YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
  159. .then(() => {
  160. this.log(
  161. "SUCCESS",
  162. "YOUTUBE_RESET_STORED_API_REQUESTS",
  163. `Resetting stored API requests was successful.`
  164. );
  165. return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
  166. })
  167. .catch(async err => {
  168. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  169. this.log(
  170. "ERROR",
  171. "YOUTUBE_RESET_STORED_API_REQUESTS",
  172. `Resetting stored API requests failed. "${err}"`
  173. );
  174. return cb({ status: "error", message: err });
  175. });
  176. }),
  177. /**
  178. * Remove stored API requests
  179. *
  180. * @returns {{status: string, data: object}}
  181. */
  182. removeStoredApiRequest: isAdminRequired(function removeStoredApiRequest(session, requestId, cb) {
  183. YouTubeModule.runJob("REMOVE_STORED_API_REQUEST", { requestId }, this)
  184. .then(() => {
  185. this.log(
  186. "SUCCESS",
  187. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  188. `Removing stored API request "${requestId}" was successful.`
  189. );
  190. return cb({ status: "success", message: "Successfully removed stored YouTube API request" });
  191. })
  192. .catch(async err => {
  193. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  194. this.log(
  195. "ERROR",
  196. "YOUTUBE_REMOVE_STORED_API_REQUEST",
  197. `Removing stored API request "${requestId}" failed. "${err}"`
  198. );
  199. return cb({ status: "error", message: err });
  200. });
  201. }),
  202. /**
  203. * Gets videos, used in the admin youtube page by the AdvancedTable component
  204. *
  205. * @param {object} session - the session object automatically added by the websocket
  206. * @param page - the page
  207. * @param pageSize - the size per page
  208. * @param properties - the properties to return for each news item
  209. * @param sort - the sort object
  210. * @param queries - the queries array
  211. * @param operator - the operator for queries
  212. * @param cb
  213. */
  214. getVideos: isAdminRequired(async function getVideos(
  215. session,
  216. page,
  217. pageSize,
  218. properties,
  219. sort,
  220. queries,
  221. operator,
  222. cb
  223. ) {
  224. async.waterfall(
  225. [
  226. next => {
  227. DBModule.runJob(
  228. "GET_DATA",
  229. {
  230. page,
  231. pageSize,
  232. properties,
  233. sort,
  234. queries,
  235. operator,
  236. modelName: "youtubeVideo",
  237. blacklistedProperties: [],
  238. specialProperties: {},
  239. specialQueries: {}
  240. },
  241. this
  242. )
  243. .then(response => {
  244. next(null, response);
  245. })
  246. .catch(err => {
  247. next(err);
  248. });
  249. }
  250. ],
  251. async (err, response) => {
  252. if (err && err !== true) {
  253. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  254. this.log("ERROR", "YOUTUBE_GET_VIDEOS", `Failed to get YouTube videos. "${err}"`);
  255. return cb({ status: "error", message: err });
  256. }
  257. this.log("SUCCESS", "YOUTUBE_GET_VIDEOS", `Fetched YouTube videos successfully.`);
  258. return cb({
  259. status: "success",
  260. message: "Successfully fetched YouTube videos.",
  261. data: response
  262. });
  263. }
  264. );
  265. }),
  266. /**
  267. * Get a YouTube video
  268. *
  269. * @returns {{status: string, data: object}}
  270. */
  271. getVideo: isLoginRequired(function getVideo(session, identifier, createMissing, cb) {
  272. YouTubeModule.runJob("GET_VIDEO", { identifier, createMissing }, this)
  273. .then(res => {
  274. this.log("SUCCESS", "YOUTUBE_GET_VIDEO", `Fetching video was successful.`);
  275. return cb({ status: "success", message: "Successfully fetched YouTube video", data: res.video });
  276. })
  277. .catch(async err => {
  278. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  279. this.log("ERROR", "YOUTUBE_GET_VIDEO", `Fetching video failed. "${err}"`);
  280. return cb({ status: "error", message: err });
  281. });
  282. }),
  283. /**
  284. * Remove YouTube videos
  285. *
  286. * @returns {{status: string, data: object}}
  287. */
  288. removeVideos: isAdminRequired(function removeVideos(session, videoIds, cb) {
  289. YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
  290. .then(() => {
  291. this.log("SUCCESS", "YOUTUBE_REMOVE_VIDEOS", `Removing videos was successful.`);
  292. return cb({ status: "success", message: "Successfully removed YouTube videos" });
  293. })
  294. .catch(async err => {
  295. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  296. this.log("ERROR", "YOUTUBE_REMOVE_VIDEOS", `Removing videos failed. "${err}"`);
  297. return cb({ status: "error", message: err });
  298. });
  299. }),
  300. /**
  301. * Requests a set of YouTube videos
  302. *
  303. * @param {object} session - the session object automatically added by the websocket
  304. * @param {string} url - the url of the the YouTube playlist
  305. * @param {boolean} musicOnly - whether to only get music from the playlist
  306. * @param {boolean} musicOnly - whether to return videos
  307. * @param {Function} cb - gets called with the result
  308. */
  309. requestSet: isLoginRequired(function requestSet(session, url, musicOnly, returnVideos, cb) {
  310. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  311. .then(response => {
  312. this.log(
  313. "SUCCESS",
  314. "REQUEST_SET",
  315. `Successfully imported a YouTube playlist to be requested for user "${session.userId}".`
  316. );
  317. return cb({
  318. status: "success",
  319. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  320. videos: returnVideos ? response.videos : null
  321. });
  322. })
  323. .catch(async err => {
  324. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  325. this.log(
  326. "ERROR",
  327. "REQUEST_SET",
  328. `Importing a YouTube playlist to be requested failed for user "${session.userId}". "${err}"`
  329. );
  330. return cb({ status: "error", message: err });
  331. });
  332. }),
  333. /**
  334. * Requests a set of YouTube videos as an admin
  335. *
  336. * @param {object} session - the session object automatically added by the websocket
  337. * @param {string} url - the url of the the YouTube playlist
  338. * @param {boolean} musicOnly - whether to only get music from the playlist
  339. * @param {boolean} musicOnly - whether to return videos
  340. * @param {Function} cb - gets called with the result
  341. */
  342. requestSetAdmin: isAdminRequired(async function requestSetAdmin(session, url, musicOnly, returnVideos, cb) {
  343. this.keepLongJob();
  344. this.publishProgress({
  345. status: "started",
  346. title: "Import playlist",
  347. message: "Importing playlist.",
  348. id: this.toString()
  349. });
  350. await CacheModule.runJob("RPUSH", { key: `longJobs.requestSetAdmin`, value: this.toString() }, this);
  351. await CacheModule.runJob(
  352. "HSET",
  353. {
  354. table: `requestSetAdmin`,
  355. key: this.toString(),
  356. value: { payload: { url, musicOnly, returnVideos }, response: null }
  357. },
  358. this
  359. );
  360. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  361. .then(async response => {
  362. this.log(
  363. "SUCCESS",
  364. "REQUEST_SET_ADMIN",
  365. `Successfully imported a YouTube playlist to be requested for admin "${session.userId}".`
  366. );
  367. this.publishProgress({
  368. status: "success",
  369. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`
  370. });
  371. await CacheModule.runJob(
  372. "HSET",
  373. {
  374. table: `requestSetAdmin`,
  375. key: this.toString(),
  376. value: { payload: { url, musicOnly, returnVideos }, response }
  377. },
  378. this
  379. );
  380. return cb({
  381. status: "success",
  382. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  383. videos: returnVideos ? response.videos : null
  384. });
  385. })
  386. .catch(async err => {
  387. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  388. this.log(
  389. "ERROR",
  390. "REQUEST_SET_ADMIN",
  391. `Importing a YouTube playlist to be requested failed for admin "${session.userId}". "${err}"`
  392. );
  393. this.publishProgress({
  394. status: "error",
  395. message: err
  396. });
  397. return cb({ status: "error", message: err });
  398. });
  399. }),
  400. /**
  401. * Gets request set admin long jobs
  402. *
  403. * @param {object} session - the session object automatically added by the websocket
  404. * @param {Function} cb - gets called with the result
  405. */
  406. getRequestSetAdminLongJobs: isLoginRequired(async function getRequestSetAdminLongJobs(session, cb) {
  407. async.waterfall(
  408. [
  409. next => {
  410. CacheModule.runJob(
  411. "LRANGE",
  412. {
  413. key: `longJobs.requestSetAdmin`
  414. },
  415. this
  416. )
  417. .then(longJobUuids => next(null, longJobUuids))
  418. .catch(next);
  419. },
  420. (longJobUuids, next) => {
  421. next(
  422. null,
  423. longJobUuids.map(longJobUuid => ({
  424. job: moduleManager.jobManager.getJob(longJobUuid),
  425. uuid: longJobUuid
  426. }))
  427. );
  428. },
  429. (longJobs, next) => {
  430. async.eachLimit(
  431. longJobs,
  432. 1,
  433. (longJob, next) => {
  434. CacheModule.runJob("HGET", { table: "requestSetAdmin", key: longJob.uuid }, this)
  435. .then(value => {
  436. if (value) {
  437. const { payload, response } = value;
  438. longJob.payload = payload;
  439. longJob.response = response;
  440. }
  441. next();
  442. })
  443. .catch(console.log);
  444. },
  445. err => {
  446. next(err, longJobs);
  447. }
  448. );
  449. },
  450. (longJobs, next) => {
  451. longJobs.forEach(({ job }) => {
  452. if (job && job.onProgress)
  453. job.onProgress.on("progress", data => {
  454. this.publishProgress(
  455. {
  456. id: job.toString(),
  457. ...data
  458. },
  459. true
  460. );
  461. });
  462. });
  463. next(
  464. null,
  465. longJobs.map(({ job, uuid, payload, response }) => ({
  466. id: uuid,
  467. name: job ? job.longJobTitle : "",
  468. status: job ? job.lastProgressData.status : "",
  469. message: job ? job.lastProgressData.message : "",
  470. payload,
  471. response
  472. }))
  473. );
  474. }
  475. ],
  476. async (err, longJobs) => {
  477. if (err) {
  478. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  479. this.log(
  480. "ERROR",
  481. "GET_REQUEST_SET_ADMIN_LONG_JOBS",
  482. `Couldn't get request set admin long jobs for user "${session.userId}". "${err}"`
  483. );
  484. return cb({ status: "error", message: err });
  485. }
  486. this.log(
  487. "SUCCESS",
  488. "GET_REQUEST_SET_ADMIN_LONG_JOBS",
  489. `Got request set admin long jobs for user "${session.userId}".`
  490. );
  491. return cb({
  492. status: "success",
  493. data: {
  494. longJobs
  495. }
  496. });
  497. }
  498. );
  499. })
  500. };