Closure-RI commited on
Commit
326a1f0
·
verified ·
1 Parent(s): 5866d93

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +40 -37
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 = `./${instanceID}`;
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': getRandomUserAgent() || 'Mozilla/5.0 (Linux; Android 6.0.1; SM-N916S Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/389.0.0.42.111;]',
518
- 'Referer': 'https://nhentai.net/',
519
- 'X-Forwarded-For': generateRandomIP()
520
  }
521
  });
522
 
523
  const $ = cheerio.load(response.data);
524
- const scriptContent = $('script').filter((i, el) =>
525
- $(el).html().includes('window._gallery = JSON.parse')
526
- ).html();
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
- const isWebp = path.extname(downloadedImagePath).toLowerCase() === '.webp';
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.log(error);
563
- throw error; // Menghentikan eksekusi jika terjadi error
564
  } finally {
565
- await fss.rm(tempDir, { recursive: true }); // Menghapus direktori sementara
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 response = await axios.get(`https://external-content.duckduckgo.com/iu/?u=${server}/galleries/${image.path.split('galleries/')[1]}`, {
 
 
 
598
  responseType: 'arraybuffer',
599
  headers: {
600
- 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; SM-N916S Build/MMB29K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/106.0.5249.126 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/389.0.0.42.111;]',
601
  'Referer': 'https://nhentai.net/'
602
  }
603
  });
604
 
605
- const imagePath = `${tempDir}/image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`;
606
- await writeFileAsync(imagePath, response.data);
 
 
 
 
 
 
 
607
 
608
- return imagePath; // Mengembalikan path gambar yang berhasil diunduh
609
  } catch (error) {
610
- console.log(`Error with server ${server}:`, error.message);
611
- lastError = error; // Menyimpan error jika terjadi
612
  }
613
  }
614
 
615
- throw new Error(`Failed to download image after trying all servers. Last error: ${lastError.message}`);
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