Spaces:
Sleeping
Sleeping
Download apps/api/src/scraper/scrapeURL/transformers/performAttributes.ts from s-eto-hub/firecrawl-api: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/spaces/s-eto-hub/firecrawl-api/resolve/main/apps/api/src/scraper/scrapeURL/transformers/performAttributes.ts
- Command line
-
hf download hf://spaces/s-eto-hub/firecrawl-api/apps/api/src/scraper/scrapeURL/transformers/performAttributes.ts
-
curl -L -o performAttributes.ts https://huggingface.co/spaces/s-eto-hub/firecrawl-api/resolve/main/apps/api/src/scraper/scrapeURL/transformers/performAttributes.ts
1.4 kB
| import { Document } from "../../../controllers/v2/types"; | |
| import { Meta } from ".."; | |
| import { extractAttributes } from "../lib/extractAttributes"; | |
| import { hasFormatOfType } from "../../../lib/format-utils"; | |
| /** | |
| * Transformer to extract attributes from HTML using the attributes format | |
| */ | |
| export async function performAttributes( | |
| meta: Meta, | |
| document: Document, | |
| ): Promise<Document> { | |
| const attributesFormat = hasFormatOfType(meta.options.formats, "attributes"); | |
| if (!attributesFormat) { | |
| return document; | |
| } | |
| if (document.html === undefined) { | |
| throw new Error( | |
| "html is undefined -- this transformer is being called out of order", | |
| ); | |
| } | |
| if (attributesFormat.selectors && attributesFormat.selectors.length > 0) { | |
| try { | |
| const attributes = await extractAttributes( | |
| document.html, | |
| attributesFormat.selectors, | |
| ); | |
| if (attributes.length > 0) { | |
| document.attributes = attributes; | |
| meta.logger.debug("Extracted attributes", { | |
| count: attributes.length, | |
| attributes: attributes.map(d => ({ | |
| selector: d.selector, | |
| attribute: d.attribute, | |
| valuesCount: d.values.length, | |
| })), | |
| }); | |
| } | |
| } catch (error) { | |
| meta.logger.error("Failed to extract attributes", { | |
| error, | |
| url: meta.url, | |
| }); | |
| } | |
| } | |
| return document; | |
| } | |