mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-14 23:47:00 +03:00
Replace request with node-fetch
This commit is contained in:
@@ -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(/(?<=\<span class=\"e-f-ih\" title=\").*?(?= users\">)/)[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
|
||||
// [...]<span><meta itemprop="interactionCount" content="UserDownloads:100.000+"/><meta itemprop="opera[...]
|
||||
const matchingString = '"UserDownloads:';
|
||||
const matchingStringLen = matchingString.length;
|
||||
const userDownloadsStartIndex = body.indexOf(matchingString);
|
||||
if (userDownloadsStartIndex >= 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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user