Nexchan commited on
Commit
621aed3
·
verified ·
1 Parent(s): b7926d7

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +67 -45
index.js CHANGED
@@ -251,67 +251,89 @@ app.get('/nhentai', async (req, res) => {
251
  }
252
  });
253
 
254
- async function r34(url) {
255
- const browser = await puppeteer.launch({
256
- headless: true, // Non-headless untuk debugging
257
- args: ['--no-sandbox', '--disable-setuid-sandbox']
258
- });
259
-
260
- const page = await browser.newPage();
261
-
262
- // Setel header User-Agent dan Referer
263
- await page.setExtraHTTPHeaders({
264
- 'User-Agent': getRandomUserAgent(),
265
- 'Referer': url,
266
- 'X-Forwarded-For': generateRandomIP()
267
- });
268
-
269
- /*
270
-
271
- // Setel direktori unduhan unik untuk setiap permintaan
272
- const downloadPath = fs.mkdtempSync(path.join(os.tmpdir(), 'r34')); // Buat subdirektori unik
273
-
274
- await page._client.send('Page.setDownloadBehavior', {
275
- behavior: 'allow',
276
- downloadPath: downloadPath
277
- });
278
- */
279
 
280
- await page.goto(url); // Buka URL yang diberikan
 
 
 
 
 
 
281
 
282
- // Asumsikan ada tombol unduh dengan selector '#downloadButton'
283
- await page.click("#tab_video_info > div:nth-child(4) > div > a:nth-child(5)");
 
 
 
284
 
285
- /*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
- // Tunggu sampai file diunduh dengan memonitor perubahan pada direktori
288
- let filename = '';
289
- while (filename === '') {
290
- const files = fs.readdirSync(downloadPath);
291
- if (files.length > 0) {
292
- filename = files[0]; // Ambil nama file pertama yang muncul
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  } else {
294
- await new Promise(resolve => setTimeout(resolve, 100)); // Tunggu sebentar sebelum cek lagi
295
  }
296
- }
297
- */
298
-
299
- const cookies = await page.cookies();
300
 
301
- await browser.close();
302
 
303
- return { cookies } //{ path: path.join(downloadPath, filename), url: url };
 
 
 
 
 
304
  }
305
 
306
-
307
-
308
  app.get('/r34', async (req, res) => {
309
  const { url } = req.query;
310
  if (!url) {
311
  return res.status(400).send('URL is required');
312
  }
313
  try {
314
- const result = await r34(url);
315
  res.json(result);
316
  } catch (error) {
317
  res.status(500).send('Error processing request');
 
251
  }
252
  });
253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
+ async function downloadVideoR34(url) {
256
+ const browser = await puppeteer.launch({
257
+ headless: true,
258
+ args: ['--no-sandbox', '--disable-setuid-sandbox']
259
+ });
260
+ const page = await browser.newPage();
261
+ const downloadPath = path.resolve(os.tmpdir()); // Set your download directory here
262
 
263
+ await page.setExtraHTTPHeaders({
264
+ 'User-Agent': getRandomUserAgent(),
265
+ 'Referer': url,
266
+ 'X-Forwarded-For': generateRandomIP()
267
+ });
268
 
269
+ await page.goto(url, { waitUntil: 'networkidle0' });
270
+
271
+ // Get download link and other information
272
+ const result = await page.evaluate(() => {
273
+ const infoElements = document.querySelectorAll("#tab_video_info > div");
274
+ const links = Array.from(document.querySelectorAll('a')).map(a => ({
275
+ href: a.href,
276
+ text: a.textContent.trim()
277
+ }));
278
+
279
+ if (infoElements.length > 0) {
280
+ const download = infoElements[infoElements.length - 1];
281
+ if (download) {
282
+ const wrapDivsDownload = download.querySelectorAll("div.wrap > a");
283
+ return { downloadLink: wrapDivsDownload[0].href, links };
284
+ }
285
+ }
286
+ return { downloadLink: null, links };
287
+ });
288
 
289
+ const { downloadLink, links } = result;
290
+
291
+ let filePath = null;
292
+ let filename = null;
293
+ if (downloadLink) {
294
+ const response = await axios({
295
+ method: 'GET',
296
+ url: downloadLink,
297
+ responseType: 'stream'
298
+ });
299
+
300
+ // Get filename from content-disposition header or generate from URL
301
+ const contentDisposition = response.headers['content-disposition'];
302
+ filename = contentDisposition ? contentDisposition.split('filename=')[1] : null;
303
+ if (!filename) {
304
+ filename = url.match(/video\/(\d+)\/([a-zA-Z0-9-]+)\//)[2] + '.mp4'; // Adjust as needed
305
+ }
306
+
307
+ filePath = path.join(downloadPath, filename);
308
+ const writer = fs.createWriteStream(filePath);
309
+
310
+ response.data.pipe(writer);
311
+
312
+ await new Promise((resolve, reject) => {
313
+ writer.on('finish', resolve);
314
+ writer.on('error', reject);
315
+ });
316
  } else {
317
+ console.error('Download link not found.');
318
  }
 
 
 
 
319
 
320
+ await browser.close();
321
 
322
+ return {
323
+ download: "https://arashicode-komik.hf.space/static/" + filename,
324
+ path: filePath,
325
+ url: downloadLink,
326
+ links
327
+ };
328
  }
329
 
 
 
330
  app.get('/r34', async (req, res) => {
331
  const { url } = req.query;
332
  if (!url) {
333
  return res.status(400).send('URL is required');
334
  }
335
  try {
336
+ const result = await downloadVideoR34(url);
337
  res.json(result);
338
  } catch (error) {
339
  res.status(500).send('Error processing request');