Implementation:1broseidon Ketch scrape NewFromConfig
| Knowledge Sources | |
|---|---|
| Domains | Scrape, Configuration |
| Last Updated | 2026-10-27 00:00 GMT |
Overview
Builds the shared `*scrape.Scraper` from operator config — compiled URL rewriter, lazy browser connection, SPA markers, cookies, and extract mode — constructed once and reused by the CLI, MCP server, and crawler.
Description
NewFromConfig is the single constructor for the cache-aware scrape pipeline. It compiles the `url_rewrites` rules into a `*urlrewrite.Rewriter` once (callers reuse one Scraper), folds operator `spa_markers` into the JS-shell detector, parses the `extract_mode`, normalizes the User-Agent, loads the Netscape cookie jar, and wires an optional external PDF converter. The returned Scraper is safe for concurrent use and must be `Close`d by the caller to release the headless browser.
Usage
Construct once per process. The CLI builds one per command; the MCP server builds one in `NewServer` and shares it across all concurrent tool calls; the crawler reuses the same Scraper it was handed.
Code Reference
Source Location
- Repository: ketch
- File: scrape/pipeline.go
Signature
// NewFromConfig builds a Scraper from cfg: compiled URL rewriter, optional
// browser binary, and operator-configured SPA markers. The rewriter's regexes
// are compiled once here — callers should construct one Scraper and reuse it.
// The returned Scraper is safe for concurrent use and must be Closed by the caller.
func NewFromConfig(cfg *config.Config) (*Scraper, error)
Import
import (
"github.com/1broseidon/ketch/config"
"github.com/1broseidon/ketch/scrape"
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cfg | *config.Config | Yes | Operator config (url_rewrites, browser, spa_markers, extract_mode, user_agent, cookie_file, external PDF converter) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | *scrape.Scraper | Concurrent-safe scraper with compiled rewriter + lazy browser |
| return error | error | Invalid url_rewrites, extract_mode, user_agent, cookie_file, or PDF converter |
Usage Examples
CLI / MCP Construction
scraper, err := scrape.NewFromConfig(cfg)
if err != nil {
return fmt.Errorf("scraper: %w", err)
}
defer scraper.Close()
page, err := scraper.CachedScrape(ctx, cache, "https://example.com")