mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-13 06:57:05 +03:00
Added youtube api cache (optional)
This commit is contained in:
@@ -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