Nexchan commited on
Commit
8797cca
·
verified ·
1 Parent(s): 571f521

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +105 -0
index.js CHANGED
@@ -76,6 +76,111 @@ function generateRandomID(length = 8) {
76
  }
77
  return result;
78
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  async function komiku_download(url) {
81
  const instanceID = generateRandomID();
 
76
  }
77
  return result;
78
  }
79
+ /**************************************/
80
+
81
+ async function komiku_downloadV2(url) {
82
+ const instanceID = generateRandomID();
83
+ const tempDir = path.join(os.tmpdir(), instanceID);
84
+ await fss.mkdir(tempDir);
85
+
86
+ const title = url.split('/').filter(part => part).pop();
87
+
88
+ const browser = await puppeteer.launch({
89
+ headless: true,
90
+ args: ['--no-sandbox', '--disable-setuid-sandbox']
91
+ });
92
+ try {
93
+ const page = await browser.newPage();
94
+ await page.setUserAgent(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;]');
95
+ await page.setExtraHTTPHeaders({
96
+ 'Referer': 'https://komiku.id/devious-son-of-heaven-chapter-04/',
97
+ 'X-Forwarded-For': generateRandomIP()
98
+ });
99
+
100
+ await page.goto(url, { waitUntil: 'networkidle2' });
101
+ const imgList = await page.evaluate(() => {
102
+ return Array.from(document.querySelectorAll('#Baca_Komik img')).map(img => ({ path: img.src }));
103
+ });
104
+
105
+ const imagePaths = await downloadImageV2s(imgList, tempDir, instanceID, page);
106
+ const pdfPath = await createPDFV2(imagePaths, instanceID, tempDir);
107
+
108
+ return { url: "https://arashicode-komik.hf.space/static/" + instanceID + ".pdf", path: `/static/${instanceID}.pdf`, title: title, path: pdfPath};
109
+ } catch (error) {
110
+ console.log(error);
111
+ throw error;
112
+ } finally {
113
+ await browser.close();
114
+ await fss.rmdir(tempDir, { recursive: true });
115
+ }
116
+ }
117
+
118
+ async function downloadImageV2(image, tempDir, instanceID, page) {
119
+ await page.setUserAgent(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;]');
120
+ await page.setExtraHTTPHeaders({
121
+ 'Referer': 'https://komiku.id/devious-son-of-heaven-chapter-04/',
122
+ 'X-Forwarded-For': generateRandomIP()
123
+ });
124
+
125
+ const viewSource = await page.goto(image.path);
126
+ const imagePath = path.join(tempDir, `image_${instanceID}_${Date.now()}_${Math.floor(Math.random() * 1000)}.jpg`);
127
+ await fs.promises.writeFile(imagePath, await viewSource.buffer());
128
+
129
+ return imagePath;
130
+ }
131
+
132
+ async function downloadImageV2s(imgList, tempDir, instanceID, page) {
133
+ const imagePaths = [];
134
+ for (const img of imgList) {
135
+ const imagePath = await downloadImageV2(img, tempDir, instanceID, page);
136
+ imagePaths.push(imagePath);
137
+ }
138
+ return imagePaths;
139
+ }
140
+
141
+ async function createPDFV2(imagePaths, instanceID, tempDir) {
142
+ const pdfPath = path.join(os.tmpdir(), `${instanceID}.pdf`);
143
+ const doc = new PDFDocument({ autoFirstPage: false });
144
+
145
+ doc.pipe(fs.createWriteStream(pdfPath));
146
+
147
+ for (const imagePath of imagePaths) {
148
+ const { width, height } = await getImageDimensions(imagePath);
149
+ doc.addPage({ size: [width, height] });
150
+ doc.image(imagePath, 0, 0, { width: width, height: height });
151
+ }
152
+
153
+ doc.end();
154
+
155
+ return pdfPath;
156
+ }
157
+
158
+
159
+ app.get('/downloadv2', async (req, res) => {
160
+ const { url } = req.query;
161
+ if (!url) {
162
+ return res.status(400).send('URL is required');
163
+ }
164
+
165
+ try {
166
+ const result = await komiku_downloadV2(url);
167
+ res.json(result);
168
+ setTimeout(() => {
169
+ fs.unlink(result.path, (err) => {
170
+ if (err) {
171
+ console.error(`Error deleting file: ${err.message}`);
172
+ } else {
173
+ console.log(`File deleted: ${result.path}`);
174
+ }
175
+ });
176
+ }, 280000);
177
+ } catch (error) {
178
+ res.status(500).send('Error processing request');
179
+ }
180
+ });
181
+
182
+
183
+ /**************************************/
184
 
185
  async function komiku_download(url) {
186
  const instanceID = generateRandomID();