-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__browser_data_async_template.py
More file actions
148 lines (121 loc) · 4.95 KB
/
__browser_data_async_template.py
File metadata and controls
148 lines (121 loc) · 4.95 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
"""浏览器沙箱数据API模板 / Browser Sandbox Data API Template
此模板用于生成浏览器沙箱数据API代码。
This template is used to generate browser sandbox data API code.
"""
from typing import Optional
from urllib.parse import parse_qs, urlencode, urlparse
from agentrun.utils.config import Config
from .sandbox_data import SandboxDataAPI
class BrowserDataAPI(SandboxDataAPI):
def __init__(
self,
sandbox_id: str,
config: Optional[Config] = None,
):
self.sandbox_id = sandbox_id
super().__init__(
sandbox_id=sandbox_id,
config=config,
)
def get_cdp_url(self, record: Optional[bool] = False):
"""
Generate the WebSocket URL for Chrome DevTools Protocol (CDP) connection.
This method constructs a WebSocket URL by:
1. Converting the HTTP endpoint to WebSocket protocol (ws://)
2. Parsing the existing URL and query parameters
3. Adding the session ID to the query parameters
4. Reconstructing the complete WebSocket URL
Returns:
str: The complete WebSocket URL for CDP automation connection,
including the session ID in the query parameters.
Example:
>>> api = BrowserDataAPI("browser123", "session456")
>>> api.get_cdp_url()
'ws://example.com/ws/automation?sessionId=session456'
"""
cdp_url = self.with_path("/ws/automation").replace("http", "ws")
u = urlparse(cdp_url)
query_dict = parse_qs(u.query)
query_dict["tenantId"] = [self.config.get_account_id()]
if record:
query_dict["recording"] = ["true"]
new_query = urlencode(query_dict, doseq=True)
new_u = u._replace(query=new_query)
return new_u.geturl()
def get_vnc_url(self, record: Optional[bool] = False):
"""
Generate the WebSocket URL for VNC (Virtual Network Computing) live view connection.
This method constructs a WebSocket URL for real-time browser viewing by:
1. Converting the HTTP endpoint to WebSocket protocol (ws://)
2. Parsing the existing URL and query parameters
3. Adding the session ID to the query parameters
4. Reconstructing the complete WebSocket URL
Returns:
str: The complete WebSocket URL for VNC live view connection,
including the session ID in the query parameters.
Example:
>>> api = BrowserDataAPI("browser123", "session456")
>>> api.get_vnc_url()
'ws://example.com/ws/liveview?sessionId=session456'
"""
vnc_url = self.with_path("/ws/liveview").replace("http", "ws")
u = urlparse(vnc_url)
query_dict = parse_qs(u.query)
query_dict["tenantId"] = [self.config.get_account_id()]
if record:
query_dict["recording"] = ["true"]
new_query = urlencode(query_dict, doseq=True)
new_u = u._replace(query=new_query)
return new_u.geturl()
def sync_playwright(
self,
browser_type: str = "chrome",
record: Optional[bool] = False,
config: Optional[Config] = None,
):
from .playwright_sync import BrowserPlaywrightSync
cfg = Config.with_configs(self.config, config)
url = self.get_cdp_url(record=record)
url, headers, _ = self.auth(
url=url, headers=cfg.get_headers(), config=cfg
)
return BrowserPlaywrightSync(
url,
browser_type=browser_type,
headers=headers,
)
def async_playwright(
self,
browser_type: str = "chrome",
record: Optional[bool] = False,
config: Optional[Config] = None,
):
from .playwright_async import BrowserPlaywrightAsync
cfg = Config.with_configs(self.config, config)
url = self.get_cdp_url(record=record)
url, headers, _ = self.auth(
url=url, headers=cfg.get_headers(), config=cfg
)
return BrowserPlaywrightAsync(
url,
browser_type=browser_type,
headers=headers,
)
async def list_recordings_async(self):
return await self.get_async("/recordings")
async def delete_recording_async(self, filename: str):
return await self.delete_async(f"/recordings/{filename}")
async def download_recording_async(self, filename: str, save_path: str):
"""
Asynchronously download a recording video file and save it to local path.
Args:
filename: The name of the recording file to download
save_path: Local file path to save the downloaded video file (.mkv)
Returns:
Dictionary with 'saved_path' and 'size' keys
Examples:
>>> await api.download_recording_async("recording.mp4", "/local/video.mkv")
"""
return await self.get_video_async(
f"/recordings/{filename}", save_path=save_path
)