youtube.js 10 KB

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