mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-11 05:57:04 +03:00
Replace request with node-fetch
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import {db} from '../databases/databases';
|
import {db} from '../databases/databases';
|
||||||
import request from 'request';
|
|
||||||
import {config} from '../config';
|
import {config} from '../config';
|
||||||
import {Request, Response} from 'express';
|
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
|
// A cache of the number of chrome web store users
|
||||||
let chromeUsersCache = 0;
|
let chromeUsersCache = 0;
|
||||||
@@ -41,30 +42,42 @@ export function getTotalStats(req: Request, res: Response) {
|
|||||||
|
|
||||||
function updateExtensionUsers() {
|
function updateExtensionUsers() {
|
||||||
if (config.userCounterURL) {
|
if (config.userCounterURL) {
|
||||||
request.get(config.userCounterURL + "/api/v1/userCount", (err, response, body) => {
|
fetch(config.userCounterURL + "/api/v1/userCount")
|
||||||
apiUsersCache = Math.max(apiUsersCache, JSON.parse(body).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) {
|
const mozillaAddonsUrl = "https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/";
|
||||||
try {
|
const chromeExtensionUrl = "https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone";
|
||||||
firefoxUsersCache = parseInt(JSON.parse(body).average_daily_users);
|
|
||||||
|
|
||||||
request.get("https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone", function (err, chromeResponse, body) {
|
fetch(mozillaAddonsUrl)
|
||||||
if (body !== undefined) {
|
.then(res => res.json())
|
||||||
try {
|
.then(data => {
|
||||||
chromeUsersCache = parseInt(body.match(/(?<=\<span class=\"e-f-ih\" title=\").*?(?= users\">)/)[0].replace(",", ""));
|
firefoxUsersCache = data.average_daily_users;
|
||||||
} catch (error) {
|
|
||||||
// Re-check later
|
fetch(chromeExtensionUrl)
|
||||||
lastUserCountCheck = 0;
|
.then(res => res.text())
|
||||||
}
|
.then(body => {
|
||||||
} else {
|
// 2021-01-05
|
||||||
lastUserCountCheck = 0;
|
// [...]<span><meta itemprop="interactionCount" content="UserDownloads:100.000+"/><meta itemprop="opera[...]
|
||||||
}
|
const matchingString = '"UserDownloads:';
|
||||||
});
|
const matchingStringLen = matchingString.length;
|
||||||
} catch (error) {
|
const userDownloadsStartIndex = body.indexOf(matchingString);
|
||||||
// Re-check later
|
if (userDownloadsStartIndex >= 0) {
|
||||||
lastUserCountCheck = 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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import {Logger} from '../utils/logger';
|
|||||||
import {db, privateDB} from '../databases/databases';
|
import {db, privateDB} from '../databases/databases';
|
||||||
import {YouTubeAPI} from '../utils/youtubeApi';
|
import {YouTubeAPI} from '../utils/youtubeApi';
|
||||||
import {getSubmissionUUID} from '../utils/getSubmissionUUID';
|
import {getSubmissionUUID} from '../utils/getSubmissionUUID';
|
||||||
import request from 'request';
|
import fetch from 'node-fetch';
|
||||||
import isoDurations from 'iso8601-duration';
|
import isoDurations from 'iso8601-duration';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import {getHash} from '../utils/getHash';
|
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
|
// If it is a first time submission
|
||||||
// Then send a notification to discord
|
// Then send a notification to discord
|
||||||
if (config.discordFirstTimeSubmissionsWebhookURL === null || userSubmissionCountRow.submissionCount > 1) return;
|
if (config.discordFirstTimeSubmissionsWebhookURL === null || userSubmissionCountRow.submissionCount > 1) return;
|
||||||
request.post(config.discordFirstTimeSubmissionsWebhookURL, {
|
|
||||||
json: {
|
fetch(config.discordFirstTimeSubmissionsWebhookURL, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
"embeds": [{
|
"embeds": [{
|
||||||
"title": data.items[0].snippet.title,
|
"title": data.items[0].snippet.title,
|
||||||
"url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (parseInt(startTime.toFixed(0)) - 2),
|
"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 : "",
|
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
|
||||||
},
|
},
|
||||||
}],
|
}],
|
||||||
},
|
})
|
||||||
}, (err, res) => {
|
})
|
||||||
if (err) {
|
.then(res => {
|
||||||
Logger.error("Failed to send first time submission Discord hook.");
|
if (res.status >= 400) {
|
||||||
Logger.error(JSON.stringify(err));
|
|
||||||
Logger.error("\n");
|
|
||||||
} else if (res && res.statusCode >= 400) {
|
|
||||||
Logger.error("Error sending first time submission Discord hook");
|
Logger.error("Error sending first time submission Discord hook");
|
||||||
Logger.error(JSON.stringify(res));
|
Logger.error(JSON.stringify(res));
|
||||||
Logger.error("\n");
|
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
|
// Send discord message
|
||||||
if (config.discordNeuralBlockRejectWebhookURL === null) return;
|
if (config.discordNeuralBlockRejectWebhookURL === null) return;
|
||||||
request.post(config.discordNeuralBlockRejectWebhookURL, {
|
|
||||||
json: {
|
fetch(config.discordNeuralBlockRejectWebhookURL, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
"embeds": [{
|
"embeds": [{
|
||||||
"title": ytData.items[0].snippet.title,
|
"title": ytData.items[0].snippet.title,
|
||||||
"url": "https://www.youtube.com/watch?v=" + videoID + "&t=" + (parseFloat(startTime.toFixed(0)) - 2),
|
"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 : "",
|
"url": ytData.items[0].snippet.thumbnails.maxres ? ytData.items[0].snippet.thumbnails.maxres.url : "",
|
||||||
},
|
},
|
||||||
}],
|
}],
|
||||||
},
|
})
|
||||||
}, (err, res) => {
|
})
|
||||||
if (err) {
|
.then(res => {
|
||||||
Logger.error("Failed to send NeuralBlock Discord hook.");
|
if (res.status >= 400) {
|
||||||
Logger.error(JSON.stringify(err));
|
|
||||||
Logger.error("\n");
|
|
||||||
} else if (res && res.statusCode >= 400) {
|
|
||||||
Logger.error("Error sending NeuralBlock Discord hook");
|
Logger.error("Error sending NeuralBlock Discord hook");
|
||||||
Logger.error(JSON.stringify(res));
|
Logger.error(JSON.stringify(res));
|
||||||
Logger.error("\n");
|
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) {
|
function proxySubmission(req: Request) {
|
||||||
request.post(config.proxySubmission + '/api/skipSegments?userID=' + req.query.userID + '&videoID=' + req.query.videoID, {json: req.body}, (err, result) => {
|
fetch(config.proxySubmission + '/api/skipSegments?userID=' + req.query.userID + '&videoID=' + req.query.videoID, {
|
||||||
if (config.mode === 'development') {
|
method: 'POST',
|
||||||
if (!err) {
|
body: req.body,
|
||||||
Logger.debug('Proxy Submission: ' + result.statusCode + ' (' + result.body + ')');
|
})
|
||||||
} else {
|
.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");
|
Logger.error("Proxy Submission: Failed to make call");
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function postSkipSegments(req: Request, res: Response) {
|
export async function postSkipSegments(req: Request, res: Response) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import {Request, Response} from 'express';
|
import {Request, Response} from 'express';
|
||||||
import {Logger} from '../utils/logger';
|
import {Logger} from '../utils/logger';
|
||||||
import {isUserVIP} from '../utils/isUserVIP';
|
import {isUserVIP} from '../utils/isUserVIP';
|
||||||
import request from 'request';
|
import fetch from 'node-fetch';
|
||||||
import {YouTubeAPI} from '../utils/youtubeApi';
|
import {YouTubeAPI} from '../utils/youtubeApi';
|
||||||
import {db, privateDB} from '../databases/databases';
|
import {db, privateDB} from '../databases/databases';
|
||||||
import {dispatchEvent, getVoteAuthor, getVoteAuthorRaw} from '../utils/webhookUtils';
|
import {dispatchEvent, getVoteAuthor, getVoteAuthorRaw} from '../utils/webhookUtils';
|
||||||
@@ -89,8 +89,9 @@ function sendWebhooks(voteData: VoteData) {
|
|||||||
|
|
||||||
// Send discord message
|
// Send discord message
|
||||||
if (webhookURL !== null && !isUpvote) {
|
if (webhookURL !== null && !isUpvote) {
|
||||||
request.post(webhookURL, {
|
fetch(webhookURL, {
|
||||||
json: {
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
"embeds": [{
|
"embeds": [{
|
||||||
"title": data.items[0].snippet.title,
|
"title": data.items[0].snippet.title,
|
||||||
"url": "https://www.youtube.com/watch?v=" + submissionInfoRow.videoID
|
"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 : "",
|
"url": data.items[0].snippet.thumbnails.maxres ? data.items[0].snippet.thumbnails.maxres.url : "",
|
||||||
},
|
},
|
||||||
}],
|
}],
|
||||||
},
|
})
|
||||||
}, (err, res) => {
|
})
|
||||||
if (err) {
|
.then(async res => {
|
||||||
Logger.error("Failed to send reported submission Discord hook.");
|
if (res.status >= 400) {
|
||||||
Logger.error(JSON.stringify(err));
|
|
||||||
Logger.error("\n");
|
|
||||||
} else if (res && res.statusCode >= 400) {
|
|
||||||
Logger.error("Error sending reported submission Discord hook");
|
Logger.error("Error sending reported submission Discord hook");
|
||||||
Logger.error(JSON.stringify(res));
|
Logger.error(JSON.stringify((await res.text())));
|
||||||
Logger.error("\n");
|
Logger.error("\n");
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
Logger.error("Failed to send reported submission Discord hook.");
|
||||||
|
Logger.error(JSON.stringify(err));
|
||||||
|
Logger.error("\n");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import {config} from '../config';
|
import {config} from '../config';
|
||||||
import {Logger} from '../utils/logger';
|
import {Logger} from '../utils/logger';
|
||||||
import request from 'request';
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
function getVoteAuthorRaw(submissionCount: number, isVIP: boolean, isOwnSubmission: boolean): string {
|
function getVoteAuthorRaw(submissionCount: number, isVIP: boolean, isOwnSubmission: boolean): string {
|
||||||
if (isOwnSubmission) {
|
if (isOwnSubmission) {
|
||||||
@@ -36,12 +36,15 @@ function dispatchEvent(scope: string, data: any): void {
|
|||||||
let scopes = webhook.scopes || [];
|
let scopes = webhook.scopes || [];
|
||||||
if (!scopes.includes(scope.toLowerCase())) return;
|
if (!scopes.includes(scope.toLowerCase())) return;
|
||||||
|
|
||||||
request.post(webhookURL, {
|
fetch(webhookURL, {
|
||||||
json: data, headers: {
|
method: 'POST',
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
headers: {
|
||||||
"Authorization": authKey,
|
"Authorization": authKey,
|
||||||
"Event-Type": scope, // Maybe change this in the future?
|
"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('Couldn\'t send webhook to ' + webhook.url);
|
||||||
Logger.warn(e.message);
|
Logger.warn(e.message);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user