0
v-firecrawl/apps/api/src/controllers/crawl.ts

106 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-04-20 19:38:05 -04:00
import { Request, Response } from "express";
import { WebScraperDataProvider } from "../../src/scraper/WebScraper";
import { billTeam } from "../../src/services/billing/credit_billing";
import { checkTeamCredits } from "../../src/services/billing/credit_billing";
import { authenticateUser } from "./auth";
import { RateLimiterMode } from "../../src/types";
import { addWebScraperJob } from "../../src/services/queue-jobs";
import { isUrlBlocked } from "../../src/scraper/WebScraper/utils/blocklist";
2024-05-06 20:16:43 -04:00
import { logCrawl } from "../../src/services/logging/crawl_log";
2024-05-07 14:29:27 -04:00
import { validateIdempotencyKey } from "../../src/services/idempotency/validate";
import { createIdempotencyKey } from "../../src/services/idempotency/create";
2024-04-20 19:38:05 -04:00
export async function crawlController(req: Request, res: Response) {
try {
const { success, team_id, error, status } = await authenticateUser(
req,
res,
RateLimiterMode.Crawl
);
if (!success) {
return res.status(status).json({ error });
}
2024-05-07 14:29:27 -04:00
if (req.headers["x-idempotency-key"]) {
const isIdempotencyValid = await validateIdempotencyKey(req);
if (!isIdempotencyValid) {
return res.status(409).json({ error: "Idempotency key already used" });
}
2024-05-23 10:47:04 -04:00
try {
createIdempotencyKey(req);
} catch (error) {
console.error(error);
return res.status(500).json({ error: error.message });
}
2024-05-07 14:29:27 -04:00
}
const { success: creditsCheckSuccess, message: creditsCheckMessage } =
await checkTeamCredits(team_id, 1);
if (!creditsCheckSuccess) {
return res.status(402).json({ error: "Insufficient credits" });
2024-04-20 19:38:05 -04:00
}
const url = req.body.url;
if (!url) {
return res.status(400).json({ error: "Url is required" });
}
if (isUrlBlocked(url)) {
2024-05-06 20:16:43 -04:00
return res
.status(403)
.json({
error:
"Firecrawl currently does not support social media scraping due to policy restrictions. We're actively working on building support for it.",
});
}
2024-05-06 20:16:43 -04:00
2024-04-20 19:38:05 -04:00
const mode = req.body.mode ?? "crawl";
2024-06-12 10:27:06 -04:00
const crawlerOptions = req.body.crawlerOptions ?? { allowBackwardCrawling: false };
2024-05-07 12:40:24 -04:00
const pageOptions = req.body.pageOptions ?? { onlyMainContent: false, includeHtml: false };
2024-04-20 19:38:05 -04:00
if (mode === "single_urls" && !url.includes(",")) {
try {
const a = new WebScraperDataProvider();
await a.setOptions({
mode: "single_urls",
urls: [url],
2024-06-12 10:27:06 -04:00
crawlerOptions: { ...crawlerOptions, returnOnlyUrls: true },
2024-04-20 19:38:05 -04:00
pageOptions: pageOptions,
});
const docs = await a.getDocuments(false, (progress) => {
job.progress({
current: progress.current,
total: progress.total,
current_step: "SCRAPING",
current_url: progress.currentDocumentUrl,
});
});
return res.json({
success: true,
documents: docs,
});
} catch (error) {
console.error(error);
return res.status(500).json({ error: error.message });
}
}
2024-05-06 20:16:43 -04:00
2024-04-20 19:38:05 -04:00
const job = await addWebScraperJob({
url: url,
mode: mode ?? "crawl", // fix for single urls not working
2024-06-11 14:24:39 -04:00
crawlerOptions: crawlerOptions,
2024-04-20 19:38:05 -04:00
team_id: team_id,
pageOptions: pageOptions,
2024-04-20 22:37:45 -04:00
origin: req.body.origin ?? "api",
2024-04-20 19:38:05 -04:00
});
2024-05-06 20:16:43 -04:00
await logCrawl(job.id.toString(), team_id);
2024-04-20 19:38:05 -04:00
res.json({ jobId: job.id });
} catch (error) {
console.error(error);
return res.status(500).json({ error: error.message });
}
}