Nexchan commited on
Commit
3ed92fd
·
verified ·
1 Parent(s): 0013d6e

Upload index.js

Browse files
Files changed (1) hide show
  1. index.js +148 -0
index.js CHANGED
@@ -279,6 +279,68 @@ async function downloadFromUrl(url, filePath) {
279
  });
280
  }
281
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  async function Rules34(url_r34) {
283
  try {
284
  const browser = await puppeteer.launch();
@@ -386,6 +448,92 @@ app.get('/r34', async (req, res) => {
386
  }
387
  });
388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389
  async function r34Cookie(url) {
390
  const browser = await puppeteer.launch({
391
  headless: true, // Non-headless untuk debugging
 
279
  });
280
  }
281
 
282
+ async function Rules34info(url_r34) {
283
+ try {
284
+ // Mengambil halaman HTML dengan Axios
285
+ const response = await axios.get(url_r34, {
286
+ headers: {
287
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
288
+ 'Referer': url_r34
289
+ }
290
+ });
291
+
292
+ const htmlContent = response.data;
293
+ const $ = cheerio.load(htmlContent);
294
+
295
+ const title = $("body > div > div.wrapper > div.main > div.container > div > div > div:nth-child(1) > div.heading > h1").text().trim() || url_r34.match(/\/video\/\d+\/(.*)\//)?.[1];
296
+
297
+ const infoElements = $("#tab_video_info > div");
298
+ const result = {
299
+ title: title,
300
+ artist: [],
301
+ uploader: [],
302
+ tags: [],
303
+ category: []
304
+ };
305
+
306
+ if (infoElements.length > 0) {
307
+ const tags = infoElements.eq(infoElements.length - 2);
308
+ const videoInfo = infoElements.eq(infoElements.length - 3);
309
+
310
+ if (tags.length > 0) {
311
+ tags.find("div.wrap > a").each((index, element) => {
312
+ result.tags.push({ title: $(element).text().trim(), url: $(element).attr('href') });
313
+ });
314
+ result.tags.pop(); // Menghapus elemen terakhir dari tags
315
+ }
316
+
317
+ if (videoInfo.length > 0) {
318
+ videoInfo.find("div.cols > div").each((index, col) => {
319
+ const categoryLinks = $(col).find("div.col:nth-child(1) > a");
320
+ categoryLinks.each((index, link) => {
321
+ result.category.push({ title: $(link).text().trim(), url: $(link).attr('href') });
322
+ });
323
+
324
+ const artistLinks = $(col).find("div.col:nth-child(2) > a");
325
+ artistLinks.each((index, link) => {
326
+ result.artist.push({ title: $(link).text().trim(), url: $(link).attr('href') });
327
+ });
328
+
329
+ const uploaderLinks = $(col).find("div.col:nth-child(3) > a");
330
+ uploaderLinks.each((index, link) => {
331
+ result.uploader.push({ title: $(link).text().trim(), url: $(link).attr('href') });
332
+ });
333
+ });
334
+ }
335
+ }
336
+
337
+ return result;
338
+ } catch (error) {
339
+ console.error('Error fetching data:', error);
340
+ throw error; // Rethrow error to handle it upstream
341
+ }
342
+ }
343
+
344
  async function Rules34(url_r34) {
345
  try {
346
  const browser = await puppeteer.launch();
 
448
  }
449
  });
450
 
451
+
452
+ app.get('/r34/download', async (req, res) => {
453
+ const { url, type = 'download' } = req.query;
454
+ if (!url) {
455
+ return res.status(400).send('URL is required');
456
+ }
457
+
458
+ try {
459
+ const result = await Rules34(url); // Mendapatkan jalur unduhan dari modul Rules34
460
+ const filePath = result.downloadPath;
461
+
462
+ // Cek apakah file benar-benar ada sebelum mengirim
463
+ if (!fs.existsSync(filePath)) {
464
+ return res.status(404).send('File not found');
465
+ }
466
+
467
+ const fileName = path.basename(filePath);
468
+ const mimeType = 'video/mp4';
469
+
470
+ res.setHeader('Content-Type', mimeType);
471
+
472
+ if (type === 'stream') {
473
+ const range = req.headers.range;
474
+
475
+ if (range) {
476
+ const stat = fs.statSync(filePath);
477
+ const fileSize = stat.size;
478
+
479
+ const parts = range.replace(/bytes=/, "").split("-");
480
+ const start = parseInt(parts[0], 10);
481
+ const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
482
+
483
+ const chunksize = (end - start) + 1;
484
+ const file = fs.createReadStream(filePath, { start, end });
485
+ const head = {
486
+ 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
487
+ 'Accept-Ranges': 'bytes',
488
+ 'Content-Length': chunksize,
489
+ 'Content-Type': mimeType,
490
+ };
491
+
492
+ res.writeHead(206, head);
493
+ file.pipe(res);
494
+ } else {
495
+ res.setHeader('Content-Length', fileSize);
496
+ res.sendFile(filePath); // Menggunakan res.sendFile untuk mengirim file
497
+ }
498
+ } else if (type === 'buffer') {
499
+ fs.readFile(filePath, (err, data) => {
500
+ if (err) {
501
+ console.error('Gagal membaca file:', err);
502
+ res.status(500).send('Gagal membaca file: \n' + err);
503
+ } else {
504
+ res.setHeader('Content-Length', data.length);
505
+ res.send(data);
506
+ }
507
+ });
508
+ } else { // Default to 'download'
509
+ res.download(filePath, fileName, (err) => {
510
+ if (err) {
511
+ console.error('Gagal mengirim file:', err);
512
+ res.status(500).send('Gagal mengirim file: \n' + err);
513
+ }
514
+ });
515
+ }
516
+ } catch (error) {
517
+ console.error('Error processing request:', error);
518
+ res.status(500).send('Error processing request');
519
+ }
520
+ });
521
+
522
+
523
+ app.get('/r34/info', async (req, res) => {
524
+ const { url } = req.query;
525
+ if (!url) {
526
+ return res.status(400).send('URL is required');
527
+ }
528
+ try {
529
+ const result = await Rules34info(url);
530
+ res.json(result);
531
+ } catch (error) {
532
+ res.status(500).send('Error processing request');
533
+ }
534
+ });
535
+
536
+
537
  async function r34Cookie(url) {
538
  const browser = await puppeteer.launch({
539
  headless: true, // Non-headless untuk debugging