-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawl.py
More file actions
40 lines (30 loc) · 1.04 KB
/
crawl.py
File metadata and controls
40 lines (30 loc) · 1.04 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
import dataclasses
from .client import Client
from .types import Config, CrawlConfig
from .robot import Robot
def _to_camel(snake: str) -> str:
parts = snake.split("_")
return parts[0] + "".join(p.title() for p in parts[1:])
def _dataclass_to_dict(obj) -> dict:
"""Convert a dataclass to a camelCase dict, dropping None values."""
return {
_to_camel(k): v
for k, v in dataclasses.asdict(obj).items()
if v is not None
}
class Crawl:
def __init__(self, config: Config):
self.client = Client(config)
async def create(self, name: str, url: str, crawl_config: CrawlConfig) -> Robot:
if not url:
raise ValueError("URL is required")
if not crawl_config:
raise ValueError("Crawl configuration is required")
robot_data = await self.client.create_crawl_robot(
url,
{
"name": name,
"crawlConfig": _dataclass_to_dict(crawl_config),
},
)
return Robot(self.client, robot_data)