mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2026-02-02 07:40:43 +03:00
Added youtube api cache (optional)
This commit is contained in:
@@ -2,6 +2,7 @@ var express = require('express');
|
||||
// Create a service (the app object is just a callback).
|
||||
var app = express();
|
||||
var config = require('./config.js');
|
||||
var redis = require('./utils/redis.js');
|
||||
|
||||
// Middleware
|
||||
var corsMiddleware = require('./middleware/cors.js');
|
||||
|
||||
@@ -48,10 +48,7 @@ function sendWebhooks(userID, videoID, UUID, segmentInfo) {
|
||||
if (config.youtubeAPIKey !== null) {
|
||||
let userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
||||
|
||||
YouTubeAPI.videos.list({
|
||||
part: "snippet",
|
||||
id: videoID
|
||||
}, function (err, data) {
|
||||
YouTubeAPI.listVideos(videoID, "snippet", (err, data) => {
|
||||
if (err || data.items.length === 0) {
|
||||
err && logger.error(err);
|
||||
return;
|
||||
@@ -108,10 +105,7 @@ async function autoModerateSubmission(submission, callback) {
|
||||
// Get the video information from the youtube API
|
||||
if (config.youtubeAPIKey !== null) {
|
||||
let {err, data} = await new Promise((resolve, reject) => {
|
||||
YouTubeAPI.videos.list({
|
||||
part: "contentDetails",
|
||||
id: submission.videoID
|
||||
}, (err, data) => resolve({err, data}));
|
||||
YouTubeAPI.listVideos(submission.videoID, "contentDetails", (err, data) => resolve({err, data}));
|
||||
});
|
||||
|
||||
if (err) {
|
||||
@@ -134,7 +128,6 @@ async function autoModerateSubmission(submission, callback) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
logger.debug("Skipped YouTube API");
|
||||
|
||||
@@ -214,6 +207,10 @@ module.exports = async function postSkipSegments(req, res) {
|
||||
if (isNaN(startTime) || isNaN(endTime)
|
||||
|| startTime === Infinity || endTime === Infinity || startTime < 0 || startTime >= endTime) {
|
||||
//invalid request
|
||||
logger.debug(videoID);
|
||||
logger.debug(userID);
|
||||
logger.debug(JSON.stringify(segments));
|
||||
logger.debug('400');
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -48,10 +48,7 @@ function sendWebhooks(voteData) {
|
||||
}
|
||||
|
||||
if (config.youtubeAPIKey !== null) {
|
||||
YouTubeAPI.videos.list({
|
||||
part: "snippet",
|
||||
id: submissionInfoRow.videoID
|
||||
}, function (err, data) {
|
||||
YouTubeAPI.listVideos(submissionInfoRow.videoID, "snippet", (err, data) => {
|
||||
if (err || data.items.length === 0) {
|
||||
err && logger.error(err);
|
||||
return;
|
||||
|
||||
15
src/utils/redis.js
Normal file
15
src/utils/redis.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const config = require('../config.js');
|
||||
const logger = require('./logger.js');
|
||||
|
||||
if (config.redis) {
|
||||
const redis = require('redis');
|
||||
logger.info('Connected to redis');
|
||||
const client = redis.createClient(config.redis);
|
||||
module.exports = client;
|
||||
} else {
|
||||
module.exports = {
|
||||
get: (key, callback) => {
|
||||
callback((true));
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -2,6 +2,8 @@ var config = require('../config.js');
|
||||
|
||||
// YouTube API
|
||||
const YouTubeAPI = require("youtube-api");
|
||||
const redis = require('./redis.js');
|
||||
const logger = require('./logger.js');
|
||||
|
||||
var exportObject;
|
||||
// If in test mode, return a mocked youtube object
|
||||
@@ -14,6 +16,44 @@ if (config.mode === "test") {
|
||||
key: config.youtubeAPIKey
|
||||
});
|
||||
exportObject = YouTubeAPI;
|
||||
|
||||
// YouTubeAPI.videos.list wrapper with cacheing
|
||||
exportObject.listVideos = (videoID, part, callback) => {
|
||||
let redisKey = "youtube.video." + videoID + "." + part;
|
||||
redis.get(redisKey, (getErr, result) => {
|
||||
if (getErr || !result) {
|
||||
logger.debug("redis: no cache for video information: " + videoID);
|
||||
YouTubeAPI.videos.list({
|
||||
part,
|
||||
id: videoID
|
||||
}, (ytErr, data) => {
|
||||
if (!ytErr) {
|
||||
// Only set cache if data returned
|
||||
if (data.items.length > 0) {
|
||||
redis.set(redisKey, JSON.stringify(data), (setErr) => {
|
||||
logger.debug("redis: video information cache set for: " + videoID);
|
||||
setErr && logger.warn(setErr);
|
||||
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, JSON.parse(result));
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/* YouTubeAPI.videos.list({
|
||||
part: "snippet",
|
||||
id: videoID
|
||||
}, function (err, data) {*/
|
||||
|
||||
|
||||
module.exports = exportObject;
|
||||
Reference in New Issue
Block a user