1- # Refreshes the per-platform desktop download counters behind the README badges.
1+ # Refreshes the desktop and combined download counters behind the README badges.
22#
33# shields.io cannot do this on its own: its asset wildcards silently report 0
44# (a tag whose .dmg had 39,877 downloads returns 0 for `*.dmg`), exact asset
1111name : Desktop Download Badges
1212
1313on :
14+ push :
15+ branches : [main]
16+ paths :
17+ - .github/workflows/desktop-download-badges.yml
1418 schedule :
1519 # Once a day is plenty; the counters move slowly and the API is rate-limited.
1620 - cron : ' 17 4 * * *'
3337 ref : badges
3438 persist-credentials : true
3539
36- - name : Sum the .dmg and .exe download counts
40+ - name : Refresh download counts
3741 env :
3842 GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
3943 # Desktop installers shipped from this repo up to v0.1.0 and move to
5256
5357 # An empty suffix matches every asset, which is what the combined
5458 # counter wants; `endswith("")` is true for any string.
55- write_badge() {
59+ write_endpoint() {
60+ local label="$1" message="$2" out="$3"
61+ jq -n --arg label "$label" --arg message "$message" '{
62+ schemaVersion: 1,
63+ label: $label,
64+ message: $message,
65+ color: "4D6BFE"
66+ }' > "$out"
67+ echo "${label}: ${message}"
68+ }
69+
70+ write_desktop_badge() {
5671 local suffix="$1" label="$2" out="$3"
5772 local total
5873 total="$(jq --arg s "$suffix" '
@@ -61,20 +76,29 @@ jobs:
6176 | .download_count
6277 ] | add // 0
6378 ' releases.json)"
64- jq -n --arg label "$label" --arg message "$total" '{
65- schemaVersion: 1,
66- label: $label,
67- message: $message,
68- color: "4D6BFE"
69- }' > "$out"
70- echo "${label}: ${total}"
79+ write_endpoint "$label" "$total" "$out"
7180 }
7281
73- write_badge '.dmg' 'macOS .dmg' desktop-dmg.json
74- write_badge '.exe' 'Windows .exe' desktop-exe.json
82+ write_desktop_badge '.dmg' 'macOS .dmg' desktop-dmg.json
83+ write_desktop_badge '.exe' 'Windows .exe' desktop-exe.json
7584 # The shields `github/downloads/.../total` route reads one repository,
7685 # so the combined counter has to be summed here like the other two.
77- write_badge '' 'desktop' desktop-total.json
86+ write_desktop_badge '' 'desktop' desktop-total.json
87+
88+ # Match the site: desktop and npm can represent the same users, so
89+ # publish the larger counter instead of adding them together.
90+ npm_downloads="$(
91+ curl --fail --silent --show-error --location \
92+ --retry 3 --retry-all-errors --connect-timeout 10 --max-time 30 \
93+ 'https://api.npmjs.org/downloads/point/last-year/%40pymodel%2Fpythinker-code' \
94+ | jq -er '.downloads | numbers | select(. >= 0 and floor == .)'
95+ )"
96+ desktop_downloads="$(jq -er '.message | tonumber' desktop-total.json)"
97+ downloads="$(
98+ jq -n --argjson desktop "$desktop_downloads" --argjson npm "$npm_downloads" \
99+ '[$desktop, $npm] | max'
100+ )"
101+ write_endpoint 'downloads' "$downloads" downloads-total.json
78102
79103 rm -f releases.json
80104
@@ -85,10 +109,10 @@ jobs:
85109 git config user.email 'github-actions[bot]@users.noreply.github.com'
86110 # Stage before comparing: a document added in this run is untracked,
87111 # and `git diff` on an untracked path reports no change at all.
88- git add desktop-dmg.json desktop-exe.json desktop-total.json
112+ git add desktop-dmg.json desktop-exe.json desktop-total.json downloads-total.json
89113 if git diff --cached --quiet; then
90114 echo 'Counts unchanged; nothing to publish.'
91115 exit 0
92116 fi
93- git commit -m 'chore: refresh desktop download counts'
117+ git commit -m 'chore: refresh download counts'
94118 git push origin badges
0 commit comments