Nexchan commited on
Commit
f75e2d0
·
verified ·
1 Parent(s): 1679f42

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +141 -0
index.js CHANGED
@@ -1,5 +1,15 @@
 
 
 
 
 
 
 
 
 
1
  const express = require('express');
2
  const axios = require('axios');
 
3
  const fs = require('fs');
4
  const { tmpdir } = require('os');
5
  const { join } = require('path');
@@ -8,6 +18,112 @@ const puppeteer = require('puppeteer');
8
  const app = express();
9
  const PORT = 7860;
10
  const { format } = require("util");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  app.get('/add', async (req, res) => {
13
  try {
@@ -420,6 +536,31 @@ app.get('/r34/v2', async (req, res) => {
420
  }
421
  });
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
  app.listen(PORT, () => {
424
  console.log(`Server berjalan di port ${PORT}`);
425
  });
 
1
+ import { createRequire } from 'module';
2
+ import { fileURLToPath } from 'url';
3
+ import path from 'path';
4
+ import PDFDocument from 'pdfkit';
5
+
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const require = createRequire(import.meta.url);
8
+
9
+ const { promisify } = require('util');
10
  const express = require('express');
11
  const axios = require('axios');
12
+ const cheerio = require('cheerio');
13
  const fs = require('fs');
14
  const { tmpdir } = require('os');
15
  const { join } = require('path');
 
18
  const app = express();
19
  const PORT = 7860;
20
  const { format } = require("util");
21
+ const os = require("os");
22
+ const writeFileAsync = promisify(fs.writeFile);
23
+ const fss = fs.promises;
24
+
25
+ function generateRandomID(length = 8) {
26
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
27
+ let result = '';
28
+ for (let i = 0; i < length; i++) {
29
+ result += characters.charAt(Math.floor(Math.random() * characters.length));
30
+ }
31
+ return result;
32
+ }
33
+
34
+ async function komiku_download(url) {
35
+ const instanceID = generateRandomID();
36
+ const tempDir = path.join(os.tmpdir(), instanceID);
37
+ await fss.mkdir(tempDir);
38
+
39
+ // Extracting the title from the URL
40
+ const title = url.split('/').filter(part => part).pop();
41
+
42
+ try {
43
+ const response = await axios.get(url);
44
+ const html = response.data;
45
+ const $ = cheerio.load(html);
46
+ const imgList = [];
47
+
48
+ $('#Baca_Komik img').each((index, element) => {
49
+ const src = $(element).attr('src');
50
+ imgList.push({ path: src });
51
+ });
52
+
53
+ const imagePaths = await downloadImages(imgList, tempDir, instanceID);
54
+ const pdfPath = await createPDF(imagePaths, instanceID, tempDir);
55
+
56
+ console.log(`PDF berhasil dibuat: ${pdfPath}`);
57
+ return { path: pdfPath, title: title };
58
+ } catch (error) {
59
+ console.log(error);
60
+ throw error;
61
+ } finally {
62
+ await fss.rmdir(tempDir, { recursive: true });
63
+ }
64
+ }
65
+
66
+ async function downloadImage(image, tempDir, instanceID) {
67
+ const response = await axios.get(image.path, { responseType: 'arraybuffer' });
68
+ const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`);
69
+ await writeFileAsync(imagePath, response.data);
70
+
71
+ return imagePath;
72
+ }
73
+
74
+ async function downloadImages(imgList, tempDir, instanceID) {
75
+ const imagePaths = [];
76
+ for (const img of imgList) {
77
+ const imagePath = await downloadImage(img, tempDir, instanceID);
78
+ imagePaths.push(imagePath);
79
+ }
80
+ return imagePaths;
81
+ }
82
+
83
+ async function createPDF(imagePaths, instanceID, tempDir) {
84
+ const pdfPath = path.join(os.tmpdir(), `${instanceID}.pdf`);
85
+ const doc = new PDFDocument({ autoFirstPage: false });
86
+
87
+ doc.pipe(fs.createWriteStream(pdfPath));
88
+
89
+ for (const imagePath of imagePaths) {
90
+ const { width, height } = await getImageDimensions(imagePath);
91
+ doc.addPage({ size: [width, height] });
92
+ doc.image(imagePath, 0, 0, { width: width, height: height });
93
+ }
94
+
95
+ doc.end();
96
+
97
+ return pdfPath;
98
+ }
99
+
100
+ async function getImageDimensions(imagePath) {
101
+ const { promisify } = require('util');
102
+ const sizeOf = promisify(require('image-size'));
103
+ const dimensions = await sizeOf(imagePath);
104
+ return dimensions;
105
+ }
106
+
107
+ app.get('/komikudl', async (req, res) => {
108
+ const url = req.query.url;
109
+
110
+ if (!url) {
111
+ return res.status(400).send('URL is required');
112
+ }
113
+
114
+ try {
115
+ const data = await komiku_download(url);
116
+ res.download(data.path, `${data.title}.pdf`, (err) => {
117
+ if (err) {
118
+ console.error(err);
119
+ }
120
+
121
+ fs.unlinkSync(data.path); // Remove the file after sending it
122
+ });
123
+ } catch (error) {
124
+ res.status(500).send('An error occurred while generating the PDF');
125
+ }
126
+ });
127
 
128
  app.get('/add', async (req, res) => {
129
  try {
 
536
  }
537
  });
538
 
539
+
540
+ async function pingWebsite() {
541
+ const browser = await puppeteer.launch({
542
+ headless: true,
543
+ args: ['--no-sandbox', '--disable-setuid-sandbox']
544
+ });
545
+ const page = await browser.newPage();
546
+ await page.setUserAgent("Mozilla/5.0 (Linux; Android 10; SM-G965U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.141 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/420.0.0.32.61;]");
547
+ await page.goto('https://huggingface.co/spaces/Nexchan/yutub');
548
+ console.log("Ping");
549
+ await browser.close();
550
+ }
551
+
552
+ // Ping website setiap 5 jam
553
+ async function pingEvery5Hours() {
554
+ await pingWebsite();
555
+ setInterval(async () => {
556
+ await pingWebsite();
557
+ }, 5 * 60 * 60 * 1000); // 5 hours in milliseconds
558
+ }
559
+
560
+ // Mulai ping
561
+ pingEvery5Hours();
562
+
563
+
564
  app.listen(PORT, () => {
565
  console.log(`Server berjalan di port ${PORT}`);
566
  });