add single file support
This commit is contained in:
parent
57c58278ab
commit
a2cc4c7793
1 changed files with 99 additions and 56 deletions
163
src/sendgb.js
163
src/sendgb.js
|
|
@ -1,74 +1,25 @@
|
||||||
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),
|
||||||
};
|
};
|
||||||
|
|
||||||
const base = "https://sendgb.com/";
|
async function download(res, rfilename) {
|
||||||
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) {
|
if (!res.ok || !res.body) {
|
||||||
C.err(`Download failed (${res.status})`);
|
C.err(`Download failed (${res.status})`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
C.info("Content-type: " + res.headers.get("content-type"))
|
C.info("Content-type: " + res.headers.get("content-type"));
|
||||||
const filename =
|
const filename =
|
||||||
res.headers.get("content-disposition")?.match(/filename="?([^"]+)"?/)?.[1]
|
rfilename ??
|
||||||
?? `${url.replace(base, '')}-sendgb.zip`;
|
res.headers
|
||||||
|
.get("content-disposition")
|
||||||
|
?.match(/filename="?([^"]+)"?/)?.[1] ??
|
||||||
|
`${url.replace(base, "")}-sendgb.zip`;
|
||||||
|
|
||||||
C.info(`Downloading ${filename}`);
|
C.info(`Downloading ${filename}`);
|
||||||
|
|
||||||
|
|
@ -101,3 +52,95 @@ file.end();
|
||||||
|
|
||||||
process.stdout.write("\n");
|
process.stdout.write("\n");
|
||||||
C.ok(`Saved as ${filename} in ${((Date.now() - start) / 1000).toFixed(1)}s`);
|
C.ok(`Saved as ${filename} in ${((Date.now() - start) / 1000).toFixed(1)}s`);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (link == "#") {
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let cf_link = { success: false };
|
||||||
|
|
||||||
|
try {
|
||||||
|
cf_link = await res_cf_link.json();
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (cf_link.success) {
|
||||||
|
C.info("Cloudflare link succesfully retrieved.");
|
||||||
|
const res = await fetch(cf_link.url);
|
||||||
|
|
||||||
|
await download(res, filename.replace("/", "_"));
|
||||||
|
} else {
|
||||||
|
C.err("Could not succesfully retrieve cloudflare link.");
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue