Files
libretro/scripts/site_insights.py
T
Abdessamad Derraz 84f749fda9 feat: read GoatCounter counts from its export
Paging /stats/hits by exclude_paths spent more than the 500 requests
GoatCounter allows in an hour on four weeks. The JSON export holds the
same hourly counts by path and referrer: on 2026-11-10 to 16 it gave
67,877 views and 24,343 actions, the figures /stats/total gives, with
identical top paths and referrers. One export an hour is allowed.
2026-11-20 07:17:36 +01:00

100 lines
4.0 KiB
Python

#!/usr/bin/env python3
"""What readers look for and do not find, from totals only.
Four sources, none of which names a person:
- Google Search Console: the searches that showed the site, by page,
against the period of the same length just before.
- GoatCounter: page views, the domains readers came from, and the actions
website/src/scripts/count.ts counts (searches that found nothing,
"Did this page help?", downloads, broken links). wiki/privacy.md says
what is sent.
- GitHub: repository traffic (14 days) and release download counts,
all-time counters compared with the snapshot the previous run left in
.cache/insights/downloads.json.
- The collection: which searched file names it holds, and which no
emulator profile declares.
The report is Markdown on stdout. Credentials stay outside the repository:
~/.config/retrobios/gsc-service-account.json Search Console, read only
~/.config/retrobios/goatcounter-api-token.txt GoatCounter, statistics and export
GITHUB_TOKEN or gh auth token GitHub
Usage:
python scripts/site_insights.py
python scripts/site_insights.py --days 90 --output report.md
python scripts/site_insights.py --skip gsc,github
python scripts/site_insights.py --downloads-snapshot tmp/downloads.json
"""
from __future__ import annotations
import argparse
import datetime as dt
import os
import sys
from dataclasses import replace
from pathlib import Path
sys.path.insert(0, os.path.dirname(__file__))
from model.database import load_database
from model.profiles import load_emulator_profiles
from core.cli import run_cli
from core.paths import REPO_ROOT
from sitegen.insights import (
SOURCES,
Readings,
collection_names,
downloads_snapshot,
fetch_github,
fetch_goatcounter,
fetch_queries,
load_snapshot,
report,
save_snapshot,
)
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0], allow_abbrev=False)
parser.add_argument("--days", type=int, default=28, help="days to read, ending 3 days ago (default 28)")
parser.add_argument("--skip", default="", help=f"sources to leave out, among {', '.join(SOURCES)}")
parser.add_argument("--output", type=Path, help="write the report here instead of stdout")
parser.add_argument("--downloads-snapshot", type=Path, default=REPO_ROOT / ".cache" / "insights" / "downloads.json",
help="release download counts of the previous run, rewritten after each run")
args = parser.parse_args()
skip = {name.strip() for name in args.skip.split(",") if name.strip()}
unknown = skip - set(SOURCES)
if unknown:
parser.error(f"unknown source(s): {', '.join(sorted(unknown))}")
if args.days < 1:
parser.error("--days must be at least 1")
# Search Console data settles two to three days after the fact.
end = dt.date.today() - dt.timedelta(days=3)
start = end - dt.timedelta(days=args.days - 1)
held, declared = collection_names(load_database(str(REPO_ROOT / "database.json")),
load_emulator_profiles(str(REPO_ROOT / "emulators")))
now = dt.datetime.now(dt.timezone.utc)
readings = Readings(start, end, now, held, declared)
if "gsc" not in skip:
readings = replace(readings, queries=fetch_queries(start, end),
previous=fetch_queries(*readings.previous_span))
if "goatcounter" not in skip:
readings = replace(readings, goat=fetch_goatcounter(start, now.date()), goat_end=now.date())
if "github" not in skip:
readings = replace(readings, github=fetch_github(), snapshot=load_snapshot(args.downloads_snapshot))
text = report(readings)
if args.output:
args.output.write_text(text, encoding="utf-8")
print(f"Wrote {args.output}", file=sys.stderr)
else:
sys.stdout.write(text)
if readings.github is not None:
save_snapshot(args.downloads_snapshot, downloads_snapshot(readings.github["downloads"], now))
return 0
if __name__ == "__main__":
run_cli(main)