youtube.js 9.9 KB

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