Skip to content

Instantly share code, notes, and snippets.

@vincerubinetti
Last active May 3, 2025 09:20
Show Gist options
  • Save vincerubinetti/98bb0b23e915db5db5916f3e1ff5d9fe to your computer and use it in GitHub Desktop.
Save vincerubinetti/98bb0b23e915db5db5916f3e1ff5d9fe to your computer and use it in GitHub Desktop.
Run in dev console on Bandcamp stats page to download plays and download stats
const data = JSON.parse(document.getElementById("pagedata").dataset.blob);
const user_id = data.identities.user.id;
const band_id = data.identities.page_band.id;
const end_date = Math.floor(new Date().getTime() / 1000);
const url = new URL(window.location.origin);
url.searchParams.set("user_id", user_id);
url.searchParams.set("band_id", band_id);
url.searchParams.set("alltime", 1);
url.searchParams.set("end_date", end_date);
const sum = (a) => a.reduce((s, v) => s + v, 0);
const pick = (a, i) => a.map((e) => e[i]);
const sort = (a, k) =>
a.toSorted((a, b) => (a[k] > b[k] ? 1 : a[k] < b[k] ? -1 : 0));
const get = async () => (await (await fetch(url)).json()).g.breakout_data;
url.pathname = "api/bandstats/2/plays";
const plays = sort(
(await get()).map(({ title, data }) => ({
title,
complete: sum(pick(data, 0)),
partial: sum(pick(data, 1)),
skip: sum(pick(data, 2)),
total: sum(pick(data, 5)),
})),
"title"
);
url.pathname = "api/bandstats/2/downloads";
const downloads = sort(
(await get()).map(({ title, kind, data }) => ({
title,
kind: kind === 2 ? "album" : "song",
downloads: sum(pick(data, 0)),
purchases: sum(pick(data, 1)),
sales: sum(pick(data, 2)),
})),
"title"
);
const blob = new Blob([JSON.stringify({ plays, downloads }, null, 2)], {
type: "application/json",
});
const link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = "stats.json";
link.click();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment