diff --git a/src/routes/getTotalStats.ts b/src/routes/getTotalStats.ts index d13d080..b5e6bf8 100644 --- a/src/routes/getTotalStats.ts +++ b/src/routes/getTotalStats.ts @@ -1,7 +1,8 @@ import {db} from '../databases/databases'; -import request from 'request'; import {config} from '../config'; import {Request, Response} from 'express'; +import fetch from 'node-fetch'; +import {Logger} from '../utils/logger'; // A cache of the number of chrome web store users let chromeUsersCache = 0; @@ -41,30 +42,42 @@ export function getTotalStats(req: Request, res: Response) { function updateExtensionUsers() { if (config.userCounterURL) { - request.get(config.userCounterURL + "/api/v1/userCount", (err, response, body) => { - apiUsersCache = Math.max(apiUsersCache, JSON.parse(body).userCount); - }); + fetch(config.userCounterURL + "/api/v1/userCount") + .then(res => res.json()) + .then(data => { + apiUsersCache = Math.max(apiUsersCache, data.userCount); + }) + .catch(() => Logger.debug("Failing to connect to user counter at: " + config.userCounterURL)); } - - request.get("https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/", function (err, firefoxResponse, body) { - try { - firefoxUsersCache = parseInt(JSON.parse(body).average_daily_users); - - request.get("https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone", function (err, chromeResponse, body) { - if (body !== undefined) { - try { - chromeUsersCache = parseInt(body.match(/(?<=\)/)[0].replace(",", "")); - } catch (error) { - // Re-check later - lastUserCountCheck = 0; - } - } else { - lastUserCountCheck = 0; - } - }); - } catch (error) { - // Re-check later - lastUserCountCheck = 0; - } + + const mozillaAddonsUrl = "https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/"; + const chromeExtensionUrl = "https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone"; + + fetch(mozillaAddonsUrl) + .then(res => res.json()) + .then(data => { + firefoxUsersCache = data.average_daily_users; + + fetch(chromeExtensionUrl) + .then(res => res.text()) + .then(body => { + // 2021-01-05 + // [...]= 0) { + const closingQuoteIndex = body.indexOf('"', userDownloadsStartIndex + matchingStringLen); + const userDownloadsStr = body.substr(userDownloadsStartIndex + matchingStringLen, closingQuoteIndex - userDownloadsStartIndex).replace(',','').replace('.',''); + chromeUsersCache = parseInt(userDownloadsStr); + } + else { + lastUserCountCheck = 0; + } + }) + .catch(() => Logger.debug("Failing to connect to " + chromeExtensionUrl)); + }) + .catch(err => { + Logger.debug("Failing to connect to " + mozillaAddonsUrl); }); } diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index f1cda5e..c03ba81 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -3,7 +3,7 @@ import {Logger} from '../utils/logger'; import {db, privateDB} from '../databases/databases'; import {YouTubeAPI} from '../utils/youtubeApi'; import {getSubmissionUUID} from '../utils/getSubmissionUUID'; -import request from 'request'; +import fetch from 'node-fetch'; import isoDurations from 'iso8601-duration'; import fetch from 'node-fetch'; import {getHash} from '../utils/getHash'; @@ -64,8 +64,10 @@ function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo // If it is a first time submission // Then send a notification to discord if (config.discordFirstTimeSubmissionsWebhookURL === null || userSubmissionCountRow.submissionCount > 1) return; - request.post(config.discordFirstTimeSubmissionsWebhookURL, { - json: { + + fetch(config.discordFirstTimeSubmissionsWebhookURL, { + method: 'POST', + body: JSON.stringify({ "embeds": [{ "title": data.items[0].snippet.title, "url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (parseInt(startTime.toFixed(0)) - 2), @@ -81,17 +83,19 @@ function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo "url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "", }, }], - }, - }, (err, res) => { - if (err) { - Logger.error("Failed to send first time submission Discord hook."); - Logger.error(JSON.stringify(err)); - Logger.error("\n"); - } else if (res && res.statusCode >= 400) { + }) + }) + .then(res => { + if (res.status >= 400) { Logger.error("Error sending first time submission Discord hook"); Logger.error(JSON.stringify(res)); Logger.error("\n"); } + }) + .catch(err => { + Logger.error("Failed to send first time submission Discord hook."); + Logger.error(JSON.stringify(err)); + Logger.error("\n"); }); }); } @@ -114,8 +118,10 @@ function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime // Send discord message if (config.discordNeuralBlockRejectWebhookURL === null) return; - request.post(config.discordNeuralBlockRejectWebhookURL, { - json: { + + fetch(config.discordNeuralBlockRejectWebhookURL, { + method: 'POST', + body: JSON.stringify({ "embeds": [{ "title": ytData.items[0].snippet.title, "url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (parseFloat(startTime.toFixed(0)) - 2), @@ -131,17 +137,19 @@ function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime "url": ytData.items[0].snippet.thumbnails.maxres ? ytData.items[0].snippet.thumbnails.maxres.url : "", }, }], - }, - }, (err, res) => { - if (err) { - Logger.error("Failed to send NeuralBlock Discord hook."); - Logger.error(JSON.stringify(err)); - Logger.error("\n"); - } else if (res && res.statusCode >= 400) { + }) + }) + .then(res => { + if (res.status >= 400) { Logger.error("Error sending NeuralBlock Discord hook"); Logger.error(JSON.stringify(res)); Logger.error("\n"); } + }) + .catch(err => { + Logger.error("Failed to send NeuralBlock Discord hook."); + Logger.error(JSON.stringify(err)); + Logger.error("\n"); }); } @@ -229,15 +237,20 @@ async function autoModerateSubmission(submission: { videoID: any; userID: any; s } function proxySubmission(req: Request) { - request.post(config.proxySubmission + '/api/skipSegments?userID=' + req.query.userID + '&videoID=' + req.query.videoID, {json: req.body}, (err, result) => { - if (config.mode === 'development') { - if (!err) { - Logger.debug('Proxy Submission: ' + result.statusCode + ' (' + result.body + ')'); - } else { + fetch(config.proxySubmission + '/api/skipSegments?userID=' + req.query.userID + '&videoID=' + req.query.videoID, { + method: 'POST', + body: req.body, + }) + .then(async res => { + if (config.mode === 'development') { + Logger.debug('Proxy Submission: ' + res.status + ' (' + (await res.text()) + ')'); + } + }) + .catch(err => { + if (config.mode === 'development') { Logger.error("Proxy Submission: Failed to make call"); } - } - }); + }); } export async function postSkipSegments(req: Request, res: Response) { diff --git a/src/routes/voteOnSponsorTime.ts b/src/routes/voteOnSponsorTime.ts index 46ab33a..aa76d26 100644 --- a/src/routes/voteOnSponsorTime.ts +++ b/src/routes/voteOnSponsorTime.ts @@ -1,7 +1,7 @@ import {Request, Response} from 'express'; import {Logger} from '../utils/logger'; import {isUserVIP} from '../utils/isUserVIP'; -import request from 'request'; +import fetch from 'node-fetch'; import {YouTubeAPI} from '../utils/youtubeApi'; import {db, privateDB} from '../databases/databases'; import {dispatchEvent, getVoteAuthor, getVoteAuthorRaw} from '../utils/webhookUtils'; @@ -89,8 +89,9 @@ function sendWebhooks(voteData: VoteData) { // Send discord message if (webhookURL !== null && !isUpvote) { - request.post(webhookURL, { - json: { + fetch(webhookURL, { + method: 'POST', + body: JSON.stringify({ "embeds": [{ "title": data.items[0].snippet.title, "url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID @@ -112,17 +113,19 @@ function sendWebhooks(voteData: VoteData) { "url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "", }, }], - }, - }, (err, res) => { - if (err) { - Logger.error("Failed to send reported submission Discord hook."); - Logger.error(JSON.stringify(err)); - Logger.error("\n"); - } else if (res && res.statusCode >= 400) { + }) + }) + .then(async res => { + if (res.status >= 400) { Logger.error("Error sending reported submission Discord hook"); - Logger.error(JSON.stringify(res)); + Logger.error(JSON.stringify((await res.text()))); Logger.error("\n"); } + }) + .catch(err => { + Logger.error("Failed to send reported submission Discord hook."); + Logger.error(JSON.stringify(err)); + Logger.error("\n"); }); } diff --git a/src/utils/webhookUtils.ts b/src/utils/webhookUtils.ts index 955107c..613e1ee 100644 --- a/src/utils/webhookUtils.ts +++ b/src/utils/webhookUtils.ts @@ -1,6 +1,6 @@ import {config} from '../config'; import {Logger} from '../utils/logger'; -import request from 'request'; +import fetch from 'node-fetch'; function getVoteAuthorRaw(submissionCount: number, isVIP: boolean, isOwnSubmission: boolean): string { if (isOwnSubmission) { @@ -36,12 +36,15 @@ function dispatchEvent(scope: string, data: any): void { let scopes = webhook.scopes || []; if (!scopes.includes(scope.toLowerCase())) return; - request.post(webhookURL, { - json: data, headers: { + fetch(webhookURL, { + method: 'POST', + body: JSON.stringify(data), + headers: { "Authorization": authKey, "Event-Type": scope, // Maybe change this in the future? }, - }).on('error', (e) => { + }) + .catch(err => { Logger.warn('Couldn\'t send webhook to ' + webhook.url); Logger.warn(e.message); });