Merge pull request #122 from ajayyy/experimental

Added option to use new user counter
This commit is contained in:
Ajay Ramachandran 2020-08-30 00:25:56 -04:00 committed by GitHub
commit ffa6b4fac8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 27 deletions

View file

@ -6,6 +6,7 @@ var config = require('./config.js');
// Middleware
var corsMiddleware = require('./middleware/cors.js');
var loggerMiddleware = require('./middleware/logger.js');
const userCounter = require('./middleware/userCounter.js');
// Routes
var getSkipSegments = require('./routes/getSkipSegments.js').endpoint;
@ -31,6 +32,8 @@ app.use(corsMiddleware);
app.use(loggerMiddleware);
app.use(express.json())
if (config.userCounterURL) app.use(userCounter);
// Setup pretty JSON
if (config.mode === "development") app.set('json spaces', 2);

View file

@ -0,0 +1,11 @@
var request = require('request');
var config = require('../config.js');
var getIP = require('../utils/getIP.js');
const getHash = require('../utils/getHash.js');
module.exports = function userCounter(req, res, next) {
request.post(config.userCounterURL + "/api/v1/addIP?hashedIP=" + getHash(getIP(req), 1));
next();
}

View file

@ -1,11 +1,15 @@
var db = require('../databases/databases.js').db;
var request = require('request');
const db = require('../databases/databases.js').db;
const request = require('request');
const config = require('../config.js');
// A cache of the number of chrome web store users
var chromeUsersCache = null;
var firefoxUsersCache = null;
var lastUserCountCheck = 0;
let chromeUsersCache = null;
let firefoxUsersCache = null;
// By the privacy friendly user counter
let apiUsersCache = null;
let lastUserCountCheck = 0;
module.exports = function getTotalStats (req, res) {
let row = db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " +
@ -16,6 +20,7 @@ module.exports = function getTotalStats (req, res) {
res.send({
userCount: row.userCount,
activeUsers: chromeUsersCache + firefoxUsersCache,
apiUsers: apiUsersCache,
viewCount: row.viewCount,
totalSubmissions: row.totalSubmissions,
minutesSaved: row.minutesSaved
@ -26,28 +31,37 @@ module.exports = function getTotalStats (req, res) {
if (now - lastUserCountCheck > 5000000) {
lastUserCountCheck = now;
// Get total users
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;
}
});
updateExtensionUsers();
}
}
}
function updateExtensionUsers() {
if (config.userCounterURL) {
request.get(config.userCounterURL + "/api/v1/userCount", (err, response, body) => {
apiUsersCache = JSON.parse(body).userCount;
});
}
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;
}
});
}