-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscrape.py
More file actions
40 lines (33 loc) · 1.08 KB
/
scrape.py
File metadata and controls
40 lines (33 loc) · 1.08 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 time
import random
import string
from typing import List, Optional
from .client import Client
from .types import Config, WorkflowFile, Format
from .robot import Robot
class Scrape:
def __init__(self, config: Config):
self.client = Client(config)
async def create(
self,
name: str,
url: str,
formats: Optional[List[Format]] = None,
) -> Robot:
if not url:
raise ValueError("URL is required")
robot_id = f"robot_{int(time.time() * 1000)}_{self._random_string()}"
workflow_file: WorkflowFile = {
"meta": {
"name": name,
"id": robot_id,
"robotType": "scrape",
"url": url,
"formats": formats or ["markdown"],
},
"workflow": [],
}
robot_data = await self.client.create_robot(workflow_file)
return Robot(self.client, robot_data)
def _random_string(self, length: int = 9) -> str:
return "".join(random.choices(string.ascii_lowercase + string.digits, k=length))