aseli commited on
Commit
a4fe837
1 Parent(s): 05339c4

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +76 -17
index.js CHANGED
@@ -2,24 +2,23 @@ const express = require('express');
2
  const puppeteer = require('puppeteer-extra');
3
  const StealthPlugin = require('puppeteer-extra-plugin-stealth');
4
  const AnonymizeUA = require('puppeteer-extra-plugin-anonymize-ua');
 
 
 
5
 
6
  puppeteer.use(StealthPlugin());
7
  puppeteer.use(AnonymizeUA());
8
 
9
  const app = express();
10
- const port = 7860;
11
 
12
  app.use(express.json());
13
  app.get('/', async (req, res) => {
14
  console.log('request to /')
15
- console.log('IP => ' + req.headers['x-real-ip'])
16
- console.log('User Agent => ' + req.headers['user-agent'])
17
  res.send('halo kontol')
18
  })
19
  app.post('/screenshot', async (req, res) => {
20
  console.log('request to /screenshot')
21
- console.log('IP => ' + req.headers['x-real-ip'])
22
- console.log('User Agent => ' + req.headers['user-agent'])
23
  const {
24
  ua,
25
  url,
@@ -43,8 +42,6 @@ app.post('/screenshot', async (req, res) => {
43
 
44
  app.get('/screenshot', async (req, res) => {
45
  console.log('request to /screenshot (get)')
46
- console.log('IP => ' + req.headers['x-real-ip'])
47
- console.log('User Agent => ' + req.headers['user-agent'])
48
  const {
49
  ua,
50
  url,
@@ -65,6 +62,69 @@ app.get('/screenshot', async (req, res) => {
65
  fullpage
66
  });
67
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  async function processScreenshot(req, res, {
70
  ua,
@@ -75,7 +135,7 @@ async function processScreenshot(req, res, {
75
  language,
76
  fullpage
77
  }) {
78
-
79
  console.log('process screenshot')
80
  const browser = await puppeteer.launch({
81
  headless: "new",
@@ -102,7 +162,7 @@ async function processScreenshot(req, res, {
102
  'Accept-Language': language || 'id-ID',
103
  'User-Agent': ua || 'Mozilla/5.0 (Linux; Android 13; Pixel Build/RPB1.210523.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36',
104
  });
105
- try {
106
  await page.goto(url, {
107
  waitUntil: 'networkidle2',
108
  });
@@ -152,18 +212,18 @@ async function processScreenshot(req, res, {
152
  });
153
  } else if (type === 'custom') {
154
  if (width && height > 4096) {
155
- await browser.close();
156
  return res.json({
157
  "status": 400,
158
  "message": "Width and height values should not exceed 4096 pixels."
159
  });
160
- }
161
- if (width && height < 90) {
162
  await browser.close();
 
 
163
  return res.json({
164
  "status": 400,
165
- "message": "Width and height values should not be less than 90 pixels."
166
  });
 
167
  }
168
  screenshotOptions = {
169
  fullPage: full,
@@ -186,11 +246,10 @@ async function processScreenshot(req, res, {
186
  'Content-Length': screenshot.length,
187
  });
188
  res.end(screenshot);
189
-
190
  await browser.close();
191
  } catch (e) {
192
- console.log(e)
193
- await browser.close();
194
  return res.json({
195
  status: 400,
196
  message: 'error when take a screenshot'
@@ -200,4 +259,4 @@ async function processScreenshot(req, res, {
200
 
201
  app.listen(port, () => {
202
  console.log(`Server is running on port ${port}`);
203
- });
 
2
  const puppeteer = require('puppeteer-extra');
3
  const StealthPlugin = require('puppeteer-extra-plugin-stealth');
4
  const AnonymizeUA = require('puppeteer-extra-plugin-anonymize-ua');
5
+ const QRCode = require('qrcode');
6
+ const Jimp = require('jimp');
7
+ const QrCodeReader = require('qrcode-reader');
8
 
9
  puppeteer.use(StealthPlugin());
10
  puppeteer.use(AnonymizeUA());
11
 
12
  const app = express();
13
+ const port = 5000;
14
 
15
  app.use(express.json());
16
  app.get('/', async (req, res) => {
17
  console.log('request to /')
 
 
18
  res.send('halo kontol')
19
  })
20
  app.post('/screenshot', async (req, res) => {
21
  console.log('request to /screenshot')
 
 
22
  const {
23
  ua,
24
  url,
 
42
 
43
  app.get('/screenshot', async (req, res) => {
44
  console.log('request to /screenshot (get)')
 
 
45
  const {
46
  ua,
47
  url,
 
62
  fullpage
63
  });
64
  });
65
+ app.get('/qrcode', async (req, res) => {
66
+ try {
67
+ const {
68
+ mode,
69
+ url
70
+ } = req.query;
71
+
72
+ if (!mode || !url) {
73
+ return res.status(400).send('what?');
74
+ }
75
+
76
+ if (mode === 'create') {
77
+ // Generate a QR code
78
+ QRCode.toDataURL(url, (err, src) => {
79
+ if (err) return res.send({
80
+ error: 'Error generating QR code'
81
+ });
82
+ res.type('png').send(Buffer.from(src.split(",")[1], "base64"));
83
+ });
84
+ } else if (['read', 'scan'].includes(mode)) {
85
+ // Fetch the image and read the QR code
86
+ const response = await fetch(url);
87
+ const buffer = await response.buffer();
88
+ const image = await Jimp.read(buffer);
89
+
90
+ // Preprocess the image to enhance QR code readability
91
+ image
92
+ .grayscale() // Convert to grayscale
93
+ .contrast(1) // Increase contrast
94
+ .normalize(); // Normalize the image
95
+
96
+ const qrCodeReader = new QrCodeReader();
97
+
98
+ qrCodeReader.callback = (err, value) => {
99
+ if (err) return res.send({
100
+ error: 'Error reading QR code'
101
+ });
102
+ res.json({
103
+ result: value.result
104
+ });
105
+ };
106
+
107
+ const bitmap = image.bitmap;
108
+ qrCodeReader.decode({
109
+ width: bitmap.width,
110
+ height: bitmap.height,
111
+ data: bitmap.data
112
+ });
113
+ } else {
114
+ QRCode.toDataURL(url, (err, src) => {
115
+ if (err) return res.send({
116
+ error: 'Error generating QR code'
117
+ });
118
+ res.type('png').send(Buffer.from(src.split(",")[1], "base64"));
119
+ });
120
+ }
121
+ } catch (error) {
122
+ console.error(error);
123
+ res.send({
124
+ error: 'Internal Server Error'
125
+ });
126
+ }
127
+ });
128
 
129
  async function processScreenshot(req, res, {
130
  ua,
 
135
  language,
136
  fullpage
137
  }) {
138
+ try {
139
  console.log('process screenshot')
140
  const browser = await puppeteer.launch({
141
  headless: "new",
 
162
  'Accept-Language': language || 'id-ID',
163
  'User-Agent': ua || 'Mozilla/5.0 (Linux; Android 13; Pixel Build/RPB1.210523.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36',
164
  });
165
+
166
  await page.goto(url, {
167
  waitUntil: 'networkidle2',
168
  });
 
212
  });
213
  } else if (type === 'custom') {
214
  if (width && height > 4096) {
 
215
  return res.json({
216
  "status": 400,
217
  "message": "Width and height values should not exceed 4096 pixels."
218
  });
 
 
219
  await browser.close();
220
+ }
221
+ if (width && height < 400) {
222
  return res.json({
223
  "status": 400,
224
+ "message": "Width and height values should not be less than 400 pixels."
225
  });
226
+ await browser.close();
227
  }
228
  screenshotOptions = {
229
  fullPage: full,
 
246
  'Content-Length': screenshot.length,
247
  });
248
  res.end(screenshot);
249
+ console.log('process done')
250
  await browser.close();
251
  } catch (e) {
252
+ log(e)
 
253
  return res.json({
254
  status: 400,
255
  message: 'error when take a screenshot'
 
259
 
260
  app.listen(port, () => {
261
  console.log(`Server is running on port ${port}`);
262
+ });