Nexchan commited on
Commit
7dc9cea
1 Parent(s): 94dbddc

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +72 -0
index.js CHANGED
@@ -173,6 +173,78 @@ app.get('/download', async (req, res) => {
173
  }
174
  });
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  // Fungsi untuk ping website
177
  async function pingWebsite() {
178
  const browser = await puppeteer.launch({
 
173
  }
174
  });
175
 
176
+ async function nhentai(url) {
177
+ const instanceID = generateRandomID();
178
+ const tempDir = `./${instanceID}`;
179
+ await fss.mkdir(tempDir);
180
+ const title = url.split('g/').filter(part => part).pop();
181
+ try {
182
+ const response = await axios.get(url)
183
+ const $ = cheerio.load(response.data);
184
+ const scriptContent = $('script').filter((i, el) => {
185
+ return $(el).html().includes('window._gallery = JSON.parse');
186
+ }).html();
187
+
188
+ const jsonString = scriptContent.match(/JSON\.parse\("(.*)"\)/)[1];
189
+ const decodedString = jsonString.replace(/\\u0022/g, '"').replace(/\\u005C/g, '\\');
190
+ const jsonData = JSON.parse(decodedString);
191
+ //console.log(jsonData)
192
+
193
+ const imgList = [];
194
+ for (let i = 0; i < jsonData.images.pages.length; i++) {
195
+ imgList.push({ path: `https://external-content.duckduckgo.com/iu/?u=https://i5.nhentai.net/galleries/${jsonData.media_id}/${i + 1}.jpg&f=1&nofb=1` });
196
+ }
197
+
198
+ const imagePaths = await downloadImageNhs(imgList, tempDir, instanceID);
199
+ const pdfPath = await createPDF(imagePaths, instanceID, tempDir);
200
+
201
+ console.log(`PDF berhasil dibuat: ${pdfPath}`);
202
+ return { path: pdfPath, result: jsonData };
203
+ } catch (error) {
204
+ console.log(error);
205
+ throw error;
206
+ } finally {
207
+ await fss.rmdir(tempDir, { recursive: true });
208
+ }
209
+ }
210
+
211
+ async function downloadImageNh(image, tempDir, instanceID) {
212
+ const response = await axios.get(image.path, {
213
+ responseType: 'arraybuffer',
214
+ headers: {
215
+ '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;]',
216
+ 'Referer': 'https://nhentai.net/'
217
+ }
218
+ });
219
+
220
+ const imagePath = `${tempDir}/image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`;
221
+ await writeFileAsync(imagePath, response.data);
222
+
223
+ return imagePath;
224
+ }
225
+
226
+ async function downloadImageNhs(imgList, tempDir, instanceID) {
227
+ const imagePaths = [];
228
+ for (const img of imgList) {
229
+ const imagePath = await downloadImageNh(img, tempDir, instanceID);
230
+ imagePaths.push(imagePath);
231
+ }
232
+ return imagePaths;
233
+ }
234
+
235
+ app.get('/nhentai', async (req, res) => {
236
+ const { url } = req.query;
237
+ if (!url) {
238
+ return res.status(400).send('URL is required');
239
+ }
240
+ try {
241
+ const result = await nhentai(url);
242
+ res.json(result);
243
+ } catch (error) {
244
+ res.status(500).send('Error processing request');
245
+ }
246
+ });
247
+
248
  // Fungsi untuk ping website
249
  async function pingWebsite() {
250
  const browser = await puppeteer.launch({