Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:1broseidon Ketch crawl Crawl

From Leeroopedia
Knowledge Sources
Domains Crawl, BFS_Traversal
Last Updated 2026-10-27 00:00 GMT

Overview

BFS/sitemap crawler with a worker pool: rewrites and validates the seed, compiles allow/deny filters, and drains a same-host work queue calling a per-page callback.

Description

Crawl is the entry point. It rewrites the seed via the shared Scraper, validates it with `ValidateWebURL`, compiles the deny regexps, and constructs a `*crawler` with a mutex-guarded `visited` map and per-host JS-shell stats. crawler.run seeds the queue either from the sitemap (sitemap mode) or with the single seed at depth 0, then a worker pool (default 8) drains buffered work channel. Each processItem scrapes the page via `ScrapeConditional` (reusing the shared pipeline), records JS detection per host for adaptive browser escalation, and — for BFS at depth < max — extracts same-host links and enqueues them.

Usage

Used by the CLI foreground crawl (streaming results to stdout), the background crawl (detached process writing status JSON), and the MCP bounded crawl (collector with max_pages/timeout caps). The caller supplies the per-page callback `fn func(Result)`.

Code Reference

Source Location

  • Repository: ketch
  • File: crawl/crawl.go

Signature

type Options struct {
	Depth       int      // max BFS depth, default 3
	Concurrency int      // worker pool size, default 8
	Allow       []string // path substrings that URLs must contain (any match passes)
	Deny        []string // regex patterns to reject URLs
}

type Result struct {
	Page   *scrape.Page `json:"page,omitempty"`
	Depth  int          `json:"depth"`
	Status string       `json:"status"` // "new", "changed", "unchanged"
	Source string       `json:"source"` // "seed", "link", "sitemap"
	Error  string       `json:"error,omitempty"`
	URL    string       `json:"url"`
}

// Crawl performs a BFS crawl from the seed, calling fn for each page.
func Crawl(ctx context.Context, seed string, s *scrape.Scraper, opts Options,
	pc *cache.Cache, sitemap bool, fn func(Result)) error

Import

import (
	"context"
	"github.com/1broseidon/ketch/cache"
	"github.com/1broseidon/ketch/crawl"
	"github.com/1broseidon/ketch/scrape"
)

I/O Contract

Inputs

Name Type Required Description
ctx context.Context Yes Bounds the whole crawl; cancellation stops workers
seed string Yes Starting URL (rewritten then validated)
s *scrape.Scraper Yes Shared scraper (pipeline + browser + rewriter)
opts Options Yes Depth, concurrency, allow/deny filters
pc *cache.Cache No Page cache (nil disables)
sitemap bool Yes Treat seed as a sitemap
fn func(Result) Yes Per-page callback

Outputs

Name Type Description
return error Invalid seed, bad deny pattern, or context cancellation

Usage Examples

Foreground BFS Crawl

err := crawl.Crawl(ctx, "https://example.com", scraper, crawl.Options{
	Depth:       3,
	Concurrency: 8,
	Deny:        []string{`\?sort=`},
}, pageCache, false, func(r crawl.Result) {
	fmt.Println(r.URL, r.Status)
})

Sitemap Crawl

// sitemap=true seeds the queue with the sitemap's URLs at depth 0.
err := crawl.Crawl(ctx, "https://example.com/sitemap.xml", scraper, opts, pc, true, fn)

Related Pages

Implements Principle

Uses Heuristic

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment