2024-08-29 03:27:29 +08:00
|
|
|
import FirecrawlApp, { CrawlStatusResponse, CrawlResponse } from '@mendable/firecrawl-js';
|
2024-05-09 21:36:56 +08:00
|
|
|
|
|
|
|
const app = new FirecrawlApp({apiKey: "fc-YOUR_API_KEY"});
|
|
|
|
|
2024-08-29 03:27:29 +08:00
|
|
|
const main = async () => {
|
2024-06-18 22:43:06 +08:00
|
|
|
|
2024-08-29 03:27:29 +08:00
|
|
|
// Scrape a website:
|
|
|
|
const scrapeResult = await app.scrapeUrl('firecrawl.dev');
|
2024-05-09 21:36:56 +08:00
|
|
|
|
2024-08-29 03:27:29 +08:00
|
|
|
if (scrapeResult) {
|
|
|
|
console.log(scrapeResult.markdown)
|
|
|
|
}
|
2024-05-09 21:36:56 +08:00
|
|
|
|
2024-08-29 03:27:29 +08:00
|
|
|
// Crawl a website:
|
|
|
|
// @ts-ignore
|
|
|
|
const crawlResult = await app.crawlUrl('mendable.ai', { excludePaths: ['blog/*'], limit: 5}, false) as CrawlResponse;
|
|
|
|
console.log(crawlResult)
|
|
|
|
|
|
|
|
const id = crawlResult.id;
|
|
|
|
console.log(id);
|
|
|
|
|
|
|
|
let checkStatus: CrawlStatusResponse;
|
|
|
|
while (true) {
|
|
|
|
checkStatus = await app.checkCrawlStatus(id);
|
|
|
|
if (checkStatus.status === 'completed') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second
|
|
|
|
}
|
2024-05-09 21:36:56 +08:00
|
|
|
|
2024-08-29 03:27:29 +08:00
|
|
|
if (checkStatus.data) {
|
|
|
|
console.log(checkStatus.data[0].markdown);
|
2024-05-09 21:36:56 +08:00
|
|
|
}
|
|
|
|
|
2024-08-30 02:01:16 +08:00
|
|
|
// Map a website:
|
2024-08-29 03:27:29 +08:00
|
|
|
const mapResult = await app.mapUrl('https://firecrawl.dev');
|
|
|
|
console.log(mapResult)
|
2024-08-30 02:01:16 +08:00
|
|
|
|
|
|
|
// Crawl a website with WebSockets:
|
|
|
|
const watch = await app.crawlUrlAndWatch('mendable.ai', { excludePaths: ['blog/*'], limit: 5});
|
|
|
|
|
|
|
|
watch.addEventListener("document", doc => {
|
|
|
|
console.log("DOC", doc.detail);
|
|
|
|
});
|
|
|
|
|
|
|
|
watch.addEventListener("error", err => {
|
|
|
|
console.error("ERR", err.detail.error);
|
|
|
|
});
|
|
|
|
|
|
|
|
watch.addEventListener("done", state => {
|
|
|
|
console.log("DONE", state.detail.status);
|
|
|
|
});
|
2024-06-18 22:43:06 +08:00
|
|
|
}
|
|
|
|
|
2024-08-29 03:27:29 +08:00
|
|
|
main()
|