From aad16e23599498614dcaa4726320683215aeeac9 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Fri, 24 Jan 2020 22:56:47 -0500 Subject: [PATCH] API now returns the user count from the download stores. --- index.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/index.js b/index.js index ac337b1..c902707 100644 --- a/index.js +++ b/index.js @@ -38,6 +38,11 @@ var adminUserID = config.adminUserID; //if so, it will use the x-forwarded header instead of the ip address of the connection var behindProxy = config.behindProxy; +// A cache of the number of chrome web store users +var chromeUsersCache = null; +var firefoxUsersCache = null; +var lastUserCountCheck = 0; + // Enable WAL mode checkpoint number if (!config.readOnly && config.mode === "production") { db.exec("PRAGMA journal_mode=WAL;"); @@ -763,10 +768,40 @@ app.get('/api/getTotalStats', function (req, res) { //send this result res.send({ userCount: row.userCount, + activeUsers: chromeUsersCache + firefoxUsersCache, viewCount: row.viewCount, totalSubmissions: row.totalSubmissions, minutesSaved: row.minutesSaved }); + + // Check if the cache should be updated (every ~14 hours) + let now = Date.now(); + 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(/(?<=\)/)[0].replace(",", "")); + } catch (error) { + // Re-check later + lastUserCountCheck = 0; + } + } else { + lastUserCountCheck = 0; + } + }); + } catch (error) { + // Re-check later + lastUserCountCheck = 0; + } + }); + } } });