s3si.ts/scripts/delete-coop.ts

61 lines
1.6 KiB
TypeScript
Raw Normal View History

import { USERAGENT } from "../src/constant.ts";
2022-11-25 07:41:02 -05:00
import { readline } from "../src/utils.ts";
2022-11-25 07:41:02 -05:00
const [key, ...params] = Deno.args;
if (!key || params.length === 0) {
console.log("Usage: delete-coop.ts <key> <uuid> <uuid...>");
2022-11-30 02:43:33 -05:00
console.log(" or: delete-coop.ts <key> @<stat.ink's id> <count>");
2022-11-25 07:41:02 -05:00
console.log(
" You can find your id at here: https://stat.ink/@YOUR_ID_HERE",
);
Deno.exit(1);
}
2022-11-25 07:41:02 -05:00
let deleted = 0;
2022-11-30 02:43:33 -05:00
if (params.length === 2 && params[0].startsWith("@")) {
const [uid, countStr] = params;
const count = parseInt(countStr);
console.warn(`This script will delete your first ${count} coop battles.`);
2022-11-25 07:41:02 -05:00
console.warn("Are you sure? (y/N)");
const answer = await readline({
skipEmpty: false,
});
2022-11-25 07:41:02 -05:00
if (answer !== "y") {
Deno.exit(0);
}
2022-11-25 07:41:02 -05:00
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]);
2022-11-30 02:43:33 -05:00
const toDelete = matches.slice(0, count - deleted);
2022-11-25 07:41:02 -05:00
2022-11-30 02:43:33 -05:00
if (toDelete.length === 0) {
2022-11-25 07:41:02 -05:00
break;
}
2022-11-30 02:43:33 -05:00
await deleteUuids(toDelete);
deleted += toDelete.length;
2022-11-25 07:41:02 -05:00
}
} 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);
}
}