-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.py
More file actions
executable file
·221 lines (192 loc) · 8.02 KB
/
Copy pathcapture.py
File metadata and controls
executable file
·221 lines (192 loc) · 8.02 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
from playwright.sync_api import sync_playwright, Browser
import time, uuid, json
from dataclasses import dataclass, field
from typing import List, Set
from pathlib import Path
from playwright._impl._errors import TargetClosedError
from video import FFmpegFullScreenRecorder
from save import LogPump, JsonlWriter
from config import Config
import platform
class Recorder:
def __init__(self, config: Config):
self.config = config
self.page_ids = {}
self.basename: str = str(uuid.uuid4())[:8]
self.ts = int(time.time())
self.pump = LogPump()
self.jsonl = JsonlWriter(self.config.jsonl_dir, f"{self.basename}_{self.ts}")
self.screen_rec = None
def set_window_state(
self, page, state="maximized"
): # "fullscreen" or "maximized" or "normal"
cdp = page.context.new_cdp_session(page)
wid = cdp.send("Browser.getWindowForTarget")["windowId"]
cdp.send(
"Browser.setWindowBounds",
{"windowId": wid, "bounds": {"windowState": state}},
)
page.bring_to_front()
def _emit_event(self, pid: str, obj: dict):
"""
Enrich and send the event to both stdout ([BROWSER_LOG] line) and JSONL.
"""
# Shallow-copy to avoid mutating original dict if reused
evt = dict(obj)
# Normalize timestamp
ts = evt.get("ts")
if isinstance(ts, (int, float)) and ts > 1e10: # detect ms epoch
evt["ts"] = round(ts / 1000.0, 6)
self.jsonl.put(evt)
# Also emit the compact JSON on stdout (the same structure)
payload = json.dumps(evt, ensure_ascii=False, separators=(",", ":"))
self.pump.put(f"[BROWSER_LOG] [{pid}] {payload}")
def _pid(self, page):
return self.page_ids.setdefault(page, str(uuid.uuid4())[:8])
def _wire(self, page):
pid = self.page_ids.setdefault(page, str(uuid.uuid4())[:8])
def _on_console(msg):
text = msg.text or ""
if not text or text[0] != "{":
if self.config.debug:
self.pump.put(
f"[BROWSER_{(msg.type or '').upper()}] [{pid}] {text}"
)
return
try:
obj = json.loads(text)
except Exception:
if self.config.debug:
self.pump.put(f"[BROWSER_PARSE_ERR] [{pid}] {text}")
return
if obj.get("__rec") == 1 and obj.get("type") in self.config.action_types:
obj.pop("__rec", None) # remove marker
# (optional) redact long input values
if obj.get("type") in {"type", "type_commit"} and isinstance(
obj.get("value"), str
):
v = obj["value"]
obj["value"] = v if len(v) <= 120 else (v[:117] + "…")
self._emit_event(pid, obj)
elif self.config.debug:
self.pump.put(f"[BROWSER_OTHER] [{pid}] {text}")
def _on_popup(p):
self._wire(p)
page.on("console", _on_console)
page.on("popup", _on_popup)
def start(self):
self.pump.start()
self.jsonl.start()
try:
with sync_playwright() as p:
browser = p.chromium.launch(
headless=self.config.headless, args=self.config.chromium_args
)
new_ctx_kwargs = {
"viewport": None,
"no_viewport": True,
"bypass_csp": self.config.bypass_csp,
}
state_path = self.config.storage_state_path
if state_path and Path(state_path).exists():
new_ctx_kwargs["storage_state"] = state_path
self.pump.put(f"[RECORDER] Loaded storage_state from {state_path}")
context = browser.new_context(**new_ctx_kwargs)
context.add_init_script(self.config.init_script)
for pg in context.pages:
self._wire(pg)
context.on("page", lambda pg: self._wire(pg))
page = context.new_page()
self.set_window_state(page)
# self._wire(page)
page.goto(self.config.start_url, wait_until="domcontentloaded")
# --- Start video as soon as we can get stable bounds ---
try:
self.screen_rec = FFmpegFullScreenRecorder(
out_dir=self.config.video_dir,
filename=f"{self.basename}_{self.ts}",
codec_preference="hevc",
fps=self.config.fps,
)
ffmpeg_start = time.time()
self.screen_rec.start()
self.pump.put(
f"[RECORDER] Full-screen recording started → {self.screen_rec.out_path}"
)
except Exception as e:
self.pump.put(f"[RECORDER] Failed to start screen recording: {e}")
while browser.is_connected():
pgs = context.pages
if pgs:
# this is a Playwright call; it pumps events without doing anything visible
try:
pgs[0].wait_for_timeout(100) # ~100 ms
except TargetClosedError:
if state_path:
try:
Path(state_path).parent.mkdir(
parents=True, exist_ok=True
)
context.storage_state(path=state_path)
self.pump.put(
f"[RECORDER] Saved storage_state → {state_path}"
)
except Exception as e:
self.pump.put(
f"[RECORDER] Failed to save storage_state: {e}"
)
break # exit when browser is closed
else:
time.sleep(0.1)
finally:
if self.screen_rec:
try:
outp = self.screen_rec.stop()
self.pump.put(
f"[RECORDER] Video started at {ffmpeg_start} saved: {outp}"
)
except Exception as e:
self.pump.put(f"[RECORDER] Video stop failed: {e}")
# Ensure pump stops
self.pump.put(f"[RECORDER] Stopped.")
self.pump.stop()
self.jsonl.stop()
if __name__ == "__main__":
start_url = "https://www.amazon.com/"
action_types = {
"action_types",
"click",
"scroll_start",
"scroll_end",
"type",
"type_commit",
"tab_hidden",
"navigation",
"recorder_init",
}
system = platform.system()
if system == "Windows":
print("Running on Windows")
config = Config(
headless=False,
bypass_csp=True,
start_url=start_url,
storage_state_path="state/amazon_state.json",
# action_types=action_types,
debug=False,
)
elif system == "Darwin":
print("Running on macOS")
config = Config(
headless=False,
bypass_csp=True,
fps=60,
start_url=start_url,
# action_types=action_types,
storage_state_path="state/amazon_state.json",
debug=False,
)
else:
print(f"Unsupported platform: {system}")
exit(1)
Recorder(config).start()