mirror of
https://github.com/mendableai/firecrawl.git
synced 2025-07-19 15:06:15 +00:00

* feat: use strictNullChecking * feat: switch logger to Winston * feat(scrapeURL): first batch * fix(scrapeURL): error swallow * fix(scrapeURL): add timeout to EngineResultsTracker * fix(scrapeURL): report unexpected error to sentry * chore: remove unused modules * feat(transfomers/coerce): warn when a format's response is missing * feat(scrapeURL): feature flag priorities, engine quality sorting, PDF and DOCX support * (add note) * feat(scrapeURL): wip readme * feat(scrapeURL): LLM extract * feat(scrapeURL): better warnings * fix(scrapeURL/engines/fire-engine;playwright): fix screenshot * feat(scrapeURL): add forceEngine internal option * feat(scrapeURL/engines): scrapingbee * feat(scrapeURL/transformars): uploadScreenshot * feat(scrapeURL): more intense tests * bunch of stuff * get rid of WebScraper (mostly) * adapt batch scrape * add staging deploy workflow * fix yaml * fix logger issues * fix v1 test schema * feat(scrapeURL/fire-engine/chrome-cdp): remove wait inserts on actions * scrapeURL: v0 backwards compat * logger fixes * feat(scrapeurl): v0 returnOnlyUrls support * fix(scrapeURL/v0): URL leniency * fix(batch-scrape): ts non-nullable * fix(scrapeURL/fire-engine/chromecdp): fix wait action * fix(logger): remove error debug key * feat(requests.http): use dotenv expression * fix(scrapeURL/extractMetadata): extract custom metadata * fix crawl option conversion * feat(scrapeURL): Add retry logic to robustFetch * fix(scrapeURL): crawl stuff * fix(scrapeURL): LLM extract * fix(scrapeURL/v0): search fix * fix(tests/v0): grant larger response size to v0 crawl status * feat(scrapeURL): basic fetch engine * feat(scrapeURL): playwright engine * feat(scrapeURL): add url-specific parameters * Update readme and examples * added e2e tests for most parameters. Still a few actions, location and iframes to be done. * fixed type * Nick: * Update scrape.ts * Update index.ts * added actions and base64 check * Nick: skipTls feature flag? * 403 * todo * todo * fixes * yeet headers from url specific params * add warning when final engine has feature deficit * expose engine results tracker for ScrapeEvents implementation * ingest scrape events * fixed some tests * comment * Update index.test.ts * fixed rawHtml * Update index.test.ts * update comments * move geolocation to global f-e option, fix removeBase64Images * Nick: * trim url-specific params * Update index.ts --------- Co-authored-by: Eric Ciarla <ericciarla@yahoo.com> Co-authored-by: rafaelmmiller <8574157+rafaelmmiller@users.noreply.github.com> Co-authored-by: Nicolas <nicolascamara29@gmail.com>
133 lines
5.5 KiB
TypeScript
133 lines
5.5 KiB
TypeScript
import { load } from "cheerio";
|
|
import { Document } from "../../../controllers/v1/types";
|
|
import { Meta } from "..";
|
|
|
|
export function extractMetadata(meta: Meta, html: string): Document["metadata"] {
|
|
let title: string | undefined = undefined;
|
|
let description: string | undefined = undefined;
|
|
let language: string | undefined = undefined;
|
|
let keywords: string | undefined = undefined;
|
|
let robots: string | undefined = undefined;
|
|
let ogTitle: string | undefined = undefined;
|
|
let ogDescription: string | undefined = undefined;
|
|
let ogUrl: string | undefined = undefined;
|
|
let ogImage: string | undefined = undefined;
|
|
let ogAudio: string | undefined = undefined;
|
|
let ogDeterminer: string | undefined = undefined;
|
|
let ogLocale: string | undefined = undefined;
|
|
let ogLocaleAlternate: string[] | undefined = undefined;
|
|
let ogSiteName: string | undefined = undefined;
|
|
let ogVideo: string | undefined = undefined;
|
|
let dcTermsCreated: string | undefined = undefined;
|
|
let dcDateCreated: string | undefined = undefined;
|
|
let dcDate: string | undefined = undefined;
|
|
let dcTermsType: string | undefined = undefined;
|
|
let dcType: string | undefined = undefined;
|
|
let dcTermsAudience: string | undefined = undefined;
|
|
let dcTermsSubject: string | undefined = undefined;
|
|
let dcSubject: string | undefined = undefined;
|
|
let dcDescription: string | undefined = undefined;
|
|
let dcTermsKeywords: string | undefined = undefined;
|
|
let modifiedTime: string | undefined = undefined;
|
|
let publishedTime: string | undefined = undefined;
|
|
let articleTag: string | undefined = undefined;
|
|
let articleSection: string | undefined = undefined;
|
|
const customMetadata: Record<string, string | string[]> = {};
|
|
|
|
const soup = load(html);
|
|
|
|
try {
|
|
title = soup("title").text() || undefined;
|
|
description = soup('meta[name="description"]').attr("content") || undefined;
|
|
|
|
// Assuming the language is part of the URL as per the regex pattern
|
|
language = soup('html').attr('lang') || undefined;
|
|
|
|
keywords = soup('meta[name="keywords"]').attr("content") || undefined;
|
|
robots = soup('meta[name="robots"]').attr("content") || undefined;
|
|
ogTitle = soup('meta[property="og:title"]').attr("content") || undefined;
|
|
ogDescription = soup('meta[property="og:description"]').attr("content") || undefined;
|
|
ogUrl = soup('meta[property="og:url"]').attr("content") || undefined;
|
|
ogImage = soup('meta[property="og:image"]').attr("content") || undefined;
|
|
ogAudio = soup('meta[property="og:audio"]').attr("content") || undefined;
|
|
ogDeterminer = soup('meta[property="og:determiner"]').attr("content") || undefined;
|
|
ogLocale = soup('meta[property="og:locale"]').attr("content") || undefined;
|
|
ogLocaleAlternate = soup('meta[property="og:locale:alternate"]').map((i, el) => soup(el).attr("content")).get() || undefined;
|
|
ogSiteName = soup('meta[property="og:site_name"]').attr("content") || undefined;
|
|
ogVideo = soup('meta[property="og:video"]').attr("content") || undefined;
|
|
articleSection = soup('meta[name="article:section"]').attr("content") || undefined;
|
|
articleTag = soup('meta[name="article:tag"]').attr("content") || undefined;
|
|
publishedTime = soup('meta[property="article:published_time"]').attr("content") || undefined;
|
|
modifiedTime = soup('meta[property="article:modified_time"]').attr("content") || undefined;
|
|
dcTermsKeywords = soup('meta[name="dcterms.keywords"]').attr("content") || undefined;
|
|
dcDescription = soup('meta[name="dc.description"]').attr("content") || undefined;
|
|
dcSubject = soup('meta[name="dc.subject"]').attr("content") || undefined;
|
|
dcTermsSubject = soup('meta[name="dcterms.subject"]').attr("content") || undefined;
|
|
dcTermsAudience = soup('meta[name="dcterms.audience"]').attr("content") || undefined;
|
|
dcType = soup('meta[name="dc.type"]').attr("content") || undefined;
|
|
dcTermsType = soup('meta[name="dcterms.type"]').attr("content") || undefined;
|
|
dcDate = soup('meta[name="dc.date"]').attr("content") || undefined;
|
|
dcDateCreated = soup('meta[name="dc.date.created"]').attr("content") || undefined;
|
|
dcTermsCreated = soup('meta[name="dcterms.created"]').attr("content") || undefined;
|
|
|
|
try {
|
|
// Extract all meta tags for custom metadata
|
|
soup("meta").each((i, elem) => {
|
|
try {
|
|
const name = soup(elem).attr("name") || soup(elem).attr("property");
|
|
const content = soup(elem).attr("content");
|
|
|
|
if (name && content) {
|
|
if (customMetadata[name] === undefined) {
|
|
customMetadata[name] = content;
|
|
} else if (Array.isArray(customMetadata[name])) {
|
|
(customMetadata[name] as string[]).push(content);
|
|
} else {
|
|
customMetadata[name] = [customMetadata[name] as string, content];
|
|
}
|
|
}
|
|
} catch (error) {
|
|
meta.logger.error(`Error extracting custom metadata (in)`, { error });
|
|
}
|
|
});
|
|
} catch (error) {
|
|
meta.logger.error(`Error extracting custom metadata`, { error });
|
|
}
|
|
} catch (error) {
|
|
meta.logger.error(`Error extracting metadata`, { error });
|
|
}
|
|
|
|
return {
|
|
title,
|
|
description,
|
|
language,
|
|
keywords,
|
|
robots,
|
|
ogTitle,
|
|
ogDescription,
|
|
ogUrl,
|
|
ogImage,
|
|
ogAudio,
|
|
ogDeterminer,
|
|
ogLocale,
|
|
ogLocaleAlternate,
|
|
ogSiteName,
|
|
ogVideo,
|
|
dcTermsCreated,
|
|
dcDateCreated,
|
|
dcDate,
|
|
dcTermsType,
|
|
dcType,
|
|
dcTermsAudience,
|
|
dcTermsSubject,
|
|
dcSubject,
|
|
dcDescription,
|
|
dcTermsKeywords,
|
|
modifiedTime,
|
|
publishedTime,
|
|
articleTag,
|
|
articleSection,
|
|
...customMetadata,
|
|
};
|
|
}
|