-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
76 lines (63 loc) · 1.88 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Configuration, PuppeteerCrawler } from "crawlee";
import aws_chromium from "@sparticuz/chromium";
export const handler = async (event, context) => {
console.log("event", event);
console.log("context", context);
const { queryStringParameters } = event;
let urls = [];
let result = null;
if (!queryStringParameters) {
return {
isBase64Encoded: false,
statusCode: 400,
body: JSON.stringify({
error: "Url is required in the query parameter!",
}),
};
}
// # add url
urls.push(queryStringParameters["url"]);
// TODO: add rate limit to api request
const crawler = new PuppeteerCrawler(
{
requestHandler: async ({ page, request }) => {
console.log("request", request);
// # turns request interceptor on
await page.setRequestInterception(true);
// # if the page makes a request to a resource type of image or stylesheet then abort that request
page.on("request", (request) => {
if (request.resourceType() === "image") request.abort();
else request.continue();
});
// # take screenshot
const screenshot = await page.screenshot({
fullPage: true,
captureBeyondViewport: false,
});
// # convert into data:image base64
const b64Data = screenshot.toString('base64');
const dataURI = `data:image/jpeg;base64,${b64Data}`;
// # update result
result = dataURI;
},
launchContext: {
launchOptions: {
executablePath: await aws_chromium.executablePath(),
args: aws_chromium.args,
headless: true,
},
},
},
new Configuration({
persistStorage: false,
})
);
await crawler.run(urls);
return {
isBase64Encoded: false,
statusCode: 200,
body: JSON.stringify({
data: result,
}),
};
};