add single file support
This commit is contained in:
parent
57c58278ab
commit
a2cc4c7793
1 changed files with 99 additions and 56 deletions
147
src/sendgb.js
147
src/sendgb.js
|
|
@ -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 };
|
||||||
|
|
||||||
|
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",
|
method: "POST",
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
action: "download",
|
action: "download",
|
||||||
secret_code: secret,
|
secret_code: secret,
|
||||||
private_id: "",
|
private_id: "",
|
||||||
download_id: url.replace(base, ""),
|
download_id: url.replace(base, ""),
|
||||||
total_files
|
total_files,
|
||||||
}),
|
}),
|
||||||
headers: {
|
headers: {
|
||||||
"User-Agent": "Mozilla/5.0"
|
"User-Agent": "Mozilla/5.0",
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!res.ok || !res.body) {
|
await download(res);
|
||||||
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]
|
|
||||||
?? `${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`);
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue