Don't always use YouTube API cache

This commit is contained in:
Ajay Ramachandran
2021-04-08 20:37:19 -04:00
parent 8088f37632
commit 6a9b218e22
6 changed files with 183 additions and 177 deletions

View File

@@ -1,6 +1,7 @@
import {config} from '../config';
import {Logger} from '../utils/logger';
import fetch from 'node-fetch';
import AbortController from "abort-controller";
function getVoteAuthorRaw(submissionCount: number, isVIP: boolean, isOwnSubmission: boolean): string {
if (isOwnSubmission) {
@@ -30,7 +31,8 @@ function dispatchEvent(scope: string, data: any): void {
let webhooks = config.webhooks;
if (webhooks === undefined || webhooks.length === 0) return;
Logger.debug("Dispatching webhooks");
webhooks.forEach(webhook => {
for (const webhook of webhooks) {
let webhookURL = webhook.url;
let authKey = webhook.key;
let scopes = webhook.scopes || [];
@@ -43,13 +45,13 @@ function dispatchEvent(scope: string, data: any): void {
"Authorization": authKey,
"Event-Type": scope, // Maybe change this in the future?
'Content-Type': 'application/json'
},
}
})
.catch(err => {
Logger.warn('Couldn\'t send webhook to ' + webhook.url);
Logger.warn(err);
});
});
}
}
export {

View File

@@ -10,43 +10,45 @@ _youTubeAPI.authenticate({
});
export class YouTubeAPI {
static listVideos(videoID: string, callback: (err: string | boolean, data: any) => void) {
static async listVideos(videoID: string, ignoreCache = false): Promise<{err: string | boolean, data?: any}> {
const part = 'contentDetails,snippet';
if (!videoID || videoID.length !== 11 || videoID.includes(".")) {
callback("Invalid video ID", undefined);
return;
return { err: "Invalid video ID" };
}
const redisKey = "youtube.video." + videoID;
redis.get(redisKey, (getErr, result) => {
if (getErr || !result) {
if (!ignoreCache) {
const {err, reply} = await redis.getAsync(redisKey);
if (!err && reply) {
Logger.debug("redis: no cache for video information: " + videoID);
_youTubeAPI.videos.list({
part,
id: videoID,
}, (ytErr: boolean | string, { data }: any) => {
if (!ytErr) {
// Only set cache if data returned
if (data.items.length > 0) {
redis.set(redisKey, JSON.stringify(data), (setErr) => {
if (setErr) {
Logger.warn(setErr.message);
} else {
Logger.debug("redis: video information cache set for: " + videoID);
}
callback(false, data); // don't fail
});
} else {
callback(false, data); // don't fail
}
} else {
callback(ytErr, data);
}
});
} else {
Logger.debug("redis: fetched video information from cache: " + videoID);
callback(getErr?.message, JSON.parse(result));
return { err: err?.message, data: JSON.parse(reply) }
}
});
};
}
const { ytErr, data } = await new Promise((resolve) => _youTubeAPI.videos.list({
part,
id: videoID,
}, (ytErr: boolean | string, { data }: any) => resolve({ytErr, data})));
if (!ytErr) {
// Only set cache if data returned
if (data.items.length > 0) {
const { err: setErr } = await redis.setAsync(redisKey, JSON.stringify(data));
if (setErr) {
Logger.warn(setErr.message);
} else {
Logger.debug("redis: video information cache set for: " + videoID);
}
return { err: false, data }; // don't fail
} else {
return { err: false, data }; // don't fail
}
} else {
return { err: ytErr, data };
}
}
}