Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -506,31 +506,26 @@ app.get('/download', async (req, res) => {
|
|
506 |
|
507 |
async function nhentai(url) {
|
508 |
const instanceID = generateRandomID();
|
509 |
-
const tempDir =
|
510 |
-
|
511 |
-
await fss.mkdir(tempDir); // Membuat direktori sementara untuk menyimpan file sementara
|
512 |
-
const title = url.split('g/').filter(part => part).pop(); // Mendapatkan ID galeri dari URL
|
513 |
|
514 |
try {
|
515 |
const response = await axios.get(url, {
|
516 |
headers: {
|
517 |
-
'User-Agent':
|
518 |
-
'Referer': 'https://nhentai.net/'
|
519 |
-
'X-Forwarded-For': generateRandomIP()
|
520 |
}
|
521 |
});
|
522 |
|
523 |
const $ = cheerio.load(response.data);
|
524 |
-
const scriptContent = $('script')
|
525 |
-
$(el).html().includes('window._gallery = JSON.parse')
|
526 |
-
|
527 |
|
528 |
const jsonString = scriptContent.match(/JSON\.parse\("(.*)"\)/)[1];
|
529 |
const decodedString = jsonString.replace(/\\u0022/g, '"').replace(/\\u005C/g, '\\');
|
530 |
const jsonData = JSON.parse(decodedString);
|
531 |
|
532 |
-
console.log(jsonData);
|
533 |
-
|
534 |
const imgList = jsonData.images.pages.map((_, index) => ({
|
535 |
path: `https://i5.nhentai.net/galleries/${jsonData.media_id}/${index + 1}.${jsonData.images.pages[index].t === "w" ? "webp" : jsonData.images.pages[index].t === "p" ? "png" : "jpg"}`
|
536 |
}));
|
@@ -538,31 +533,21 @@ async function nhentai(url) {
|
|
538 |
const imagePaths = [];
|
539 |
for (const img of imgList) {
|
540 |
const downloadedImagePath = await downloadImageNh(img, tempDir, instanceID);
|
541 |
-
|
542 |
-
|
543 |
-
if (isWebp) {
|
544 |
-
const convertedImagePath = downloadedImagePath.replace('.webp', '.jpg');
|
545 |
-
await convertImageV2(downloadedImagePath, convertedImagePath);
|
546 |
-
fs.unlinkSync(downloadedImagePath); // PERUBAHAN: Hapus file asli .webp
|
547 |
-
imagePaths.push(convertedImagePath);
|
548 |
-
} else {
|
549 |
-
imagePaths.push(downloadedImagePath);
|
550 |
-
}
|
551 |
}
|
552 |
|
553 |
const pdfPath = await createPDF(imagePaths, instanceID, tempDir);
|
554 |
-
|
555 |
-
console.log(`PDF berhasil dibuat: ${pdfPath}`);
|
556 |
return {
|
557 |
url: `https://arashicode-komik.hf.space/static/${instanceID}.pdf`,
|
558 |
path: pdfPath,
|
559 |
result: jsonData
|
560 |
};
|
561 |
} catch (error) {
|
562 |
-
console.
|
563 |
-
throw error;
|
564 |
} finally {
|
565 |
-
await
|
566 |
}
|
567 |
}
|
568 |
|
@@ -580,6 +565,17 @@ async function convertImageV2(inputPath, outputPath) {
|
|
580 |
}
|
581 |
|
582 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
583 |
async function downloadImageNh(image, tempDir, instanceID) {
|
584 |
const servers = [
|
585 |
"https://i1.nhentai.net",
|
@@ -590,29 +586,36 @@ async function downloadImageNh(image, tempDir, instanceID) {
|
|
590 |
"https://i7.nhentai.net"
|
591 |
];
|
592 |
|
593 |
-
let lastError = null; // Menyimpan error terakhir
|
594 |
-
|
595 |
for (const server of servers) {
|
596 |
try {
|
597 |
-
const
|
|
|
|
|
|
|
598 |
responseType: 'arraybuffer',
|
599 |
headers: {
|
600 |
-
'User-Agent': 'Mozilla/5.0
|
601 |
'Referer': 'https://nhentai.net/'
|
602 |
}
|
603 |
});
|
604 |
|
605 |
-
|
606 |
-
await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
607 |
|
608 |
-
return imagePath;
|
609 |
} catch (error) {
|
610 |
-
console.
|
611 |
-
lastError = error; // Menyimpan error jika terjadi
|
612 |
}
|
613 |
}
|
614 |
|
615 |
-
throw new Error(
|
616 |
}
|
617 |
|
618 |
|
|
|
506 |
|
507 |
async function nhentai(url) {
|
508 |
const instanceID = generateRandomID();
|
509 |
+
const tempDir = path.join(os.tmpdir(), instanceID);
|
510 |
+
await fs.promises.mkdir(tempDir, { recursive: true });
|
|
|
|
|
511 |
|
512 |
try {
|
513 |
const response = await axios.get(url, {
|
514 |
headers: {
|
515 |
+
'User-Agent': 'Mozilla/5.0',
|
516 |
+
'Referer': 'https://nhentai.net/'
|
|
|
517 |
}
|
518 |
});
|
519 |
|
520 |
const $ = cheerio.load(response.data);
|
521 |
+
const scriptContent = $('script')
|
522 |
+
.filter((i, el) => $(el).html().includes('window._gallery = JSON.parse'))
|
523 |
+
.html();
|
524 |
|
525 |
const jsonString = scriptContent.match(/JSON\.parse\("(.*)"\)/)[1];
|
526 |
const decodedString = jsonString.replace(/\\u0022/g, '"').replace(/\\u005C/g, '\\');
|
527 |
const jsonData = JSON.parse(decodedString);
|
528 |
|
|
|
|
|
529 |
const imgList = jsonData.images.pages.map((_, index) => ({
|
530 |
path: `https://i5.nhentai.net/galleries/${jsonData.media_id}/${index + 1}.${jsonData.images.pages[index].t === "w" ? "webp" : jsonData.images.pages[index].t === "p" ? "png" : "jpg"}`
|
531 |
}));
|
|
|
533 |
const imagePaths = [];
|
534 |
for (const img of imgList) {
|
535 |
const downloadedImagePath = await downloadImageNh(img, tempDir, instanceID);
|
536 |
+
imagePaths.push(downloadedImagePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
537 |
}
|
538 |
|
539 |
const pdfPath = await createPDF(imagePaths, instanceID, tempDir);
|
540 |
+
console.log(`PDF successfully created: ${pdfPath}`);
|
|
|
541 |
return {
|
542 |
url: `https://arashicode-komik.hf.space/static/${instanceID}.pdf`,
|
543 |
path: pdfPath,
|
544 |
result: jsonData
|
545 |
};
|
546 |
} catch (error) {
|
547 |
+
console.error(error);
|
548 |
+
throw error;
|
549 |
} finally {
|
550 |
+
await fs.promises.rm(tempDir, { recursive: true });
|
551 |
}
|
552 |
}
|
553 |
|
|
|
565 |
}
|
566 |
|
567 |
|
568 |
+
async function convertImageToJPG(inputPath, outputPath) {
|
569 |
+
try {
|
570 |
+
await execAsync(`ffmpeg -i "${inputPath}" -q:v 1 "${outputPath}"`);
|
571 |
+
console.log(`Converted ${inputPath} to ${outputPath}`);
|
572 |
+
return outputPath;
|
573 |
+
} catch (error) {
|
574 |
+
console.error(`Error converting image: ${error.message}`);
|
575 |
+
throw new Error(`Failed to convert image ${inputPath} to JPG.`);
|
576 |
+
}
|
577 |
+
}
|
578 |
+
|
579 |
async function downloadImageNh(image, tempDir, instanceID) {
|
580 |
const servers = [
|
581 |
"https://i1.nhentai.net",
|
|
|
586 |
"https://i7.nhentai.net"
|
587 |
];
|
588 |
|
|
|
|
|
589 |
for (const server of servers) {
|
590 |
try {
|
591 |
+
const imageUrl = `${server}/galleries/${image.path.split('galleries/')[1]}`;
|
592 |
+
console.log(`Downloading image from: ${imageUrl}`);
|
593 |
+
|
594 |
+
const response = await axios.get(imageUrl, {
|
595 |
responseType: 'arraybuffer',
|
596 |
headers: {
|
597 |
+
'User-Agent': 'Mozilla/5.0',
|
598 |
'Referer': 'https://nhentai.net/'
|
599 |
}
|
600 |
});
|
601 |
|
602 |
+
let imagePath = `${tempDir}/image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.webp`;
|
603 |
+
await fs.promises.writeFile(imagePath, response.data);
|
604 |
+
|
605 |
+
const isWebp = path.extname(imagePath).toLowerCase() === '.webp';
|
606 |
+
if (isWebp) {
|
607 |
+
const convertedPath = imagePath.replace('.webp', '.jpg');
|
608 |
+
imagePath = await convertImageToJPG(imagePath, convertedPath);
|
609 |
+
fs.unlinkSync(imagePath.replace('.jpg', '.webp')); // Hapus file asli `webp`
|
610 |
+
}
|
611 |
|
612 |
+
return imagePath;
|
613 |
} catch (error) {
|
614 |
+
console.error(`Error downloading image from ${server}:`, error.message);
|
|
|
615 |
}
|
616 |
}
|
617 |
|
618 |
+
throw new Error('Failed to download image from all servers.');
|
619 |
}
|
620 |
|
621 |
|