add single file support

This commit is contained in:
Soph :3 2025-12-31 16:00:04 +02:00
parent 57c58278ab
commit a2cc4c7793

View file

@ -1,12 +1,58 @@
import fs from "fs"; import fs from "fs";
const C = { const C = {
info: msg => console.log("\x1b[36m[i]\x1b[0m " + msg), info: (msg) => console.log("\x1b[36m[i]\x1b[0m " + msg),
ok: msg => console.log("\x1b[32m[✓]\x1b[0m " + msg), ok: (msg) => console.log("\x1b[32m[✓]\x1b[0m " + msg),
warn: msg => console.log("\x1b[33m[!]\x1b[0m " + msg), warn: (msg) => console.log("\x1b[33m[!]\x1b[0m " + msg),
err: msg => console.log("\x1b[31m[✗]\x1b[0m " + msg), err: (msg) => console.log("\x1b[31m[✗]\x1b[0m " + msg),
}; };
async function download(res, rfilename) {
if (!res.ok || !res.body) {
C.err(`Download failed (${res.status})`);
process.exit(1);
}
C.info("Content-type: " + res.headers.get("content-type"));
const filename =
rfilename ??
res.headers
.get("content-disposition")
?.match(/filename="?([^"]+)"?/)?.[1] ??
`${url.replace(base, "")}-sendgb.zip`;
C.info(`Downloading ${filename}`);
const file = fs.createWriteStream(filename);
const reader = res.body.getReader();
let downloaded = 0;
const total = Number(res.headers.get("content-length")) || 0;
const start = Date.now();
while (true) {
const { done, value } = await reader.read();
if (done) break;
file.write(value);
downloaded += value.length;
if (total) {
const pct = ((downloaded / total) * 100).toFixed(1);
const mb = (downloaded / 1024 / 1024).toFixed(2);
const tmb = (total / 1024 / 1024).toFixed(2);
process.stdout.write(`\r${pct}% ${mb}/${tmb} MB`);
} else {
const mb = (downloaded / 1024 / 1024).toFixed(2);
process.stdout.write(`\r${mb} MB`);
}
}
file.end();
process.stdout.write("\n");
C.ok(`Saved as ${filename} in ${((Date.now() - start) / 1000).toFixed(1)}s`);
}
const base = "https://sendgb.com/"; const base = "https://sendgb.com/";
const url = process.argv[2]; const url = process.argv[2];
@ -22,7 +68,7 @@ const html = await page.text();
const sendgb_session = page.headers const sendgb_session = page.headers
.getSetCookie() .getSetCookie()
.find(c => c.startsWith("sendgb_ses=")) .find((c) => c.startsWith("sendgb_ses="))
?.split(";")[0] ?.split(";")[0]
.split("=") .split("=")
.at(-1); .at(-1);
@ -43,61 +89,58 @@ if (!link || !secret || !total_files) {
process.exit(1); process.exit(1);
} }
C.info(`Files: ${total_files}`); if (link == "#") {
C.info("Requesting ZIP…"); const filename = get(
/<div class="fw-bold dlfl" title="[^"]*">([^<]*)<\/div>/gm,
);
C.info(`File: ${filename}`);
C.info("Requesting file…");
const res_cf_link = await fetch(
"https://www.sendgb.com/src/download_one.php?uploadId=" +
url.replace(base, "") +
"&sc=" +
secret +
"&file=" +
encodeURIComponent(filename) +
"&private_id=",
{
headers: {
"User-Agent": "Mozilla/5.0",
},
},
);
const res = await fetch(link, { let cf_link = { success: false };
method: "POST",
body: new URLSearchParams({
action: "download",
secret_code: secret,
private_id: "",
download_id: url.replace(base, ""),
total_files
}),
headers: {
"User-Agent": "Mozilla/5.0"
}
});
if (!res.ok || !res.body) { try {
C.err(`Download failed (${res.status})`); cf_link = await res_cf_link.json();
process.exit(1); } catch {}
}
C.info("Content-type: " + res.headers.get("content-type")) if (cf_link.success) {
const filename = C.info("Cloudflare link succesfully retrieved.");
res.headers.get("content-disposition")?.match(/filename="?([^"]+)"?/)?.[1] const res = await fetch(cf_link.url);
?? `${url.replace(base, '')}-sendgb.zip`;
C.info(`Downloading ${filename}`); await download(res, filename.replace("/", "_"));
const file = fs.createWriteStream(filename);
const reader = res.body.getReader();
let downloaded = 0;
const total = Number(res.headers.get("content-length")) || 0;
const start = Date.now();
while (true) {
const { done, value } = await reader.read();
if (done) break;
file.write(value);
downloaded += value.length;
if (total) {
const pct = ((downloaded / total) * 100).toFixed(1);
const mb = (downloaded / 1024 / 1024).toFixed(2);
const tmb = (total / 1024 / 1024).toFixed(2);
process.stdout.write(`\r${pct}% ${mb}/${tmb} MB`);
} else { } else {
const mb = (downloaded / 1024 / 1024).toFixed(2); C.err("Could not succesfully retrieve cloudflare link.");
process.stdout.write(`\r${mb} MB`);
} }
} else {
C.info(`Files: ${total_files}`);
C.info("Requesting ZIP…");
const res = await fetch(link, {
method: "POST",
body: new URLSearchParams({
action: "download",
secret_code: secret,
private_id: "",
download_id: url.replace(base, ""),
total_files,
}),
headers: {
"User-Agent": "Mozilla/5.0",
},
});
await download(res);
} }
file.end();
process.stdout.write("\n");
C.ok(`Saved as ${filename} in ${((Date.now() - start) / 1000).toFixed(1)}s`);