Files
firecrawl/apps/api/src/controllers/v0/scrape.ts
T

304 lines
8.1 KiB
TypeScript
Raw Normal View History

2024-08-28 12:46:59 -03:00
import { ExtractorOptions, PageOptions } from "./../../lib/entities";
2024-04-20 16:38:05 -07:00
import { Request, Response } from "express";
2024-08-28 12:46:59 -03:00
import {
billTeam,
checkTeamCredits,
} from "../../services/billing/credit_billing";
2024-08-26 18:48:00 -03:00
import { authenticateUser } from "../auth";
2024-08-28 12:42:23 -03:00
import { PlanType, RateLimiterMode } from "../../types";
2024-08-15 21:51:59 +02:00
import { logJob } from "../../services/logging/log_job";
import { Document } from "../../lib/entities";
import { isUrlBlocked } from "../../scraper/WebScraper/utils/blocklist"; // Import the isUrlBlocked function
2024-08-28 12:46:59 -03:00
import { numTokensFromString } from "../../lib/LLM-extraction/helpers";
import {
defaultPageOptions,
defaultExtractorOptions,
defaultTimeout,
defaultOrigin,
} from "../../lib/default-values";
import { addScrapeJob, waitForJob } from "../../services/queue-jobs";
import { getScrapeQueue } from "../../services/queue-service";
2024-07-24 14:31:25 +02:00
import { v4 as uuidv4 } from "uuid";
2024-08-28 12:46:59 -03:00
import { Logger } from "../../lib/logger";
import * as Sentry from "@sentry/node";
2024-08-28 12:46:59 -03:00
import { getJobPriority } from "../../lib/job-priority";
2024-04-20 16:38:05 -07:00
export async function scrapeHelper(
2024-07-24 15:18:12 +02:00
jobId: string,
2024-04-20 16:38:05 -07:00
req: Request,
team_id: string,
crawlerOptions: any,
2024-05-06 11:36:44 -03:00
pageOptions: PageOptions,
2024-05-06 19:45:56 -03:00
extractorOptions: ExtractorOptions,
2024-05-30 14:46:55 -07:00
timeout: number,
2024-08-21 20:54:39 -03:00
plan?: PlanType
2024-04-20 18:55:39 -07:00
): Promise<{
success: boolean;
error?: string;
data?: Document;
2024-04-20 19:04:27 -07:00
returnCode: number;
2024-04-20 18:55:39 -07:00
}> {
2024-04-20 16:38:05 -07:00
const url = req.body.url;
2024-09-10 21:18:53 +02:00
if (typeof url !== "string") {
2024-04-20 18:55:39 -07:00
return { success: false, error: "Url is required", returnCode: 400 };
2024-04-20 16:38:05 -07:00
}
if (isUrlBlocked(url)) {
2024-08-28 12:46:59 -03:00
return {
success: false,
error:
"Firecrawl currently does not support social media scraping due to policy restrictions. We're actively working on building support for it.",
returnCode: 403,
};
}
2024-08-28 12:46:59 -03:00
const jobPriority = await getJobPriority({ plan, team_id, basePriority: 10 });
2024-08-21 20:54:39 -03:00
2024-08-28 12:46:59 -03:00
const job = await addScrapeJob(
{
url,
mode: "single_urls",
crawlerOptions,
team_id,
pageOptions,
extractorOptions,
origin: req.body.origin ?? defaultOrigin,
2024-08-28 18:32:45 -03:00
is_scrape: true,
2024-08-28 12:46:59 -03:00
},
{},
jobId,
jobPriority
);
2024-07-25 00:14:25 +02:00
2024-08-06 17:25:58 +02:00
let doc;
2024-08-22 16:00:13 +02:00
2024-08-28 12:46:59 -03:00
const err = await Sentry.startSpan(
{
name: "Wait for job to finish",
op: "bullmq.wait",
attributes: { job: jobId },
},
async (span) => {
try {
doc = (await waitForJob(job.id, timeout))[0];
} catch (e) {
if (e instanceof Error && e.message.startsWith("Job wait")) {
span.setAttribute("timedOut", true);
return {
success: false,
error: "Request timed out",
returnCode: 408,
};
} else if (
typeof e === "string" &&
(e.includes("Error generating completions: ") ||
e.includes("Invalid schema for function") ||
e.includes(
"LLM extraction did not match the extraction schema you provided."
))
) {
return {
success: false,
error: e,
returnCode: 500,
};
} else {
throw e;
2024-08-22 16:00:13 +02:00
}
2024-08-06 17:25:58 +02:00
}
2024-08-28 12:46:59 -03:00
span.setAttribute("result", JSON.stringify(doc));
return null;
2024-08-06 17:25:58 +02:00
}
2024-08-28 12:46:59 -03:00
);
2024-08-22 16:00:13 +02:00
if (err !== null) {
return err;
2024-08-06 17:25:58 +02:00
}
2024-07-25 00:14:25 +02:00
await job.remove();
2024-07-25 00:14:25 +02:00
if (!doc) {
2024-08-06 16:26:46 +02:00
console.error("!!! PANIC DOC IS", doc, job);
2024-08-28 12:46:59 -03:00
return {
success: true,
error: "No page found",
returnCode: 200,
data: doc,
};
2024-04-20 16:38:05 -07:00
}
2024-07-25 00:50:03 +02:00
delete doc.index;
delete doc.provider;
2024-07-01 08:48:21 -04:00
// Remove rawHtml if pageOptions.rawHtml is false and extractorOptions.mode is llm-extraction-from-raw-html
2024-08-28 12:46:59 -03:00
if (
!pageOptions.includeRawHtml &&
extractorOptions.mode == "llm-extraction-from-raw-html"
) {
2024-08-22 15:15:45 -03:00
if (doc.rawHtml) {
delete doc.rawHtml;
}
}
if (!pageOptions.includeHtml) {
if (doc.html) {
delete doc.html;
}
2024-06-28 16:39:09 -04:00
}
2024-04-20 16:38:05 -07:00
return {
success: true,
2024-07-25 00:14:25 +02:00
data: doc,
2024-04-20 18:55:39 -07:00
returnCode: 200,
2024-04-20 16:38:05 -07:00
};
}
export async function scrapeController(req: Request, res: Response) {
try {
2024-07-22 18:30:58 -04:00
let earlyReturn = false;
2024-04-20 16:38:05 -07:00
// make sure to authenticate user first, Bearer <token>
2024-05-30 14:46:55 -07:00
const { success, team_id, error, status, plan } = await authenticateUser(
2024-04-20 16:38:05 -07:00
req,
res,
RateLimiterMode.Scrape
);
if (!success) {
return res.status(status).json({ error });
}
2024-07-22 18:30:58 -04:00
2024-04-20 16:38:05 -07:00
const crawlerOptions = req.body.crawlerOptions ?? {};
2024-06-26 09:00:54 -03:00
const pageOptions = { ...defaultPageOptions, ...req.body.pageOptions };
2024-08-28 12:46:59 -03:00
const extractorOptions = {
...defaultExtractorOptions,
...req.body.extractorOptions,
};
2024-07-17 20:44:34 -04:00
const origin = req.body.origin ?? defaultOrigin;
let timeout = req.body.timeout ?? defaultTimeout;
2024-07-22 18:30:58 -04:00
if (extractorOptions.mode.includes("llm-extraction")) {
2024-08-28 12:46:59 -03:00
if (
typeof extractorOptions.extractionSchema !== "object" ||
extractorOptions.extractionSchema === null
) {
2024-08-28 18:32:45 -03:00
return res.status(400).json({
error:
"extractorOptions.extractionSchema must be an object if llm-extraction mode is specified",
});
}
pageOptions.onlyMainContent = true;
2024-07-17 20:44:34 -04:00
timeout = req.body.timeout ?? 90000;
}
2024-04-20 16:38:05 -07:00
// checkCredits
try {
2024-08-28 12:46:59 -03:00
const { success: creditsCheckSuccess, message: creditsCheckMessage } =
await checkTeamCredits(team_id, 1);
if (!creditsCheckSuccess) {
2024-07-22 18:30:58 -04:00
earlyReturn = true;
return res.status(402).json({ error: "Insufficient credits" });
2024-04-20 16:38:05 -07:00
}
} catch (error) {
Logger.error(error);
earlyReturn = true;
2024-08-28 18:32:45 -03:00
return res.status(500).json({
error:
"Error checking team credits. Please contact hello@firecrawl.com for help.",
});
}
2024-07-22 18:30:58 -04:00
2024-07-24 15:18:12 +02:00
const jobId = uuidv4();
2024-04-20 19:37:45 -07:00
const startTime = new Date().getTime();
2024-04-20 18:55:39 -07:00
const result = await scrapeHelper(
2024-07-24 15:18:12 +02:00
jobId,
2024-04-20 18:55:39 -07:00
req,
team_id,
crawlerOptions,
2024-04-28 15:52:09 -07:00
pageOptions,
2024-05-06 19:45:56 -03:00
extractorOptions,
2024-05-30 14:46:55 -07:00
timeout,
plan
2024-04-20 18:55:39 -07:00
);
2024-04-20 19:37:45 -07:00
const endTime = new Date().getTime();
const timeTakenInSeconds = (endTime - startTime) / 1000;
2024-08-28 12:46:59 -03:00
const numTokens =
result.data && result.data.markdown
? numTokensFromString(result.data.markdown, "gpt-3.5-turbo")
: 0;
2024-04-30 09:20:15 -07:00
2024-07-22 18:30:58 -04:00
if (result.success) {
2024-08-28 18:32:45 -03:00
let creditsToBeBilled = 1;
2024-09-12 12:51:14 -04:00
const creditsPerLLMExtract = 4;
2024-07-22 18:30:58 -04:00
if (extractorOptions.mode.includes("llm-extraction")) {
2024-07-22 19:12:51 -04:00
// creditsToBeBilled = creditsToBeBilled + (creditsPerLLMExtract * filteredDocs.length);
2024-07-22 18:30:58 -04:00
creditsToBeBilled += creditsPerLLMExtract;
}
let startTimeBilling = new Date().getTime();
if (earlyReturn) {
// Don't bill if we're early returning
return;
}
2024-08-28 18:32:45 -03:00
if (creditsToBeBilled > 0) {
// billing for doc done on queue end, bill only for llm extraction
2024-09-03 21:09:32 -03:00
billTeam(team_id, creditsToBeBilled).catch(error => {
Logger.error(`Failed to bill team ${team_id} for ${creditsToBeBilled} credits: ${error}`);
// Optionally, you could notify an admin or add to a retry queue here
});
2024-07-22 18:30:58 -04:00
}
}
2024-08-30 17:32:41 -03:00
let doc = result.data;
if (!pageOptions || !pageOptions.includeRawHtml) {
if (doc && doc.rawHtml) {
delete doc.rawHtml;
}
}
if(pageOptions && pageOptions.includeExtract) {
if(!pageOptions.includeMarkdown && doc && doc.markdown) {
delete doc.markdown;
}
}
2024-07-22 18:30:58 -04:00
2024-04-20 18:55:39 -07:00
logJob({
2024-07-24 15:18:12 +02:00
job_id: jobId,
2024-04-20 18:55:39 -07:00
success: result.success,
message: result.error,
num_docs: 1,
2024-08-30 17:32:41 -03:00
docs: [doc],
2024-04-20 19:37:45 -07:00
time_taken: timeTakenInSeconds,
2024-04-20 18:55:39 -07:00
team_id: team_id,
mode: "scrape",
url: req.body.url,
crawlerOptions: crawlerOptions,
pageOptions: pageOptions,
2024-08-28 12:46:59 -03:00
origin: origin,
2024-04-30 09:20:15 -07:00
extractor_options: extractorOptions,
2024-05-06 19:45:56 -03:00
num_tokens: numTokens,
2024-04-20 18:55:39 -07:00
});
2024-07-22 18:30:58 -04:00
2024-04-20 19:04:27 -07:00
return res.status(result.returnCode).json(result);
2024-04-20 16:38:05 -07:00
} catch (error) {
2024-09-20 18:35:30 -04:00
if (typeof error === "string" && error.startsWith("{\"type\":\"all\",")) {
return res.status(500).json({
success: false,
error: "All scraping methods failed for URL: " + req.body.url,
details: JSON.parse(error).errors as string[],
});
} else {
Sentry.captureException(error);
Logger.error(error);
2024-09-20 18:35:30 -04:00
return res.status(500).json({
error:
2024-08-28 18:32:45 -03:00
typeof error === "string"
? error
: error?.message ?? "Internal Server Error",
2024-09-20 18:35:30 -04:00
});
}
2024-04-20 16:38:05 -07:00
}
}