mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 06:27:10 +03:00
add new CWS user parsing method
This commit is contained in:
@@ -3,6 +3,7 @@ import { config } from "../config";
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { Logger } from "../utils/logger";
|
import { Logger } from "../utils/logger";
|
||||||
|
import { getCWSUsers } from "../utils/getCWSUsers";
|
||||||
|
|
||||||
// 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;
|
||||||
@@ -26,10 +27,7 @@ let lastFetch: DBStatsData = {
|
|||||||
minutesSaved: 0
|
minutesSaved: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
updateExtensionUsers();
|
|
||||||
|
|
||||||
export async function getTotalStats(req: Request, res: Response): Promise<void> {
|
export async function getTotalStats(req: Request, res: Response): Promise<void> {
|
||||||
|
|
||||||
const row = await getStats(!!req.query.countContributingUsers);
|
const row = await getStats(!!req.query.countContributingUsers);
|
||||||
lastFetch = row;
|
lastFetch = row;
|
||||||
|
|
||||||
@@ -51,7 +49,7 @@ export async function getTotalStats(req: Request, res: Response): Promise<void>
|
|||||||
if (now - lastUserCountCheck > 5000000) {
|
if (now - lastUserCountCheck > 5000000) {
|
||||||
lastUserCountCheck = now;
|
lastUserCountCheck = now;
|
||||||
|
|
||||||
updateExtensionUsers();
|
await updateExtensionUsers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -68,22 +66,28 @@ function getStats(countContributingUsers: boolean): Promise<DBStatsData> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function updateExtensionUsers() {
|
async function updateExtensionUsers() {
|
||||||
if (config.userCounterURL) {
|
if (config.userCounterURL) {
|
||||||
axios.get(`${config.userCounterURL}/api/v1/userCount`)
|
axios.get(`${config.userCounterURL}/api/v1/userCount`)
|
||||||
.then(res => {
|
.then(res => apiUsersCache = Math.max(apiUsersCache, res.data.userCount))
|
||||||
apiUsersCache = Math.max(apiUsersCache, res.data.userCount);
|
.catch( /* istanbul ignore next */ () => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
||||||
})
|
|
||||||
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mozillaAddonsUrl = "https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/";
|
const mozillaAddonsUrl = "https://addons.mozilla.org/api/v3/addons/addon/sponsorblock/";
|
||||||
const chromeExtensionUrl = "https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone";
|
const chromeExtensionUrl = "https://chrome.google.com/webstore/detail/sponsorblock-for-youtube/mnjggcdmjocbbbhaepdhchncahnbgone";
|
||||||
|
const chromeExtId = "mnjggcdmjocbbbhaepdhchncahnbgone";
|
||||||
|
|
||||||
axios.get(mozillaAddonsUrl)
|
firefoxUsersCache = await axios.get(mozillaAddonsUrl)
|
||||||
.then(res => {
|
.then(res => res.data.average_daily_users )
|
||||||
firefoxUsersCache = res.data.average_daily_users;
|
.catch( /* istanbul ignore next */ () => {
|
||||||
axios.get(chromeExtensionUrl)
|
Logger.debug(`Failing to connect to ${mozillaAddonsUrl}`);
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
chromeUsersCache = await getCWSUsers(chromeExtId) ?? await getChromeUsers(chromeExtensionUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getChromeUsers(chromeExtensionUrl: string): Promise<number> {
|
||||||
|
return axios.get(chromeExtensionUrl)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
const body = res.data;
|
const body = res.data;
|
||||||
// 2021-01-05
|
// 2021-01-05
|
||||||
@@ -91,18 +95,17 @@ function updateExtensionUsers() {
|
|||||||
const matchingString = '"UserDownloads:';
|
const matchingString = '"UserDownloads:';
|
||||||
const matchingStringLen = matchingString.length;
|
const matchingStringLen = matchingString.length;
|
||||||
const userDownloadsStartIndex = body.indexOf(matchingString);
|
const userDownloadsStartIndex = body.indexOf(matchingString);
|
||||||
|
/* istanbul ignore else */
|
||||||
if (userDownloadsStartIndex >= 0) {
|
if (userDownloadsStartIndex >= 0) {
|
||||||
const closingQuoteIndex = body.indexOf('"', userDownloadsStartIndex + matchingStringLen);
|
const closingQuoteIndex = body.indexOf('"', userDownloadsStartIndex + matchingStringLen);
|
||||||
const userDownloadsStr = body.substr(userDownloadsStartIndex + matchingStringLen, closingQuoteIndex - userDownloadsStartIndex).replace(",", "").replace(".", "");
|
const userDownloadsStr = body.substr(userDownloadsStartIndex + matchingStringLen, closingQuoteIndex - userDownloadsStartIndex).replace(",", "").replace(".", "");
|
||||||
chromeUsersCache = parseInt(userDownloadsStr);
|
return parseInt(userDownloadsStr);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
lastUserCountCheck = 0;
|
lastUserCountCheck = 0;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => Logger.debug(`Failing to connect to ${chromeExtensionUrl}`));
|
.catch(/* istanbul ignore next */ () => {
|
||||||
})
|
Logger.debug(`Failing to connect to ${chromeExtensionUrl}`);
|
||||||
.catch(() => {
|
return 0;
|
||||||
Logger.debug(`Failing to connect to ${mozillaAddonsUrl}`);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
13
src/utils/getCWSUsers.ts
Normal file
13
src/utils/getCWSUsers.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import { Logger } from "../utils/logger";
|
||||||
|
|
||||||
|
export const getCWSUsers = (extID: string) =>
|
||||||
|
axios.post(`https://chrome.google.com/webstore/ajax/detail?pv=20210820&id=${extID}`)
|
||||||
|
.then((res) => res.data.split("\n")[2])
|
||||||
|
.then((data) => JSON.parse(data))
|
||||||
|
.then((data) => (data[1][1][0][23]).replaceAll(/,|\+/,""))
|
||||||
|
.then((data) => parseInt(data))
|
||||||
|
.catch((err) => {
|
||||||
|
Logger.error(`Error getting chrome users - ${err}`);
|
||||||
|
return undefined;
|
||||||
|
});
|
||||||
@@ -2,6 +2,7 @@ import axios from "axios";
|
|||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import { config } from "../../src/config";
|
import { config } from "../../src/config";
|
||||||
import { getHash } from "../../src/utils/getHash";
|
import { getHash } from "../../src/utils/getHash";
|
||||||
|
import { client } from "../utils/httpClient";
|
||||||
|
|
||||||
describe("userCounter", () => {
|
describe("userCounter", () => {
|
||||||
it("Should return 200", function (done) {
|
it("Should return 200", function (done) {
|
||||||
@@ -20,4 +21,13 @@ describe("userCounter", () => {
|
|||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
it("Should not incremeent counter on OPTIONS", function (done) {
|
||||||
|
/* cannot spy test */
|
||||||
|
if (!config.userCounterURL) this.skip(); // skip if no userCounterURL is set
|
||||||
|
//const spy = sinon.spy(UserCounter);
|
||||||
|
client({ method: "OPTIONS", url: "/api/status" })
|
||||||
|
.then(() => client({ method: "GET", url: "/api/status" }));
|
||||||
|
//assert.strictEqual(spy.callCount, 1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user