diff --git a/src/libpoketube/init/pages-api.js b/src/libpoketube/init/pages-api.js index 8cdb8730..efadfea1 100644 --- a/src/libpoketube/init/pages-api.js +++ b/src/libpoketube/init/pages-api.js @@ -101,184 +101,287 @@ if (!data.users) data.users = {} res.json({ ok: true }) }) - app.get("/api/stats", (req, res) => { - if (!telemetryConfig.telemetry) { - const view = (req.query.view || "").toString() - if (!view) { - return res.send(` - - - - Poke – Stats - - - - -
-

Poke stats

-

How Poke improves itself while keeping your browsing private.

- -
-

- This page shows information about Poke’s anonymous, local-only usage statistics feature - (/api/stats). -

-

- For full details on what is collected (and what is not), see the privacy policy section: - /policies/privacy#stats. -

-
- -
-

Developers:

-

- • To get the aggregated stats as JSON, call:
- GET /api/stats?view=json

- • The response includes:
- videos (top 10 most-watched), browsers, os, and totalUsers. -

-
-
- -`) - } - - return res.json({ videos: {}, browsers: {}, os: {}, totalUsers: 0 }) - } - +app.get("/api/stats", (req, res) => { const view = (req.query.view || "").toString() - // if no ?view= param, show info page instead of raw JSON - if (!view) { - return res.send(` + // JSON view (for programmatic access) + if (view === "json") { + if (!telemetryConfig.telemetry) { + return res.json({ videos: {}, browsers: {}, os: {}, totalUsers: 0 }) + } + + const raw = fs.readFileSync(statsFile, "utf8") + const data = JSON.parse(raw) + + if (!data.videos) data.videos = {} + if (!data.browsers) data.browsers = {} + if (!data.os) data.os = {} + if (!data.users) data.users = {} + + const sortedVideos = Object.entries(data.videos) + .sort((a, b) => b[1] - a[1]) + .slice(0, 10) + + const topVideos = Object.fromEntries(sortedVideos) + + return res.json({ + videos: topVideos, + browsers: data.browsers, + os: data.os, + totalUsers: Object.keys(data.users).length + }) + } + + const telemetryOn = telemetryConfig.telemetry + + res.send(` Poke – Stats +

Poke stats

-

How Poke improves itself while keeping your browsing private.

+

+ + At Poke, we do not collect or share any personal information. + To improve Poke we use a completely anonymous, local-only way to figure out how the site is being used. + +

-
-

- This page shows information about Poke’s anonymous, local-only usage statistics feature - (/api/stats). -

-

- For full details on what is collected (and what is not), see the privacy policy section: +

+

Status

+

+

+ For full details on what is collected (and what is not), see: /policies/privacy#stats.

-
-

Developers:

-

- • To get the aggregated stats as JSON, call:
- GET /api/stats?view=json

- • The response includes:
- videos (top 10 most-watched), browsers, os, and totalUsers. +

+

Overview

+

Loading stats…

+
+ +
+
+

Top videos

+ + + + + + + + + + +
Video IDViews
Loading…
+
+ +
+

Platforms

+

Browsers

+
    +
  • Loading…
  • +
+

Operating systems

+
    +
  • Loading…
  • +
+
+
+ +
+

API usage

+

+ • Human view (this page): /api/stats
+ • JSON view (for scripts/tools): /api/stats?view=json

+ + `) - } - - const raw = fs.readFileSync(statsFile, "utf8") - const data = JSON.parse(raw) - - // ensure structure exists - if (!data.videos) data.videos = {} - if (!data.browsers) data.browsers = {} - if (!data.os) data.os = {} - if (!data.users) data.users = {} - - const sortedVideos = Object.entries(data.videos) - .sort((a, b) => b[1] - a[1]) - .slice(0, 10) - - const topVideos = Object.fromEntries(sortedVideos) - - res.json({ - videos: topVideos, - browsers: data.browsers, - os: data.os, - totalUsers: Object.keys(data.users).length - }) }) - app.get("/avatars/:v", async function (req, res) { var url = `https://yt3.ggpht.com/${req.params.v}`;