Переглянути джерело

feature: add job/action to fetch YT videos for YT channel ids

Kristian Vos 1 місяць тому
батько
коміт
83230e87f3
2 змінених файлів з 71 додано та 0 видалено
  1. 54 0
      backend/logic/actions/youtube.js
  2. 17 0
      backend/logic/youtube.js

+ 54 - 0
backend/logic/actions/youtube.js

@@ -452,6 +452,31 @@ export default {
 			});
 	}),
 
+	/**
+	 * Get YouTube videos for channel ids
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {string} channelIds - the channel ids
+	 * @param {Function} cb - gets called with the result
+	 * @returns {{status: string, data: object}}
+	 */
+	getVideosForChannelIds: useHasPermission("youtube.getChannel", function getVideo(session, channelIds, cb) {
+		return YouTubeModule.runJob("GET_VIDEOS_FOR_CHANNEL_IDS", { channelIds }, this)
+			.then(res => {
+				this.log("SUCCESS", "YOUTUBE_GET_VIDEOS_FOR_CHANNEL_IDS", `Fetching videos was successful.`);
+
+				return cb({
+					status: "success",
+					message: "Successfully fetched YouTube videos",
+					data: res.videos
+				});
+			})
+			.catch(async err => {
+				err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+				this.log("ERROR", "YOUTUBE_GET_VIDEOS_FOR_CHANNEL_IDS", `Fetching videos failed. "${err}"`);
+				return cb({ status: "error", message: err });
+			});
+	}),
+
 	/**
 	 * Get a YouTube channel from ID
 	 * @param {object} session - the session object automatically added by the websocket
@@ -481,6 +506,35 @@ export default {
 			});
 	}),
 
+	/**
+	 * Get a YouTube channels from ID
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {string} channelIds - the YouTube channel ids to get
+	 * @param {Function} cb - gets called with the result
+	 * @returns {{status: string, data: object}}
+	 */
+	getChannelsById: useHasPermission("youtube.getChannel", function getChannel(session, channelIds, cb) {
+		return YouTubeModule.runJob("GET_CHANNELS_FROM_IDS", { channelIds }, this)
+			.then(res => {
+				if (res.channels.length === 0) {
+					this.log("ERROR", "YOUTUBE_GET_CHANNELS_FROM_IDS", `Fetching channel failed.`);
+					return cb({ status: "error", message: "Failed to get channel" });
+				}
+
+				this.log("SUCCESS", "YOUTUBE_GET_CHANNELS_FROM_IDS", `Fetching channels was successful.`);
+				return cb({
+					status: "success",
+					message: "Successfully fetched YouTube channels",
+					data: res.channels
+				});
+			})
+			.catch(async err => {
+				err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+				this.log("ERROR", "YOUTUBE_GET_CHANNELS_FROM_IDS", `Fetching channels failed. "${err}"`);
+				return cb({ status: "error", message: err });
+			});
+	}),
+
 	/**
 	 * Remove YouTube videos
 	 * @param {object} session - the session object automatically added by the websocket

+ 17 - 0
backend/logic/youtube.js

@@ -2102,6 +2102,23 @@ class _YouTubeModule extends CoreClass {
 			got: gotChannels.length
 		};
 	}
+
+	/**
+	 * Gets YouTube videos currently in the database for the provided YouTube channel ids
+	 * Doesn't make any API requests to update/refetch/fetch new YouTube videos
+	 * @param {object} payload - an object containing the payload
+	 * @param {Array} payload.channelIds - list of YouTube channel ids
+	 * @returns {Promise} - returns a promise (resolve, reject)
+	 */
+	async GET_VIDEOS_FOR_CHANNEL_IDS(payload) {
+		const { channelIds } = payload;
+
+		const videos = await YouTubeModule.youtubeVideoModel.find({ "rawData.snippet.channelId": { $in: channelIds } });
+
+		return {
+			videos: videos.map(video => video.toObject())
+		};
+	}
 }
 
 export default new _YouTubeModule();