Skip to content

Commit

Permalink
feat: text detail
Browse files Browse the repository at this point in the history
  • Loading branch information
Soontao committed May 29, 2024
1 parent 7b01608 commit a77a2ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"dependencies": {
"express": "^4.19.2",
"puppeteer-core": "^22.9.0",
"turndown": "^7.1.3"
"turndown": "^7.1.3",
"undici": "^6.18.1"
}
}
20 changes: 18 additions & 2 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import console from "console";
import express from "express";
import process from "process";
import puppeteer from "puppeteer-core";
import { fetch } from "undici";
import { asyncExpressMiddleware, defaultUserAgent } from "./utils.mjs";

if (process.env.PW_REMOTE_URL === undefined) {
Expand All @@ -27,12 +28,14 @@ app.get(
});
const results = await page.$(".results");
const cards = await results.$$(".vrwrap");

const refLinks = await Promise.all(
cards.map((card) => {
return card.evaluate((node) => {
cards.map(async (card) => {
const item = await card.evaluate((node) => {
const linkEle = node.querySelector("h3 a");
if (!linkEle) return;
const link = linkEle.href;

// get text
const title = linkEle.innerText;
const description = node.querySelector(".space-txt")?.innerText;
Expand All @@ -44,6 +47,19 @@ app.get(
img,
};
});
if (!item?.link) return;
const res = await fetch(process.env.TF_URL + "/extract", {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": defaultUserAgent(),
},
body: JSON.stringify({ url: item.link }),
});
if (!res.ok) return item;
const data = await res.json();
if (!data.text) return item;
return { ...item, text: data.text };
}),
);
await browser.close();
Expand Down

0 comments on commit a77a2ef

Please sign in to comment.