-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path__init__.py
More file actions
399 lines (350 loc) · 16.1 KB
/
__init__.py
File metadata and controls
399 lines (350 loc) · 16.1 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import json
import re
import sys
import textwrap
from email.utils import parsedate_to_datetime
import click
from rich.console import Console
from rich.live import Live
from rich.markdown import Markdown
from rich.progress import Progress, SpinnerColumn, TextColumn
from datetime import datetime
from diffbot import CrawlEventType, AuthError, ExtractionError, APIError
from ._common import get_client
console = Console()
is_interactive = sys.stdout.isatty()
def print_markdown(text):
if is_interactive:
console.print(Markdown(text))
else:
sys.stdout.write(text)
sys.stdout.write("\n")
@click.group()
def main():
"""
Diffbot 🤖 Structure the world's knowledge.
"""
pass
@main.command(name="extract")
@click.argument("url")
@click.option("-o", "--output", type=click.Path(), help="Write output to file instead of stdout")
@click.option("-f", "--format", "fmt", type=click.Choice(["markdown", "json"]), default="markdown", help="Output format")
@click.option("-a", "--api", type=str, default="analyze", help="Diffbot API to use")
def extract(url, output, fmt, api):
"""Get structured content from a URL."""
db = get_client()
try:
if is_interactive and not output:
with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True) as progress:
progress.add_task(description="Extracting content...", total=None)
data = db.extract(url, api=api, fmt=fmt)
else:
data = db.extract(url, api=api, fmt=fmt)
if fmt == "json":
output_str = json.dumps(data, indent=2)
else:
objects = data.get("objects", [])
if not objects:
output_str = json.dumps(data, indent=2)
else:
obj = objects[0]
title = obj.get("title", "")
url = obj.get("pageUrl", "")
content = obj.get("content", "")
output_str = f"Title: {title}\n\nURL: {url}\n\nContent: {content}"
if output:
with open(output, "w") as f:
f.write(output_str)
click.echo(f"Output written to {output}")
else:
print_markdown(output_str)
except ExtractionError as e:
click.echo(f"Extraction error {e.error_code}: {e.error}", err=True)
raise click.Abort()
except AuthError:
click.echo("Error: Invalid or unauthorized API token.", err=True)
raise click.Abort()
except APIError as e:
click.echo(f"API error {e.status_code}: {e.message or e.body}", err=True)
raise click.Abort()
@main.command()
@click.argument("prompt")
@click.option("-o", "--output", type=click.Path(), help="Write output to file instead of stdout")
@click.option("--json", "as_json", is_flag=True, help="Output from LLM as JSON")
def ask(prompt: str, output: str = None, as_json: bool = False):
"""Ask a question to the Diffbot LLM"""
db = get_client()
stdin_content = sys.stdin.read() if not sys.stdin.isatty() else None
interactive_mode = is_interactive and not output and not as_json
messages = [
{
"role": "system",
"content": f"Current local time: {datetime.now().strftime('%A, %B %d, %Y, %I:%M:%S %p %Z')}",
},
{"role": "user", "content": prompt},
]
if as_json:
messages[1]["content"] += "\nReturn the output as a JSON object. Do not include any other text outside of the JSON object."
if stdin_content:
messages[1]["content"] = f"<input>{stdin_content}</input>\n" + messages[1]["content"]
try:
if interactive_mode:
response_text = ""
with Live("", auto_refresh=False, console=console) as live:
for chunk in db.ask(messages):
response_text += chunk
live.update(Markdown(response_text))
live.refresh()
messages.append({"role": "assistant", "content": response_text})
while True:
try:
sys.stdout.write("\n> ")
sys.stdout.flush()
user_input = input()
messages.append({"role": "user", "content": user_input})
except (KeyboardInterrupt, EOFError):
console.print("\nExiting chat...")
return
response_text = ""
with Live("", auto_refresh=False, console=console) as live:
for chunk in db.ask(messages):
response_text += chunk
live.update(Markdown(response_text))
live.refresh()
messages.append({"role": "assistant", "content": response_text})
else:
response = ""
if output:
for chunk in db.ask(messages):
response += chunk
with open(output, "w") as f:
f.write(response)
click.echo(f"Output written to {output}")
elif as_json:
buffer = ""
for chunk in db.ask(messages):
buffer += chunk
buffer = buffer[buffer.find("{") : buffer.rfind("}") + 1]
sys.stdout.write(buffer)
else:
for chunk in db.ask(messages):
sys.stdout.write(chunk)
except (KeyboardInterrupt, EOFError):
console.print("\nExiting chat...")
except AuthError:
click.echo("Error: Invalid or unauthorized API token.", err=True)
raise click.Abort()
except APIError as e:
click.echo(f"API error {e.status_code}: {e.message or e.body}", err=True)
raise click.Abort()
@main.command()
@click.argument("site")
@click.option("--hops", type=int, default=2, help="Maximum link depth from seed URLs")
@click.option("--job-name", type=str, help="Name for the crawler job (generated if not provided)")
@click.option("--max-to-crawl", type=int, default=100, help="Maximum number of pages to crawl")
@click.option("--max-to-process", type=int, default=100, help="Maximum number of pages to process")
@click.option("--restrict-domain", is_flag=True, default=True, help="Restrict crawling to the same domain as seeds")
@click.option("--api-url", type=str, default="", help="Diffbot API endpoint to use for processing")
@click.option("--crawl-delay", type=float, default=-1, help="Delay between requests to the same domain in seconds")
@click.option("--url-crawl-pattern", type=str, help="Only crawl URLs matching this pattern")
@click.option("--url-process-pattern", type=str, help="Only process URLs matching this pattern")
@click.option("--obey-robots", is_flag=True, help="Obey robots.txt rules")
@click.option("--use-proxies", is_flag=True, help="Use proxies for crawling")
@click.option("--custom-headers", type=str, help="Custom HTTP headers to send with requests (newline separated)")
@click.option("-o", "--output", type=click.Path(), help="Write output to file instead of stdout")
@click.option("-f", "--format", "fmt", type=click.Choice(["markdown", "json"]), default="markdown", help="Output format")
def crawl(site, hops, job_name, max_to_crawl, max_to_process, restrict_domain,
api_url, crawl_delay, url_crawl_pattern, url_process_pattern, obey_robots,
use_proxies, custom_headers, output, fmt):
"""Crawl a website using the Diffbot Crawler API."""
db = get_client()
events = []
try:
with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True) as progress:
progress.add_task(description=f"Crawling {site}...", total=None)
for event in db.crawl(
site,
hops=hops,
job_name=job_name,
max_to_crawl=max_to_crawl,
max_to_process=max_to_process,
restrict_domain=restrict_domain,
api_url=api_url,
crawl_delay=crawl_delay,
url_crawl_pattern=url_crawl_pattern,
url_process_pattern=url_process_pattern,
obey_robots=obey_robots,
use_proxies=use_proxies,
custom_headers=custom_headers,
watch=True,
):
events.append(event)
if event.event_type == CrawlEventType.JOB_CREATED:
progress.console.print(f"Job created: {event.details['job_name']}")
elif event.event_type == CrawlEventType.URL_PROCESSED:
status = event.details.get("status", "")
icon = "✓" if status == "Success" else "!"
progress.console.print(f" [{icon}] {event.details['url']}")
if fmt == "json":
output_str = json.dumps(
[{"event_type": e.event_type.value, "timestamp": e.timestamp, "details": e.details} for e in events],
indent=2,
)
else:
lines = []
for event in events:
if event.event_type == CrawlEventType.JOB_CREATED:
lines.append(f"# Job: {event.details['job_name']}\n")
elif event.event_type == CrawlEventType.URL_PROCESSED:
lines.append(f"- [{event.details.get('status', 'unknown')}] {event.details['url']}")
output_str = "\n".join(lines)
if output:
with open(output, "w") as f:
f.write(output_str)
click.echo(f"Output written to {output}")
else:
print_markdown(output_str)
except AuthError:
click.echo("Error: Invalid or unauthorized API token.", err=True)
raise click.Abort()
except APIError as e:
click.echo(f"API error {e.status_code}: {e.message or e.body}", err=True)
raise click.Abort()
@main.command()
@click.argument("job_name", required=False)
def crawl_list_jobs(job_name):
"""List Diffbot crawler jobs or get details for a specific job."""
db = get_client()
try:
if job_name:
job = db.crawl_get_job(job_name)
print_markdown(f"# Job: {job_name}\n\n```json\n{json.dumps(job, indent=2)}\n```")
else:
jobs = db.crawl_list_jobs()
if not jobs:
print_markdown("No crawler jobs found.")
return
lines = ["# Diffbot Crawler Jobs\n"]
for job in jobs:
name = job.get("name", "Unknown")
job_type = job.get("type", "Unknown")
status = job.get("jobStatus", {}).get("message", "Unknown")
lines.append(f"## {name}")
lines.append(f"Type: {job_type}")
lines.append(f"Status: {status}")
if "pageCrawlSuccesses" in job:
lines.append(f"Pages Crawled: {job['pageCrawlSuccesses']}")
if "pageProcessSuccesses" in job:
lines.append(f"Pages Processed: {job['pageProcessSuccesses']}")
lines.append("\n---\n")
print_markdown("\n".join(lines))
except AuthError:
click.echo("Error: Invalid or unauthorized API token.", err=True)
raise click.Abort()
except APIError as e:
click.echo(f"API error {e.status_code}: {e.message or e.body}", err=True)
raise click.Abort()
@main.command()
@click.argument("job_name")
def crawl_delete_job(job_name):
"""Delete a Diffbot crawler job."""
db = get_client()
try:
with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True) as progress:
progress.add_task(description=f"Deleting job {job_name}...", total=None)
db.crawl_delete_job(job_name)
progress.console.print(f"Job {job_name} deleted.")
except AuthError:
click.echo("Error: Invalid or unauthorized API token.", err=True)
raise click.Abort()
except APIError as e:
click.echo(f"API error {e.status_code}: {e.message or e.body}", err=True)
raise click.Abort()
def _format_date(date_str: str) -> str:
if not date_str:
return ""
try:
dt = parsedate_to_datetime(date_str)
return f"{dt.strftime('%b')} {dt.day}, {dt.year}"
except Exception:
return date_str[:12].strip()
def _score_color(score: float) -> str:
if score > 0.85:
return "bright_green"
elif score >= 0.7:
return "green"
elif score >= 0.5:
return "yellow"
return "red"
def _strip_markdown(text: str) -> str:
text = re.sub(r'!\[.*?\]\(.*?\)', '', text) # images
text = re.sub(r'\[([^\]]*)\]\([^)]*\)', r'\1', text) # links → label
text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE) # headings
text = re.sub(r'^\s*[-*+]\s+', '', text, flags=re.MULTILINE) # bullets
text = re.sub(r'^\s*\d+\.\s+', '', text, flags=re.MULTILINE) # numbered lists
text = re.sub(r'[`*_~]{1,3}', '', text) # inline code/bold/italic
text = re.sub(r'\s+', ' ', text)
return text.strip()
@main.command(name="web-search")
@click.argument("query")
@click.option("-n", "--num-results", type=int, default=None, help="Number of results to return")
@click.option("-m", "--max-tokens", type=int, default=None, help="Limit total response tokens (for agentic use cases)")
@click.option("-f", "--format", "fmt", type=click.Choice(["list", "json", "text"]), default="list", help="Output format (text is plain/agent-friendly)")
def web_search(query: str, num_results, max_tokens, fmt: str):
"""Search the web using the Diffbot LLM web search API."""
db = get_client()
try:
if is_interactive:
with Progress(SpinnerColumn(), TextColumn("[progress.description]{task.description}"), transient=True) as progress:
progress.add_task(description="Searching...", total=None)
data = db.web_search(query, num_results=num_results, max_tokens=max_tokens)
else:
data = db.web_search(query, num_results=num_results, max_tokens=max_tokens)
if fmt == "json":
sys.stdout.write(json.dumps(data, indent=2))
sys.stdout.write("\n")
return
results = data.get("search_results", [])
if fmt == "text":
for i, r in enumerate(results, 1):
date_fmt = _format_date(r.get("date", ""))
content = " ".join(r.get("content", "").split())
lines = [
f"[{i}] {r.get('title', '')} (score: {r.get('score', 0):.3f})",
f"URL: {r.get('pageUrl', '')}",
]
if date_fmt:
lines.append(f"Date: {date_fmt}")
lines += [f"Content: {content}", "---"]
sys.stdout.write("\n".join(lines) + "\n")
return
time_ms = data.get("timeMs", 0)
for i, r in enumerate(results, 1):
score = r.get("score", 0)
title = r.get("title", "")
url = r.get("pageUrl", "")
date = r.get("date", "")
content = _strip_markdown(r.get("content", ""))
date_fmt = _format_date(date)
color = _score_color(score)
prefix_len = len(date_fmt) + 3 if date_fmt else 0 # " — "
snippet_width = max(80, console.width * 2 - prefix_len)
snippet = textwrap.shorten(content, width=snippet_width, placeholder="...")
console.print(f"[bold]#{i}: {title}[/bold] [[{color}]{score:.3f}[/{color}]]")
console.print(f"[dim blue]{url}[/dim blue]")
if date_fmt:
console.print(f"[dim]{date_fmt}[/dim] — {snippet}")
else:
console.print(snippet)
console.print()
console.print(f"[dim]{len(results)} result(s) in {time_ms}ms[/dim]")
except AuthError:
click.echo("Error: Invalid or unauthorized API token.", err=True)
raise click.Abort()
except APIError as e:
click.echo(f"API error {e.status_code}: {e.message or e.body}", err=True)
raise click.Abort()
from .dql import dql as _dql_group # noqa: E402
from .entities import entities as _entities_cmd # noqa: E402
main.add_command(_dql_group)
main.add_command(_entities_cmd)