@graphai/puppeteer_agent 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/puppeteer_agent.js +59 -17
- package/package.json +4 -4
package/lib/puppeteer_agent.js
CHANGED
|
@@ -7,28 +7,70 @@ exports.puppeteerAgent = void 0;
|
|
|
7
7
|
const puppeteer_1 = __importDefault(require("puppeteer"));
|
|
8
8
|
const readability_1 = require("@mozilla/readability");
|
|
9
9
|
const jsdom_1 = require("jsdom");
|
|
10
|
-
const
|
|
11
|
-
|
|
10
|
+
const NAV_TIMEOUT = 45_000;
|
|
11
|
+
const userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36";
|
|
12
|
+
const normalize = (s) => s
|
|
13
|
+
.replace(/\r\n/g, "\n")
|
|
14
|
+
.replace(/[\n\t]{2,}/g, "\n")
|
|
15
|
+
.trim();
|
|
16
|
+
/* global document */
|
|
17
|
+
async function waitStable(page, ms = 1200, step = 200) {
|
|
18
|
+
let last = -1;
|
|
19
|
+
let stable = 0;
|
|
20
|
+
while (stable < ms) {
|
|
21
|
+
const len = await page.evaluate(() => document.body?.innerText?.length || 0);
|
|
22
|
+
stable = len === last ? stable + step : 0;
|
|
23
|
+
last = len;
|
|
24
|
+
await new Promise((r) => setTimeout(r, step));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function fetchArticle(url) {
|
|
28
|
+
const browser = await puppeteer_1.default.launch({
|
|
29
|
+
headless: true,
|
|
30
|
+
args: ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage"],
|
|
31
|
+
});
|
|
32
|
+
const page = await browser.newPage();
|
|
33
|
+
await page.setUserAgent(userAgent);
|
|
34
|
+
await page.setViewport({ width: 1366, height: 900 });
|
|
12
35
|
try {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
await page.goto(url, { waitUntil: "networkidle2" });
|
|
17
|
-
// console.log(2);
|
|
36
|
+
await page.goto(url, { waitUntil: "networkidle2", timeout: NAV_TIMEOUT });
|
|
37
|
+
await Promise.race([page.waitForSelector("article, main, [role=main], .article, .post", { timeout: 8000 }), new Promise((r) => setTimeout(r, 8000))]);
|
|
38
|
+
await waitStable(page, 1200);
|
|
18
39
|
const html = await page.content();
|
|
19
40
|
const dom = new jsdom_1.JSDOM(html, { url: page.url() });
|
|
20
41
|
const reader = new readability_1.Readability(dom.window.document);
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
42
|
+
const a = reader.parse();
|
|
43
|
+
const title = a?.title || (await page.title()) || null;
|
|
44
|
+
const text = normalize(a?.textContent || "");
|
|
45
|
+
let finalText = text;
|
|
46
|
+
if (finalText.length < 100) {
|
|
47
|
+
const raw = await page.evaluate(() => {
|
|
48
|
+
const el = document.querySelector("article, main, [role=main], .article, .post") || document.body;
|
|
49
|
+
return el?.textContent || "";
|
|
50
|
+
});
|
|
51
|
+
finalText = normalize(raw);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
url,
|
|
55
|
+
title,
|
|
56
|
+
byline: a?.byline || null,
|
|
57
|
+
excerpt: a?.excerpt || null,
|
|
58
|
+
length: a?.length ?? (finalText?.length || null),
|
|
59
|
+
textContent: finalText || null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
await page.close().catch(() => { });
|
|
64
|
+
await browser.close().catch(() => { });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const puppeteerAgent = async ({ namedInputs, params }) => {
|
|
68
|
+
const { url } = namedInputs;
|
|
69
|
+
try {
|
|
70
|
+
const data = await fetchArticle(url);
|
|
24
71
|
return {
|
|
25
|
-
title:
|
|
26
|
-
content: (
|
|
27
|
-
"\n" +
|
|
28
|
-
(article?.textContent ?? "")
|
|
29
|
-
.replace(/\r\n/g, "\n")
|
|
30
|
-
.replace(/(\n[ \t]*){2,}/g, "\n")
|
|
31
|
-
.trim(),
|
|
72
|
+
title: data?.title,
|
|
73
|
+
content: (data?.title ?? "") + "\n" + (data?.textContent ?? ""),
|
|
32
74
|
};
|
|
33
75
|
}
|
|
34
76
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphai/puppeteer_agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/receptron/graphai-agents#readme",
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@types/jsdom": "^
|
|
29
|
+
"@types/jsdom": "^28.0.3"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@mozilla/readability": "^0.6.0",
|
|
33
|
-
"jsdom": "^
|
|
34
|
-
"puppeteer": "^
|
|
33
|
+
"jsdom": "^29.1.1",
|
|
34
|
+
"puppeteer": "^25.2.1"
|
|
35
35
|
},
|
|
36
36
|
"types": "./lib/index.d.ts",
|
|
37
37
|
"directories": {
|