Downloads badge: count setup downloads on a stats branch
A scheduled workflow (every 6 hours, on each published release, or by hand) adds GitHub's live SmoovPlay-Setup.exe download counts to the counts saved before the release files were re-uploaded on 18 Nov (which reset them), and writes downloads.json (a shields.io endpoint) and history.csv to the stats branch, only when the total changed. The README shows it as a badge.
This commit is contained in:
1 parent
ed0f60fc89
commit
0153ab6246
4 files changed
+143
No files matched your search
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"note": "Setup downloads GitHub counted before the release files were re-uploaded on 18 Nov 2026, which reset its counters. The download count adds these to GitHub's live counts.",
|
||||
"taken": "2026-11-18T18:34Z",
|
||||
"setup_downloads": {
|
||||
"v0.3.2": 81,
|
||||
"v0.3.1": 1360,
|
||||
"v0.3.0": 331,
|
||||
"v0.2.0": 302,
|
||||
"v0.1.0": 29
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
"""The README's download count: setup downloads from GitHub's live counts plus the counts from before they were reset
|
||||
(.github/download-baseline.json), written for a shields.io endpoint badge.
|
||||
|
||||
python .github/scripts/download_count.py STATS_DIR
|
||||
|
||||
STATS_DIR is a checkout of the `stats` branch (empty the first time). When the total has changed (against the last
|
||||
line of history.csv), it rewrites downloads.json (the badge, in shields.io's endpoint format with nothing else in
|
||||
it) and adds a line to history.csv; otherwise it leaves both alone, so there's nothing to commit. Counts SmoovPlay-Setup.exe only: new installs and in-app updates, not people. Uses GITHUB_TOKEN if set."""
|
||||
import csv
|
||||
import datetime as dt
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
import urllib.request
|
||||
|
||||
SETUP = "SmoovPlay-Setup.exe"
|
||||
REPO = os.environ.get("GITHUB_REPOSITORY", "smoovplay/smoovplay")
|
||||
BASELINE = pathlib.Path(__file__).resolve().parent.parent / "download-baseline.json"
|
||||
|
||||
|
||||
def live_counts() -> dict[str, int]:
|
||||
"""Setup downloads per published release, as GitHub counts them now."""
|
||||
out, page = {}, 1
|
||||
while True:
|
||||
req = urllib.request.Request(f"https://api.github.com/repos/{REPO}/releases?per_page=100&page={page}",
|
||||
headers={"Accept": "application/vnd.github+json", "User-Agent": "smoovplay-stats"})
|
||||
if os.environ.get("GITHUB_TOKEN"):
|
||||
req.add_header("Authorization", f"Bearer {os.environ['GITHUB_TOKEN']}")
|
||||
with urllib.request.urlopen(req, timeout=30) as r:
|
||||
releases = json.load(r)
|
||||
for rel in releases:
|
||||
if not rel.get("draft"):
|
||||
out[rel["tag_name"]] = sum(a["download_count"] for a in rel["assets"] if a["name"] == SETUP)
|
||||
if len(releases) < 100:
|
||||
return out
|
||||
page += 1
|
||||
|
||||
|
||||
def short(n: int) -> str:
|
||||
"""2103 -> 2.1k, 12345 -> 12k, 987 -> 987."""
|
||||
if n < 1000:
|
||||
return str(n)
|
||||
if n < 10_000:
|
||||
return f"{n / 1000:.1f}k".replace(".0k", "k")
|
||||
if n < 1_000_000:
|
||||
return f"{n // 1000}k"
|
||||
return f"{n / 1_000_000:.1f}M".replace(".0M", "M")
|
||||
|
||||
|
||||
def main(stats_dir: str) -> int:
|
||||
base = json.loads(BASELINE.read_text(encoding="utf-8"))["setup_downloads"]
|
||||
live = live_counts()
|
||||
total = sum(base.values()) + sum(live.values())
|
||||
d = pathlib.Path(stats_dir)
|
||||
badge, hist = d / "downloads.json", d / "history.csv"
|
||||
rows = list(csv.reader(hist.open(newline="", encoding="utf-8"))) if hist.exists() else []
|
||||
before = int(rows[-1][1]) if len(rows) > 1 else None
|
||||
if before == total:
|
||||
print(f"downloads: {total:,}, unchanged")
|
||||
return 0
|
||||
badge.write_text(json.dumps({"schemaVersion": 1, "label": "downloads", "message": short(total),
|
||||
"color": "3A4250", "cacheSeconds": 3600}, indent=2) + "\n",
|
||||
encoding="utf-8")
|
||||
new = not hist.exists()
|
||||
with hist.open("a", newline="", encoding="utf-8") as f:
|
||||
w = csv.writer(f)
|
||||
if new:
|
||||
w.writerow(["time_utc", "setup_downloads", "by_release"])
|
||||
tags = sorted(set(base) | set(live), key=lambda t: [int(x) for x in t.lstrip("v").split("-")[0].split(".")],
|
||||
reverse=True)
|
||||
w.writerow([dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%dT%H:%MZ"), total,
|
||||
";".join(f"{t}={base.get(t, 0) + live.get(t, 0)}" for t in tags)])
|
||||
print(f"downloads: {before if before is not None else '-'} -> {total:,} ({short(total)})")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv[1]))
|
||||
@@ -0,0 +1,52 @@
|
||||
# Keeps the README's download count up to date: every 6 hours and when a release is published, adds GitHub's live
|
||||
# setup download counts to the ones from before they were reset (.github/download-baseline.json) and, when the total
|
||||
# has changed, commits it to the `stats` branch (downloads.json for the badge, history.csv). Nothing goes to main.
|
||||
name: Download count
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "17 */6 * * *"
|
||||
release:
|
||||
types: [published]
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
concurrency:
|
||||
group: download-count
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
count:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
ref: main
|
||||
fetch-depth: 1
|
||||
- name: Check out the stats branch (or start it)
|
||||
run: |
|
||||
if git ls-remote --exit-code --heads origin stats >/dev/null; then
|
||||
git fetch --depth=1 origin stats
|
||||
git worktree add ../stats FETCH_HEAD
|
||||
git -C ../stats switch -c stats
|
||||
else
|
||||
git worktree add --orphan -b stats ../stats
|
||||
fi
|
||||
- name: Count
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ github.token }}
|
||||
run: python .github/scripts/download_count.py ../stats
|
||||
- name: Commit if it changed
|
||||
working-directory: ../stats
|
||||
run: |
|
||||
git add -A
|
||||
if git diff --cached --quiet; then
|
||||
echo "Unchanged: nothing to commit."
|
||||
exit 0
|
||||
fi
|
||||
total=$(tail -n 1 history.csv | cut -d, -f2)
|
||||
git -c user.name="github-actions[bot]" -c user.email="41898282+github-actions[bot]@users.noreply.github.com" \
|
||||
commit -q -m "Downloads: $total"
|
||||
git push origin stats
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/smoovplay/smoovplay/releases"><img alt="Status: early release" src="https://img.shields.io/badge/status-early%20release-FFB02E?style=flat-square"></a>
|
||||
<a href="https://github.com/smoovplay/smoovplay/releases"><img alt="Downloads of SmoovPlay-Setup.exe, all releases" src="https://img.shields.io/endpoint?url=https%3A%2F%2Fraw.githubusercontent.com%2Fsmoovplay%2Fsmoovplay%2Fstats%2Fdownloads.json&style=flat-square"></a>
|
||||
<img alt="Windows 10 and 11" src="https://img.shields.io/badge/Windows-10%20%7C%2011-3A4250?style=flat-square">
|
||||
<img alt="Graphics: NVIDIA RTX 30, 40 or 50 series; AMD and Intel as an Experimental option" src="https://img.shields.io/badge/GPU-NVIDIA%20RTX%2030%20%7C%2040%20%7C%2050%2C%20AMD%20%26%20Intel%20experimental-3A4250?style=flat-square">
|
||||
<a href="LICENSE"><img alt="MIT licence" src="https://img.shields.io/badge/licence-MIT-3A4250?style=flat-square"></a>
|
||||
|
||||
Reference in new issue
Block a user