generated from arvinxx/vercel-serverless-api-template
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { Readability } from '@mozilla/readability'; | ||
import { JSDOM } from 'jsdom'; | ||
import { NodeHtmlMarkdown } from 'node-html-markdown'; | ||
|
||
const BASE_URL = process.env.BROWSERLESS_URL ?? 'https://chrome.browserless.io'; | ||
const BROWSERLESS_TOKEN = process.env.BROWSERLESS_TOKEN; | ||
|
||
export const htmlToMarkdown = (html: string, url: string) => { | ||
const doc = new JSDOM(html, { url }); | ||
|
||
const article = new Readability(doc.window.document).parse(); | ||
const content = NodeHtmlMarkdown.translate(article?.content || '', {}); | ||
|
||
return { ...article, content }; | ||
}; | ||
|
||
const runner = async ({ url }: { url: string }) => { | ||
const input = { | ||
gotoOptions: { waitUntil: 'networkidle2' }, | ||
url, | ||
}; | ||
|
||
try { | ||
const res = await fetch(`${BASE_URL}/content?token=${BROWSERLESS_TOKEN}`, { | ||
body: JSON.stringify(input), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
}); | ||
const html = await res.text(); | ||
|
||
const article = htmlToMarkdown(html, url); | ||
|
||
return { content: article.content, title: article?.title, url, website: article?.siteName }; | ||
} catch (error) { | ||
console.error(error); | ||
return { content: '抓取失败', errorMessage: (error as any).message, url }; | ||
} | ||
}; | ||
|
||
export default runner; |
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,15 @@ | ||
import fetchContent from './_utils'; | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
}; | ||
|
||
export default async (req: Request) => { | ||
if (req.method !== 'POST') return new Response('Method Not Allowed', { status: 405 }); | ||
|
||
const args = (await req.json()) as { url: string }; | ||
|
||
const result = await fetchContent(args); | ||
|
||
return new Response(JSON.stringify(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,35 @@ | ||
export type Result = { | ||
content: string; | ||
title?: string; | ||
url: string; | ||
website?: string; | ||
}; | ||
|
||
export interface ParserResponse { | ||
/** author metadata */ | ||
byline: string; | ||
|
||
/** HTML string of processed article content */ | ||
content: string; | ||
|
||
/** content direction */ | ||
dir: string; | ||
|
||
/** article description, or short excerpt from the content */ | ||
excerpt: string; | ||
|
||
/** content language */ | ||
lang: string; | ||
|
||
/** length of an article, in characters */ | ||
length: number; | ||
|
||
/** name of the site */ | ||
siteName: string; | ||
|
||
/** text content of the article, with all the HTML tags removed */ | ||
textContent: string; | ||
|
||
/** article title */ | ||
title: string; | ||
} |
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