-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.ts
More file actions
40 lines (35 loc) · 1.31 KB
/
scrape.ts
File metadata and controls
40 lines (35 loc) · 1.31 KB
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
import { defineCommand } from "citty";
import * as scrapegraphai from "scrapegraph-js";
import { resolveApiKey } from "../lib/folders.js";
import * as log from "../lib/log.js";
export default defineCommand({
meta: {
name: "scrape",
description: "Get raw HTML content from a URL",
},
args: {
url: {
type: "positional",
description: "Website URL to scrape",
required: true,
},
stealth: { type: "boolean", description: "Bypass bot detection (+4 credits)" },
branding: { type: "boolean", description: "Extract branding info (+2 credits)" },
"country-code": { type: "string", description: "ISO country code for geo-targeting" },
json: { type: "boolean", description: "Output raw JSON (pipeable)" },
},
run: async ({ args }) => {
const out = log.create(!!args.json);
out.docs("https://docs.scrapegraphai.com/services/scrape");
const key = await resolveApiKey(!!args.json);
const params: scrapegraphai.ScrapeParams = { website_url: args.url };
if (args.stealth) params.stealth = true;
if (args.branding) params.branding = true;
if (args["country-code"]) params.country_code = args["country-code"];
out.start("Scraping");
const result = await scrapegraphai.scrape(key, params);
out.stop(result.elapsedMs);
if (result.data) out.result(result.data);
else out.error(result.error);
},
});