|
| 1 | +import firecrawl from "../../firecrawl.app.mjs"; |
| 2 | +import { ConfigurationError } from "@pipedream/platform"; |
| 3 | +import { parseObjectEntries } from "../../common/utils.mjs"; |
| 4 | + |
| 5 | +export default { |
| 6 | + key: "firecrawl-extract-data", |
| 7 | + name: "Extract Data", |
| 8 | + description: "Extract structured data from one or multiple URLs. [See the documentation](https://docs.firecrawl.dev/api-reference/endpoint/extract)", |
| 9 | + version: "0.0.1", |
| 10 | + type: "action", |
| 11 | + props: { |
| 12 | + firecrawl, |
| 13 | + urls: { |
| 14 | + type: "string[]", |
| 15 | + label: "URLs", |
| 16 | + description: "An array of one or more URLs. Supports wildcards (/*) for broader crawling.", |
| 17 | + }, |
| 18 | + prompt: { |
| 19 | + type: "string", |
| 20 | + label: "Prompt", |
| 21 | + description: "(Optional unless no schema): A natural language prompt describing the data you want or specifying how you want that data structured.", |
| 22 | + optional: true, |
| 23 | + }, |
| 24 | + schema: { |
| 25 | + type: "object", |
| 26 | + label: "Schema", |
| 27 | + description: "(Optional unless no prompt): A more rigid structure if you already know the JSON layout.", |
| 28 | + optional: true, |
| 29 | + }, |
| 30 | + enableWebSearch: { |
| 31 | + type: "boolean", |
| 32 | + label: "Enable Web Search", |
| 33 | + description: "When `true`, the extraction will use web search to find additional data", |
| 34 | + optional: true, |
| 35 | + }, |
| 36 | + ignoreSitemap: { |
| 37 | + type: "boolean", |
| 38 | + label: "Ignore Sitemap", |
| 39 | + description: "When true, sitemap.xml files will be ignored during website scanning", |
| 40 | + optional: true, |
| 41 | + }, |
| 42 | + includeSubdomains: { |
| 43 | + type: "boolean", |
| 44 | + label: "Include Subdomains", |
| 45 | + description: "When true, subdomains of the provided URLs will also be scanned", |
| 46 | + optional: true, |
| 47 | + }, |
| 48 | + showSources: { |
| 49 | + type: "boolean", |
| 50 | + label: "Show Sources", |
| 51 | + description: "When true, the sources used to extract the data will be included in the response", |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + waitForCompletion: { |
| 55 | + type: "boolean", |
| 56 | + label: "Wait For Completion", |
| 57 | + description: "Set to `true` to poll the API in 3-second intervals until the job is completed", |
| 58 | + optional: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + async run({ $ }) { |
| 62 | + if (!this.prompt && !this.schema) { |
| 63 | + throw new ConfigurationError("Must enter one of Prompt or Schema"); |
| 64 | + } |
| 65 | + |
| 66 | + let response = await this.firecrawl.extract({ |
| 67 | + $, |
| 68 | + data: { |
| 69 | + urls: this.urls, |
| 70 | + prompt: this.prompt, |
| 71 | + schema: this.schema && parseObjectEntries(this.schema), |
| 72 | + enableWebSearch: this.enableWebSearch, |
| 73 | + ignoreSitemap: this.ignoreSitemap, |
| 74 | + includeSubdomains: this.includeSubdomains, |
| 75 | + showSources: this.showSources, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + if (this.waitForCompletion) { |
| 80 | + const id = response.id; |
| 81 | + const timer = (ms) => new Promise((res) => setTimeout(res, ms)); |
| 82 | + do { |
| 83 | + response = await this.firecrawl.getExtractStatus({ |
| 84 | + $, |
| 85 | + id, |
| 86 | + }); |
| 87 | + await timer(3000); |
| 88 | + } while (response.status === "processing"); |
| 89 | + } |
| 90 | + |
| 91 | + if (response.success) { |
| 92 | + $.export("$summary", "Successfully extracted data."); |
| 93 | + } |
| 94 | + return response; |
| 95 | + }, |
| 96 | +}; |
0 commit comments