Spaces:
Paused
Paused
Update index.js
Browse files
index.js
CHANGED
|
@@ -399,7 +399,134 @@ app.get('/komiku/download', async (req, res) => {
|
|
| 399 |
});
|
| 400 |
}
|
| 401 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
/***********/
|
| 404 |
|
| 405 |
async function getLatestKomik(page) {
|
|
|
|
| 399 |
});
|
| 400 |
}
|
| 401 |
});
|
| 402 |
+
/*
|
| 403 |
+
V2 V2 V2 V2
|
| 404 |
+
*/
|
| 405 |
+
|
| 406 |
+
async function komiku_downloadV2(url) {
|
| 407 |
+
const instanceID = generateRandomID();
|
| 408 |
+
const tempDir = path.join(tempDirBase, instanceID);
|
| 409 |
+
await fss.mkdir(tempDir);
|
| 410 |
+
|
| 411 |
+
// Extracting the title from the URL
|
| 412 |
+
const title = url.split('/').filter(part => part).pop();
|
| 413 |
+
|
| 414 |
+
let browser;
|
| 415 |
+
try {
|
| 416 |
+
browser = await puppeteer.launch({
|
| 417 |
+
headless: true,
|
| 418 |
+
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
| 419 |
+
});
|
| 420 |
+
const page = await browser.newPage();
|
| 421 |
+
await page.goto(url, { waitUntil: 'networkidle2' });
|
| 422 |
+
|
| 423 |
+
// Extracting images from the page
|
| 424 |
+
const imgList = await page.evaluate(() => {
|
| 425 |
+
return Array.from(document.querySelectorAll('#Baca_Komik img')).map(img => img.src);
|
| 426 |
+
});
|
| 427 |
+
|
| 428 |
+
const images = imgList.map(src => ({ path: src }));
|
| 429 |
+
|
| 430 |
+
await processImagesV2(images, tempDir, instanceID);
|
| 431 |
+
const pdfPath = await createPDFV2(instanceID, tempDir);
|
| 432 |
+
|
| 433 |
+
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
| 434 |
+
return { path: pdfPath, title: title, url: `https://${process.env.SPACE_HOST}/temp/${path.basename(pdfPath)}` };
|
| 435 |
+
} catch (error) {
|
| 436 |
+
console.log(error);
|
| 437 |
+
throw error;
|
| 438 |
+
} finally {
|
| 439 |
+
if (browser) {
|
| 440 |
+
await browser.close();
|
| 441 |
+
}
|
| 442 |
+
await fss.rmdir(tempDir, { recursive: true });
|
| 443 |
+
}
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
async function downloadImageV2(image, tempDir, instanceID) {
|
| 447 |
+
const response = await puppeteer.download(image.path, { responseType: 'arraybuffer' });
|
| 448 |
+
const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`);
|
| 449 |
+
await writeFileAsync(imagePath, response);
|
| 450 |
+
|
| 451 |
+
const imageHeight = await getImageHeightV2(imagePath);
|
| 452 |
+
const newHeight = Math.floor(imageHeight * 0.7);
|
| 453 |
+
const command = `convert ${imagePath} -resize 720x${newHeight}! -quality 75 -background white -gravity center -extent 720x${newHeight} ${imagePath}`;
|
| 454 |
+
await execPromise(command);
|
| 455 |
+
|
| 456 |
+
return imagePath;
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
async function getImageHeightV2(imagePath) {
|
| 460 |
+
const { stdout } = await execPromise(`identify -format "%h" ${imagePath}`);
|
| 461 |
+
return parseInt(stdout.trim());
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
async function processImagesV2(imgList, tempDir, instanceID) {
|
| 465 |
+
const maxImagesPerPage = 10; // Maksimal 10 gambar per halaman
|
| 466 |
+
let partIndex = 0;
|
| 467 |
+
let partImages = [];
|
| 468 |
+
|
| 469 |
+
for (let i = 0; i < imgList.length; i++) {
|
| 470 |
+
const imagePath = await downloadImageV2(imgList[i], tempDir, instanceID);
|
| 471 |
+
partImages.push(imagePath);
|
| 472 |
+
|
| 473 |
+
if (partImages.length >= maxImagesPerPage) {
|
| 474 |
+
await combineAndSaveV2(partImages, partIndex, tempDir, instanceID);
|
| 475 |
+
partImages = [];
|
| 476 |
+
partIndex++;
|
| 477 |
+
}
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
// Jika masih ada gambar yang belum diproses
|
| 481 |
+
if (partImages.length > 0) {
|
| 482 |
+
await combineAndSaveV2(partImages, partIndex, tempDir, instanceID);
|
| 483 |
+
}
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
async function combineAndSaveV2(imagePaths, partIndex, tempDir, instanceID) {
|
| 487 |
+
const combinedImagePath = path.join(tempDir, `combined_part_${instanceID}_${partIndex}.jpg`);
|
| 488 |
+
const command = `convert ${imagePaths.join(' ')} -append -quality 75 ${combinedImagePath}`;
|
| 489 |
+
await execPromise(command);
|
| 490 |
+
|
| 491 |
+
imagePaths.forEach(fs.unlinkSync);
|
| 492 |
+
|
| 493 |
+
return combinedImagePath;
|
| 494 |
+
}
|
| 495 |
+
|
| 496 |
+
async function createPDFV2(instanceID, tempDir) {
|
| 497 |
+
const combinedParts = fs.readdirSync(tempDir).filter(file => file.startsWith(`combined_part_${instanceID}_`));
|
| 498 |
+
const combinedImagesPath = combinedParts.map(file => path.join(tempDir, file)).join(' ');
|
| 499 |
+
|
| 500 |
+
const pdfPath = path.join(tempDir, `${instanceID}.pdf`);
|
| 501 |
+
const createPDFCommand = `convert ${combinedImagesPath} ${pdfPath}`;
|
| 502 |
+
await execPromise(createPDFCommand);
|
| 503 |
+
|
| 504 |
+
return pdfPath;
|
| 505 |
+
}
|
| 506 |
|
| 507 |
+
|
| 508 |
+
app.get('/komiku/downloadV2', async (req, res) => {
|
| 509 |
+
try {
|
| 510 |
+
const { url } = req.query;
|
| 511 |
+
if (!url) return res.status(400).json({ error: 'Parameter url is required' });
|
| 512 |
+
let result = await komiku_downloadV2(url);
|
| 513 |
+
res.json(result);
|
| 514 |
+
|
| 515 |
+
// Menghapus file setelah 10 menit
|
| 516 |
+
try {
|
| 517 |
+
await new Promise(resolve => setTimeout(resolve, 10 * 60 * 1000)); // 10 minutes
|
| 518 |
+
await fss.unlink(result.path);
|
| 519 |
+
console.log(`File ${result.path} deleted.`);
|
| 520 |
+
} catch (error) {
|
| 521 |
+
console.error(`Error deleting file ${result.path}:`, error);
|
| 522 |
+
}
|
| 523 |
+
} catch (error) {
|
| 524 |
+
console.error('Error processing request:', error);
|
| 525 |
+
res.status(500).json({
|
| 526 |
+
error: 'Failed to process request\n' + error
|
| 527 |
+
});
|
| 528 |
+
}
|
| 529 |
+
});
|
| 530 |
/***********/
|
| 531 |
|
| 532 |
async function getLatestKomik(page) {
|