jesus christ
This commit is contained in:
1 parent
d97600122e
commit
49309d6733
9 files changed
+505
-295
No files matched your search
+80
-57
@@ -14,52 +14,74 @@ const OUTPUT_DIR = process.env.OUTPUT_DIR || "/mnt/media/WIP/";
|
||||
const client = new MongoClient(MONGO_URI);
|
||||
let queueCol, doneCol, failedCol;
|
||||
|
||||
// ========================
|
||||
// HELPER
|
||||
// ========================
|
||||
|
||||
async function isBlocked() {
|
||||
const settings = await queueCol.db
|
||||
.collection("settings")
|
||||
.findOne({ _id: "global" });
|
||||
return settings?.captcha_blocked === true;
|
||||
}
|
||||
|
||||
// ========================
|
||||
// RUN A JOB
|
||||
// ========================
|
||||
function runJob(job) {
|
||||
return new Promise((resolve) => {
|
||||
const venvPython = process.env.VENV_PYTHON
|
||||
async function runJob(job) {
|
||||
try {
|
||||
const cmd = [
|
||||
"python3",
|
||||
path.join(__dirname, "proxer-dl-video.py"),
|
||||
"-u", job.url,
|
||||
"-f", job.filename,
|
||||
"-t", job.token,
|
||||
"-o", process.env.OUTPUT_DIR
|
||||
];
|
||||
|
||||
const cleanName = job.filename.replace(".mp4", "");
|
||||
const safeToken = job.token.replace(/^"+|"+$/g, '');
|
||||
const scriptPath = 'scripts/proxer-dl-video.py'
|
||||
const command = `${venvPython} ${scriptPath} -u "${job.url}" -f "${cleanName}" -t "${safeToken}" -o "${OUTPUT_DIR}"`;
|
||||
const proc = spawn(cmd[0], cmd.slice(1));
|
||||
|
||||
console.log("Running:", command);
|
||||
let output = "";
|
||||
proc.stdout.on("data", d => output += d.toString());
|
||||
proc.stderr.on("data", d => output += d.toString());
|
||||
|
||||
exec(command, async (error, stdout, stderr) => {
|
||||
const outputLog = `${stdout}\n${stderr}`.trim();
|
||||
const exitCode = await new Promise(resolve =>
|
||||
proc.on("close", resolve)
|
||||
);
|
||||
|
||||
try {
|
||||
if (error) {
|
||||
console.error(`FAILED: ${cleanName}`);
|
||||
await failedCol.insertOne({
|
||||
filename: job.filename,
|
||||
url: job.url,
|
||||
failed_at: new Date(),
|
||||
output_log: outputLog,
|
||||
return_code: error.code,
|
||||
});
|
||||
} else {
|
||||
console.log(`SUCCESS: ${cleanName}`);
|
||||
await doneCol.insertOne({
|
||||
filename: job.filename,
|
||||
url: job.url,
|
||||
finished_at: new Date(),
|
||||
output_log: outputLog,
|
||||
result: "success",
|
||||
});
|
||||
}
|
||||
// Remove from queue in both cases
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
} catch (dbErr) {
|
||||
console.error("DB error:", dbErr);
|
||||
} finally {
|
||||
resolve(); // resolve promise when done
|
||||
}
|
||||
// ========== CAPTCHA / ERROR DETECTION ==========
|
||||
const suspicious =
|
||||
exitCode !== 0 ||
|
||||
output.toLowerCase().includes("captcha") ||
|
||||
output.toLowerCase().includes("403") ||
|
||||
output.toLowerCase().includes("blocked") ||
|
||||
output.toLowerCase().includes("token") ||
|
||||
output.toLowerCase().includes("forbidden");
|
||||
|
||||
if (suspicious) {
|
||||
console.log("⛔ CAPTCHA or fatal error detected, notifying system");
|
||||
await failedCol.insertOne({
|
||||
filename: job.filename,
|
||||
failed_at: new Date(),
|
||||
output_log: output
|
||||
});
|
||||
return true; // signals worker to halt globally
|
||||
}
|
||||
|
||||
// Success
|
||||
await doneCol.insertOne({
|
||||
filename: job.filename,
|
||||
finished_at: new Date(),
|
||||
output_log: output
|
||||
});
|
||||
});
|
||||
|
||||
await queueCol.deleteOne({ _id: job._id });
|
||||
return false;
|
||||
|
||||
} catch (err) {
|
||||
console.error("Worker crash:", err);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,29 +100,31 @@ async function main() {
|
||||
failedCol = db.collection("failed");
|
||||
|
||||
while (true) {
|
||||
if (await isBlocked()) {
|
||||
console.log("⛔ CAPTCHA BLOCK — Worker paused");
|
||||
await new Promise((r) => setTimeout(r, 5000));
|
||||
continue;
|
||||
}
|
||||
|
||||
const job = await queueCol.findOneAndUpdate(
|
||||
{
|
||||
// Only pick jobs NOT being processed
|
||||
$or: [
|
||||
{ processing: { $exists: false } },
|
||||
{ processing: false }
|
||||
]
|
||||
},
|
||||
{
|
||||
// Atomically claim job
|
||||
$set: { processing: true, started_at: new Date() }
|
||||
},
|
||||
{
|
||||
sort: { created_at: 1 },
|
||||
returnDocument: "after"
|
||||
}
|
||||
{ processing: { $ne: true } }, // prevents grabbing others
|
||||
{ $set: { processing: true } },
|
||||
{ sort: { created_at: 1 }, returnDocument: "after" }
|
||||
);
|
||||
|
||||
if (job && job.token) {
|
||||
await runJob(job);
|
||||
const failed = await runJob(job);
|
||||
if (failed === true) {
|
||||
await queueCol.db
|
||||
.collection("settings")
|
||||
.updateOne(
|
||||
{ _id: "global" },
|
||||
{ $set: { captcha_blocked: true } },
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// No available job, wait a bit
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -109,4 +133,3 @@ async function main() {
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
Reference in new issue
Block a user