From 2f468765591ab7821e64527238dbfb3003664bac Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 6 Nov 2025 23:14:29 +0100 Subject: [PATCH] Update src/libpoketube/init/pages-api.js --- src/libpoketube/init/pages-api.js | 337 ++++++++++++------------------ 1 file changed, 139 insertions(+), 198 deletions(-) diff --git a/src/libpoketube/init/pages-api.js b/src/libpoketube/init/pages-api.js index efadfea1..1c880600 100644 --- a/src/libpoketube/init/pages-api.js +++ b/src/libpoketube/init/pages-api.js @@ -100,12 +100,11 @@ if (!data.users) data.users = {} fs.writeFileSync(statsFile, JSON.stringify(data, null, 2)) res.json({ ok: true }) }) - -app.get("/api/stats", (req, res) => { + app.get("/api/stats", (req, res) => { const view = (req.query.view || "").toString() - // JSON view (for programmatic access) - if (view === "json") { + // JSON view (default) – for programmatic access + if (!view || view === "json") { if (!telemetryConfig.telemetry) { return res.json({ videos: {}, browsers: {}, os: {}, totalUsers: 0 }) } @@ -132,179 +131,116 @@ app.get("/api/stats", (req, res) => { }) } - const telemetryOn = telemetryConfig.telemetry + // Human view – /api/stats?view=human + if (view === "human") { + const telemetryOn = telemetryConfig.telemetry - res.send(` + return res.send(` - Poke – Stats + Improving Poke -
-

Poke stats

+
+ Poke logo +

Improving Poke

+

Private by design

+

- - 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. - + At Poke, we do not collect or share any personal information. + That's our privacy promise in a nutshell. + To improve Poke we use a completely anonymous, local-only way to figure out how the site is being used.

-
-

Status

-

-

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

-
+

+ Any anonymous stats recorded by this instance come from the /api/stats system. + You can read exactly what is measured (and what is not) in our privacy policy: + /policies/privacy#stats. +

-
-

Overview

-

Loading stats…

-
+
-
-
-

Top videos

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

Current anonymous stats

+

Loading…

+
    -
    -

    Platforms

    -

    Browsers

    -
      -
    • Loading…
    • -
    -

    Operating systems

    -
      -
    • Loading…
    • -
    -
    -
    +

    Top videos (local-only)

    +

    Up to 10 most watched videos on this instance.

    +
      -
      -

      API usage

      -

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

      -
      -
      +
      + +

      API usage

      +

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

      + `) + } + + // any other view value -> fallback to 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) + + res.json({ + videos: topVideos, + browsers: data.browsers, + os: data.os, + totalUsers: Object.keys(data.users).length + }) })