feat: add delete all to delete-coop

main
spacemeowx2 2022-11-25 20:41:02 +08:00
parent a4d4f23263
commit 84752ea427
1 changed files with 49 additions and 11 deletions

View File

@ -1,20 +1,58 @@
import { USERAGENT } from "../src/constant.ts"; import { USERAGENT } from "../src/constant.ts";
import { readline } from "../src/utils.ts";
const [key, ...uuids] = Deno.args; const [key, ...params] = Deno.args;
if (!key || uuids.length === 0) { if (!key || params.length === 0) {
console.log("Usage: delete-coop.ts <key> <uuid> <uuid...>"); console.log("Usage: delete-coop.ts <key> <uuid> <uuid...>");
console.log(" or: delete-coop.ts <key> @<stat.ink's id>");
console.log(
" You can find your id at here: https://stat.ink/@YOUR_ID_HERE",
);
Deno.exit(1); Deno.exit(1);
} }
for (const uuid of uuids) { let deleted = 0;
console.log("Deleting", uuid); if (params.length === 1 && params[0].startsWith("@")) {
const resp = await fetch(`https://stat.ink/api/v3/salmon/${uuid}`, { const [uid] = params;
method: "DELETE", console.warn("This script will delete all your coop battles.");
headers: { console.warn("Are you sure? (y/N)");
"Authorization": `Bearer ${key}`, const answer = await readline({
"User-Agent": USERAGENT, skipEmpty: false,
},
}); });
if (answer !== "y") {
Deno.exit(0);
}
console.log(resp.status); while (true) {
const res = await (await fetch(`https://stat.ink/${uid}/salmon3`)).text();
const re = /href="\/@\w+\/salmon3\/([0-9a-fA-F-]{36})/g;
const matches = [...res.matchAll(re)].map((m) => m[1]);
if (matches.length === 0) {
break;
}
await deleteUuids(matches);
deleted += matches.length;
}
} else {
await deleteUuids(params);
deleted += params.length;
}
console.log(`Deleted ${deleted} coop battles.`);
async function deleteUuids(uuids: string[]) {
for (const uuid of uuids) {
console.log("Deleting", uuid);
const resp = await fetch(`https://stat.ink/api/v3/salmon/${uuid}`, {
method: "DELETE",
headers: {
"Authorization": `Bearer ${key}`,
"User-Agent": USERAGENT,
},
});
console.log(resp.status);
}
} }