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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#[cfg(feature = "api")]
use std::sync::Arc;
use std::time::Duration;
use clap::{Parser, Subcommand};
use kreuzcrawl::{CrawlConfig, CrawlEngine, PerDomainThrottle, ProxyConfig};
#[derive(Parser)]
#[command(name = "kreuzcrawl", about = "High-performance web crawler and scraper", version)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Scrape a single URL and extract metadata
Scrape {
/// URL to scrape
url: String,
/// Output format: json or markdown
#[arg(long, default_value = "json")]
format: String,
/// Proxy URL
#[arg(long)]
proxy: Option<String>,
/// Custom user agent
#[arg(long)]
user_agent: Option<String>,
/// Request timeout in milliseconds
#[arg(long, default_value = "30000")]
timeout: u64,
/// Respect robots.txt
#[arg(long)]
respect_robots_txt: bool,
},
/// Crawl a website following links
Crawl {
/// Seed URL(s) to crawl
#[arg(required = true)]
urls: Vec<String>,
/// Maximum crawl depth
#[arg(long, short = 'd', default_value = "2")]
depth: usize,
/// Maximum pages to crawl
#[arg(long, short = 'n')]
max_pages: Option<usize>,
/// Maximum concurrent requests
#[arg(long, short = 'c', default_value = "10")]
concurrent: usize,
/// Rate limit delay in milliseconds
#[arg(long, default_value = "200")]
rate_limit: u64,
/// Output format: json or markdown
#[arg(long, default_value = "json")]
format: String,
/// Proxy URL
#[arg(long)]
proxy: Option<String>,
/// Custom user agent
#[arg(long)]
user_agent: Option<String>,
/// Request timeout in milliseconds
#[arg(long, default_value = "30000")]
timeout: u64,
/// Respect robots.txt
#[arg(long)]
respect_robots_txt: bool,
/// Stay on the same domain
#[arg(long)]
stay_on_domain: bool,
},
/// Discover all URLs on a website via sitemaps and link extraction
Map {
/// URL to map
url: String,
/// Maximum URLs to return
#[arg(long)]
limit: Option<usize>,
/// Filter URLs by substring
#[arg(long)]
search: Option<String>,
/// Respect robots.txt
#[arg(long)]
respect_robots_txt: bool,
},
/// Start the REST API server
#[cfg(feature = "api")]
Serve {
/// Host address to bind to
#[arg(long, default_value = "0.0.0.0")]
host: String,
/// Port to listen on
#[arg(long, default_value = "3000")]
port: u16,
},
/// Start the MCP server (stdio transport)
#[cfg(feature = "mcp")]
Mcp {},
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
match cli.command {
Commands::Scrape {
url,
format,
proxy,
user_agent,
timeout,
respect_robots_txt,
} => {
let config = CrawlConfig {
user_agent,
request_timeout: Duration::from_millis(timeout),
respect_robots_txt,
proxy: proxy.map(|url| ProxyConfig {
url,
username: None,
password: None,
}),
..Default::default()
};
let engine = CrawlEngine::builder().config(config).build().unwrap();
match engine.scrape(&url).await {
Ok(result) => {
if format == "markdown" {
if let Some(ref md) = result.markdown {
println!("{}", md.content);
} else {
eprintln!("No markdown content available");
}
} else {
println!("{}", serde_json::to_string_pretty(&result).unwrap());
}
}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
Commands::Crawl {
urls,
depth,
max_pages,
concurrent,
rate_limit,
format,
proxy,
user_agent,
timeout,
respect_robots_txt,
stay_on_domain,
} => {
let config = CrawlConfig {
max_depth: Some(depth),
max_pages,
max_concurrent: Some(concurrent),
user_agent,
request_timeout: Duration::from_millis(timeout),
respect_robots_txt,
stay_on_domain,
proxy: proxy.map(|url| ProxyConfig {
url,
username: None,
password: None,
}),
..Default::default()
};
let engine = CrawlEngine::builder()
.config(config)
.rate_limiter(PerDomainThrottle::new(Duration::from_millis(rate_limit)))
.build()
.unwrap();
if urls.len() == 1 {
match engine.crawl(&urls[0]).await {
Ok(result) => {
if format == "markdown" {
for page in &result.pages {
if let Some(ref md) = page.markdown {
println!("---\nURL: {}\n---\n{}\n", page.url, md.content);
}
}
} else {
println!("{}", serde_json::to_string_pretty(&result).unwrap());
}
}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
} else {
let url_refs: Vec<&str> = urls.iter().map(|s| s.as_str()).collect();
let results = engine.batch_crawl(&url_refs).await;
if format == "markdown" {
for (seed_url, result) in &results {
match result {
Ok(r) => {
for page in &r.pages {
if let Some(ref md) = page.markdown {
println!("---\nSeed: {seed_url}\nURL: {}\n---\n{}\n", page.url, md.content);
}
}
}
Err(e) => eprintln!("Error crawling {seed_url}: {e}"),
}
}
} else {
println!(
"{}",
serde_json::to_string_pretty(
&results
.iter()
.map(|(url, r)| {
serde_json::json!({
"seed_url": url,
"result": match r {
Ok(r) => serde_json::to_value(r).unwrap_or_default(),
Err(e) => serde_json::json!({"error": e.to_string()}),
}
})
})
.collect::<Vec<_>>()
)
.unwrap()
);
}
}
}
Commands::Map {
url,
limit,
search,
respect_robots_txt,
} => {
let config = CrawlConfig {
respect_robots_txt,
map_limit: limit,
map_search: search,
..Default::default()
};
let engine = CrawlEngine::builder().config(config).build().unwrap();
match engine.map(&url).await {
Ok(result) => {
for url_entry in &result.urls {
println!("{}", url_entry.url);
}
}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
#[cfg(feature = "api")]
Commands::Serve { host, port } => {
let engine = CrawlEngine::builder().build().unwrap();
eprintln!("Starting REST API server on {host}:{port}");
if let Err(e) = kreuzcrawl::api::serve(&host, port, Arc::new(engine)).await {
eprintln!("Server error: {e}");
std::process::exit(1);
}
}
#[cfg(feature = "mcp")]
Commands::Mcp {} => {
eprintln!("Starting MCP server (stdio transport)");
if let Err(e) = kreuzcrawl::mcp::start_mcp_server().await {
eprintln!("MCP server error: {e}");
std::process::exit(1);
}
}
}
}