import fs from "fs"; import randomstring from "randomstring"; const TEMP_DIR = "./temp-tts"; const FAILED_LOG = TEMP_DIR + "/failed.log"; const sub_name = randomstring.generate({ length: 5, charset: "alphanumeric" }); const HOST = "http://85.31.224.193:4000"; // tidur const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms)); // cek apakah file sudah ada function fileExists(path: string) { return fs.existsSync(path) && fs.statSync(path).size > 44; } // tulis log gagal function logFail(msg: string) { fs.appendFileSync(FAILED_LOG, msg + "\n"); } async function downloadWithRetry(jobId: string, outFile: string) { const MAX_RETRY = 5; // jika sebelumnya sudah ada → skip if (fileExists(outFile)) { console.log(` šŸ” Resume: file sudah ada → ${outFile}`); return true; } for (let attempt = 1; attempt <= MAX_RETRY; attempt++) { try { console.log(` ā³ Download ${jobId} (try ${attempt}/${MAX_RETRY})...`); const res = await fetch(`${HOST}/file/${jobId}`); if (!res.ok) { throw new Error(`Status ${res.status}`); } const buf = Buffer.from(await res.arrayBuffer()); // pastikan ukurannya wajar if (buf.length < 44) { throw new Error("File terlalu kecil (korup?)"); } fs.writeFileSync(outFile, buf); console.log(` āœ“ Success → ${outFile}`); return true; } catch (err: any) { console.error(` āŒ Error: ${err.message}`); if (attempt < MAX_RETRY) { console.log(" šŸ”„ Retry dalam 2s..."); await sleep(2000); } } } console.error(` āŒ Gagal total untuk ${jobId}`); logFail(jobId); return false; } async function main() { const jobsPath = TEMP_DIR + "/jobs.json"; if (!fs.existsSync(jobsPath)) { console.error("āŒ jobs.json not found"); process.exit(1); } const jobs = JSON.parse(fs.readFileSync(jobsPath, "utf-8")); console.log(`šŸ“¦ Total jobs: ${jobs.length}`); const resultFiles: string[] = []; for (let i = 0; i < jobs.length; i++) { const jobId = jobs[i]; const outFile = `${TEMP_DIR}/${jobId}_${sub_name}_${(i + 1) .toString() .padStart(4, "0")}.wav`; console.log(`\nā–¶ļø Job ${i + 1}/${jobs.length} → ${jobId}`); const success = await downloadWithRetry(jobId, outFile); if (success) { resultFiles.push(outFile); } } console.log("\nšŸŽ‰ DONE!"); console.log("Result files:", resultFiles); return resultFiles; } if (import.meta.main) { main().then((files) => { console.log("\nšŸ“ Final:", files.length, "file tersimpan."); }); }