-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update examples & test with pupperteer
- Loading branch information
Showing
10 changed files
with
111 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,6 @@ | |
}, | ||
"dependencies": { | ||
"@extractus/article-extractor": "latest", | ||
"express": "^4.18.2" | ||
"express": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# node-article-parser with Pupperteer | ||
|
||
Install dependencies: | ||
|
||
```bash | ||
npm i | ||
|
||
# or pnpm, yarn | ||
``` | ||
|
||
Start server: | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
Open `http://localhost:3100/?url=https://www.techradar.com/televisions/samsungs-new-cheaper-oled-tvs-are-now-available-to-buy` to see the result. | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import puppeteer from 'puppeteer' | ||
import express from 'express' | ||
import { extractFromHtml } from '@extractus/article-extractor' | ||
|
||
const app = express() | ||
|
||
const meta = { | ||
service: 'article-parser-pupperteer', | ||
lang: 'javascript', | ||
server: 'express', | ||
platform: 'node', | ||
} | ||
|
||
const loadHtml = async (url) => { | ||
let browser = null | ||
try { | ||
console.log('Initialize puppeteer engine') | ||
browser = await puppeteer.launch() | ||
const page = await browser.newPage() | ||
await page.setDefaultNavigationTimeout(6e4) | ||
console.log(`Start rendering target page "${url}"`) | ||
await page.goto(url, { | ||
waitUntil: 'networkidle0', | ||
}) | ||
console.log(`Load html content from target page ${url}`) | ||
const html = await page.content() | ||
return html | ||
} catch (err) { | ||
console.error(err) | ||
return null | ||
} finally { | ||
if (browser) { | ||
await browser.close() | ||
} | ||
} | ||
} | ||
|
||
app.get('/', async (req, res) => { | ||
const url = req.query.url | ||
if (!url) { | ||
return res.json(meta) | ||
} | ||
try { | ||
const html = await loadHtml(url) | ||
const data = await extractFromHtml(html, url) | ||
return res.json({ | ||
error: 0, | ||
message: 'article has been extracted successfully', | ||
data, | ||
meta, | ||
}) | ||
} catch (err) { | ||
return res.json({ | ||
error: 1, | ||
message: err.message, | ||
data: null, | ||
meta, | ||
}) | ||
} | ||
}) | ||
|
||
app.listen(3100, () => { | ||
console.log('Server is running at http://localhost:3100') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "node-pupperteer", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@extractus/article-extractor": "latest", | ||
"express": "latest", | ||
"puppeteer": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "8.0.8", | ||
"version": "8.0.9", | ||
"name": "@extractus/article-extractor", | ||
"description": "To extract main article from given URL", | ||
"homepage": "https://github.com/extractus/article-extractor", | ||
|
@@ -11,15 +11,16 @@ | |
"main": "./src/main.js", | ||
"type": "module", | ||
"imports": { | ||
"cross-fetch": "./src/deno/cross-fetch.js" | ||
"cross-fetch": "./src/deno/cross-fetch.js", | ||
"linkedom": "https://deno.land/x/[email protected]/deno-dom-wasm.ts" | ||
}, | ||
"browser": { | ||
"cross-fetch": "./src/deno/cross-fetch.js", | ||
"linkedom": "./src/browser/linkedom.js" | ||
}, | ||
"types": "./index.d.ts", | ||
"engines": { | ||
"node": ">= 16" | ||
"node": ">= 18" | ||
}, | ||
"scripts": { | ||
"lint": "eslint .", | ||
|
@@ -38,8 +39,8 @@ | |
}, | ||
"devDependencies": { | ||
"@types/sanitize-html": "^2.11.0", | ||
"eslint": "^9.1.1", | ||
"globals": "^15.0.0", | ||
"eslint": "^9.2.0", | ||
"globals": "^15.1.0", | ||
"https-proxy-agent": "^7.0.4", | ||
"jest": "^29.7.0", | ||
"nock": "^13.5.4" | ||
|