add sendgb downloader like a boss
This commit is contained in:
parent
b21f6594a8
commit
e61343f04f
2 changed files with 105 additions and 2 deletions
|
|
@ -8,8 +8,8 @@
|
||||||
"deezify": "bun run src/deezify.js",
|
"deezify": "bun run src/deezify.js",
|
||||||
"now-playing": "bun run src/now-playing.js",
|
"now-playing": "bun run src/now-playing.js",
|
||||||
"request-bot": "bun run src/request-bot.js",
|
"request-bot": "bun run src/request-bot.js",
|
||||||
"pillow": "bun run src/pillow.js"
|
"pillow": "bun run src/pillow.js",
|
||||||
|
"sendgb": "bun run src/sendgb.js"
|
||||||
},
|
},
|
||||||
"engineStrict": true,
|
"engineStrict": true,
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
||||||
103
src/sendgb.js
Normal file
103
src/sendgb.js
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
const C = {
|
||||||
|
info: msg => console.log("\x1b[36m[i]\x1b[0m " + msg),
|
||||||
|
ok: msg => console.log("\x1b[32m[✓]\x1b[0m " + msg),
|
||||||
|
warn: msg => console.log("\x1b[33m[!]\x1b[0m " + msg),
|
||||||
|
err: msg => console.log("\x1b[31m[✗]\x1b[0m " + msg),
|
||||||
|
};
|
||||||
|
|
||||||
|
const base = "https://sendgb.com/";
|
||||||
|
const url = process.argv[2];
|
||||||
|
|
||||||
|
if (!url || !url.startsWith(base)) {
|
||||||
|
C.err("Usage: node sendgb.js <sendgb-url>");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
C.info(`Fetching page: ${url}`);
|
||||||
|
|
||||||
|
const page = await fetch(url);
|
||||||
|
const html = await page.text();
|
||||||
|
|
||||||
|
const sendgb_session = page.headers
|
||||||
|
.getSetCookie()
|
||||||
|
.find(c => c.startsWith("sendgb_ses="))
|
||||||
|
?.split(";")[0]
|
||||||
|
.split("=")
|
||||||
|
.at(-1);
|
||||||
|
|
||||||
|
if (!sendgb_session) {
|
||||||
|
C.err("Failed to extract sendgb session cookie");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const get = (re) => [...html.matchAll(re)][0]?.[1];
|
||||||
|
|
||||||
|
const link = get(/<form id="downloadItems" action="([^"]*)"/gm);
|
||||||
|
const secret = get(/name="secret_code"[^>]*value="([^"]*)"/gm);
|
||||||
|
const total_files = get(/name="total_files"[^>]*value="(\d+)"/gm);
|
||||||
|
|
||||||
|
if (!link || !secret || !total_files) {
|
||||||
|
C.err("Failed to parse download form");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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 =
|
||||||
|
res.headers.get("content-disposition")?.match(/filename="?([^"]+)"?/)?.[1]
|
||||||
|
?? "download.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`);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue