Spaces:
Sleeping
Sleeping
File size: 8,480 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | import { ExtractorOptions, PageOptions } from "./../../lib/entities";
import { Request, Response } from "express";
import { checkTeamCredits } from "../../services/billing/credit_billing";
import { authenticateUser } from "../auth";
import { RateLimiterMode, AuthResponse } from "../../types";
import { TeamFlags, toLegacyDocument, url as urlSchema } from "../v1/types";
import { isUrlBlocked } from "../../scraper/WebScraper/utils/blocklist"; // Import the isUrlBlocked function
import {
defaultPageOptions,
defaultExtractorOptions,
defaultTimeout,
defaultOrigin,
} from "../../lib/default-values";
import { addScrapeJob, waitForJob } from "../../services/queue-jobs";
import { redisEvictConnection } from "../../../src/services/redis";
import { v7 as uuidv7 } from "uuid";
import { logger } from "../../lib/logger";
import * as Sentry from "@sentry/node";
import { getJobPriority } from "../../lib/job-priority";
import { ZodError } from "zod";
import { Document as V0Document } from "./../../lib/entities";
import { UNSUPPORTED_SITE_MESSAGE } from "../../lib/strings";
import { fromV0Combo } from "../v2/types";
import { ScrapeJobTimeoutError } from "../../lib/error";
import { scrapeQueue } from "../../services/worker/nuq";
import { getErrorContactMessage } from "../../lib/deployment";
import { logRequest } from "../../services/logging/log_job";
import { getScrapeZDR } from "../../lib/zdr-helpers";
async function scrapeHelper(
jobId: string,
req: Request,
team_id: string,
crawlerOptions: any,
pageOptions: PageOptions,
extractorOptions: ExtractorOptions,
timeout: number,
flags: TeamFlags,
apiKeyId: number | null,
): Promise<{
success: boolean;
error?: string;
data?: V0Document | { url: string };
returnCode: number;
}> {
let url: string;
try {
url = urlSchema.parse(req.body.url);
} catch (error) {
if (error instanceof ZodError) {
const errorMessage =
error.issues
.map(issue => issue.message)
.filter((msg, idx, arr) => arr.indexOf(msg) === idx)
.join("; ") || "Invalid URL";
return { success: false, error: errorMessage, returnCode: 400 };
}
throw error;
}
if (typeof url !== "string") {
return { success: false, error: "Url is required", returnCode: 400 };
}
if (
isUrlBlocked(url, flags, {
team_id,
origin: req.body?.origin ?? null,
})
) {
return {
success: false,
error: UNSUPPORTED_SITE_MESSAGE,
returnCode: 403,
};
}
const { scrapeOptions, internalOptions } = fromV0Combo(
pageOptions,
extractorOptions,
timeout,
crawlerOptions,
team_id,
);
internalOptions.saveScrapeResultToGCS = process.env
.GCS_FIRE_ENGINE_BUCKET_NAME
? true
: false;
await addScrapeJob(
{
url,
mode: "single_urls",
team_id,
scrapeOptions,
internalOptions,
origin: req.body.origin ?? defaultOrigin,
integration: req.body.integration,
billing: { endpoint: "scrape", jobId },
startTime: Date.now(),
zeroDataRetention: false, // not supported on v0
apiKeyId,
},
jobId,
await getJobPriority({ team_id, basePriority: 10 }),
false,
true,
);
let doc;
try {
doc = await waitForJob(jobId, timeout, false);
} catch (e) {
if (e instanceof ScrapeJobTimeoutError) {
return {
success: false,
error: e.message,
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;
}
}
const err = null;
if (err !== null) {
return err;
}
await scrapeQueue.removeJob(jobId);
if (!doc) {
console.error("!!! PANIC DOC IS", doc);
return {
success: true,
error: "No page found",
returnCode: 200,
data: doc,
};
}
delete doc.index;
delete doc.provider;
// Remove rawHtml if pageOptions.rawHtml is false and extractorOptions.mode is llm-extraction-from-raw-html
if (
!pageOptions.includeRawHtml &&
extractorOptions.mode == "llm-extraction-from-raw-html"
) {
if (doc.rawHtml) {
delete doc.rawHtml;
}
}
if (!pageOptions.includeHtml) {
if (doc.html) {
delete doc.html;
}
}
return {
success: true,
data: toLegacyDocument(doc, internalOptions),
returnCode: 200,
};
}
export async function scrapeController(req: Request, res: Response) {
try {
let earlyReturn = false;
// make sure to authenticate user first, Bearer <token>
const auth = await authenticateUser(req, res, RateLimiterMode.Scrape);
if (!auth.success) {
return res.status(auth.status).json({ error: auth.error });
}
const { team_id, chunk } = auth;
if (getScrapeZDR(chunk?.flags) === "forced") {
return res.status(400).json({
error:
"Your team has zero data retention enabled. This is not supported on the v0 API. Please update your code to use the v1 API.",
});
}
const jobId = uuidv7();
await logRequest({
id: jobId,
kind: "scrape",
api_version: "v0",
team_id,
origin: req.body.origin ?? "api",
integration: req.body.integration,
target_hint: req.body.url ?? "",
zeroDataRetention: false, // not supported on v0
api_key_id: chunk?.api_key_id ?? null,
});
redisEvictConnection.sadd("teams_using_v0", team_id).catch(error =>
logger.error("Failed to add team to teams_using_v0", {
error,
team_id,
}),
);
redisEvictConnection
.sadd("teams_using_v0:" + team_id, "scrape:" + jobId)
.catch(error =>
logger.error("Failed to add team to teams_using_v0 (2)", {
error,
team_id,
}),
);
const crawlerOptions = req.body.crawlerOptions ?? {};
const pageOptions = { ...defaultPageOptions, ...req.body.pageOptions };
const extractorOptions = {
...defaultExtractorOptions,
...req.body.extractorOptions,
};
const origin = req.body.origin ?? defaultOrigin;
let timeout = req.body.timeout ?? defaultTimeout;
if (extractorOptions.mode.includes("llm-extraction")) {
if (
typeof extractorOptions.extractionSchema !== "object" ||
extractorOptions.extractionSchema === null
) {
return res.status(400).json({
error:
"extractorOptions.extractionSchema must be an object if llm-extraction mode is specified",
});
}
pageOptions.onlyMainContent = true;
timeout = req.body.timeout ?? 90000;
}
// checkCredits
try {
const { success: creditsCheckSuccess, message: creditsCheckMessage } =
await checkTeamCredits(chunk, team_id, 1);
if (!creditsCheckSuccess) {
earlyReturn = true;
return res.status(402).json({
error:
"Insufficient credits. For more credits, you can upgrade your plan at https://firecrawl.dev/pricing",
});
}
} catch (error) {
logger.error(error);
earlyReturn = true;
return res.status(500).json({
error: getErrorContactMessage(),
});
}
const result = await scrapeHelper(
jobId,
req,
team_id,
crawlerOptions,
pageOptions,
extractorOptions,
timeout,
chunk?.flags ?? null,
chunk?.api_key_id ?? null,
);
let doc = result.data;
if (!pageOptions || !pageOptions.includeRawHtml) {
if (doc && (doc as V0Document).rawHtml) {
delete (doc as V0Document).rawHtml;
}
}
if (pageOptions && pageOptions.includeExtract) {
if (!pageOptions.includeMarkdown && doc && (doc as V0Document).markdown) {
delete (doc as V0Document).markdown;
}
}
return res.status(result.returnCode).json(result);
} catch (error) {
Sentry.captureException(error);
logger.error("Scrape error occcurred", { error });
return res.status(500).json({
error:
error instanceof ZodError
? "Invalid URL"
: typeof error === "string"
? error
: (error?.message ?? "Internal Server Error"),
});
}
}
|