-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_search.py
More file actions
74 lines (57 loc) · 2.13 KB
/
basic_search.py
File metadata and controls
74 lines (57 loc) · 2.13 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
"""
Search Example
This example demonstrates:
- Using the Search SDK to search the web and scrape results
- Configuring search mode (discover vs scrape)
- Setting search filters (time range, region)
- Limiting search results
Provider: DuckDuckGo
"""
import asyncio
import os
import sys
from dotenv import load_dotenv
from maxun import Search, Config, SearchConfig
async def main():
searcher = Search(Config(
api_key=os.environ["MAXUN_API_KEY"],
base_url=os.environ.get("MAXUN_BASE_URL", "https://app.maxun.dev/api/sdk/"),
))
robot = await searcher.create(
"Tech News Search",
SearchConfig(
query="latest AI developments 2025",
mode="discover",
filters={"timeRange": "week"},
limit=10,
),
)
print(f"Search robot created: {robot.id}")
result = await robot.run()
search_data = (result.get("data") or {}).get("searchData")
if isinstance(search_data, dict):
all_results = []
for value in search_data.values():
if isinstance(value, list):
all_results.extend(value)
elif isinstance(value, dict) and isinstance(value.get("results"), list):
all_results.extend(value["results"])
print(f"Search results found: {len(all_results)}")
print("\nSearch Results:")
for i, item in enumerate(all_results, 1):
print(f"\n {i}. {item.get('title') or 'No title'}")
print(f" URL: {item.get('url') or 'No URL'}")
description = item.get("description")
if description:
print(f" Description: {description[:150]}...")
elif isinstance(search_data, list):
print(f"Search results found: {len(search_data)}")
print("\nSearch Results:")
for i, item in enumerate(search_data, 1):
print(f"\n {i}. {item.get('title') or 'No title'}")
print(f" URL: {item.get('url') or 'No URL'}")
load_dotenv()
if not os.environ.get("MAXUN_API_KEY"):
print("Error: MAXUN_API_KEY environment variable is required", file=sys.stderr)
sys.exit(1)
asyncio.run(main())