youtube.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. const importJobModel = await DBModule.runJob("GET_MODEL", { modelName: "importJob" }, this);
  344. this.keepLongJob();
  345. this.publishProgress({
  346. status: "started",
  347. title: "Import playlist",
  348. message: "Importing playlist.",
  349. id: this.toString()
  350. });
  351. await CacheModule.runJob("RPUSH", { key: `longJobs.${session.userId}`, value: this.toString() }, this);
  352. await CacheModule.runJob(
  353. "PUB",
  354. {
  355. channel: "longJob.added",
  356. value: { jobId: this.toString(), userId: session.userId }
  357. },
  358. this
  359. );
  360. async.waterfall(
  361. [
  362. next => {
  363. importJobModel.create(
  364. {
  365. type: "youtube",
  366. query: {
  367. url,
  368. musicOnly
  369. },
  370. status: "in-progress",
  371. response: {},
  372. requestedBy: session.userId,
  373. requestedAt: Date.now()
  374. },
  375. next
  376. );
  377. },
  378. (importJob, next) => {
  379. YouTubeModule.runJob("REQUEST_SET", { url, musicOnly, returnVideos }, this)
  380. .then(response => {
  381. next(null, importJob, response);
  382. })
  383. .catch(err => {
  384. next(err, importJob);
  385. });
  386. },
  387. (importJob, response, next) => {
  388. importJobModel.updateOne(
  389. { _id: importJob._id },
  390. {
  391. $set: {
  392. status: "success",
  393. response: {
  394. failed: response.failed,
  395. successful: response.successful,
  396. alreadyInDatabase: response.alreadyInDatabase,
  397. successfulVideoIds: response.successfulVideoIds,
  398. failedVideoIds: response.failedVideoIds
  399. }
  400. }
  401. },
  402. err => {
  403. next(err, importJob, response);
  404. }
  405. );
  406. }
  407. ],
  408. async (err, importJob, response) => {
  409. if (err) {
  410. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  411. this.log(
  412. "ERROR",
  413. "REQUEST_SET_ADMIN",
  414. `Importing a YouTube playlist to be requested failed for admin "${session.userId}". "${err}"`
  415. );
  416. importJobModel.updateOne({ _id: importJob._id }, { $set: { status: "error" } });
  417. return cb({ status: "error", message: err });
  418. }
  419. this.log(
  420. "SUCCESS",
  421. "REQUEST_SET_ADMIN",
  422. `Successfully imported a YouTube playlist to be requested for admin "${session.userId}".`
  423. );
  424. this.publishProgress({
  425. status: "success",
  426. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`
  427. });
  428. return cb({
  429. status: "success",
  430. message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInDatabase} were already in database)`,
  431. videos: returnVideos ? response.videos : null
  432. });
  433. }
  434. );
  435. })
  436. };