diff --git a/build.py b/build.py index 02854ef..b287579 100644 --- a/build.py +++ b/build.py @@ -52,6 +52,20 @@ def latest_release(full_name): raise +def total_downloads(full_name): + total = 0 + page = 1 + while True: + batch = api(f"/repos/{full_name}/releases?per_page=100&page={page}") + if not batch: + break + for release in batch: + for asset in release.get("assets", []): + total += asset.get("download_count", 0) + page += 1 + return total + + def zip_asset(release): for asset in release.get("assets", []): if asset["name"].lower().endswith(".zip"): @@ -138,7 +152,8 @@ def build_mod(repo): print(f"skip {repo['name']}: no DLL at the top of the zip") shutil.rmtree(OUT.joinpath(*base.parts), ignore_errors=True) return None - print(f"mod {mod_id} {version} ({len(files)} files)") + downloads = total_downloads(repo["full_name"]) + print(f"mod {mod_id} {version} ({len(files)} files, {downloads} downloads)") return { "id": mod_id, "name": meta.get("name") or mod_id, @@ -152,6 +167,7 @@ def build_mod(repo): "releaseUrl": release["html_url"], "downloadUrl": asset["browser_download_url"], "published": release.get("published_at"), + "downloads": downloads, "base": f"{base}/", "dll": dll, "manifestPath": manifest_path, diff --git a/site/app.js b/site/app.js index c962a37..eef8a37 100644 --- a/site/app.js +++ b/site/app.js @@ -275,10 +275,15 @@ function installRedloader() { }); } +function countDownload(mod) { + if (mod.downloadUrl) fetch(mod.downloadUrl, { mode: "no-cors", cache: "no-store" }).catch(() => {}); +} + function installMod(mod) { return run(async () => { const before = state.statuses.get(mod.id); await installPackage(mod, mod.roots); + countDownload(mod); const message = before && before.installed ? `${mod.name} updated to ${mod.version}.` : `${mod.name} ${mod.version} installed.`; setStatus(message, "ok"); }); @@ -294,7 +299,10 @@ function removeMod(mod) { function updateAll(mods) { return run(async () => { - for (const mod of mods) await installPackage(mod, mod.roots); + for (const mod of mods) { + await installPackage(mod, mod.roots); + countDownload(mod); + } setStatus(`Updated ${mods.length} mod${mods.length === 1 ? "" : "s"}.`, "ok"); }); } @@ -430,6 +438,7 @@ function modRow(mod) { mod.description ? el("p", { class: "desc" }, mod.description) : null, el("p", { class: "meta" }, versionLine(mod, status), + typeof mod.downloads === "number" ? el("span", {}, `${mod.downloads.toLocaleString()} downloads`) : null, el("a", { href: mod.releaseUrl, target: "_blank", rel: "noopener" }, supportsFs ? "Release notes" : "Download"), ), ),