Remove dead code, split long functions (tilt search, wipe, sync search, blob mask/ellipse, scan attempt); center split title
This commit is contained in:
+1
-71
@@ -1,13 +1,10 @@
|
||||
"""tiny chrome devtools protocol client: headless chrome, pages, eval, screencast"""
|
||||
"""tiny chrome devtools protocol client for the ring bench: headless chrome, pages, eval"""
|
||||
import asyncio
|
||||
import base64
|
||||
import itertools
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
import websockets
|
||||
@@ -96,7 +93,6 @@ class Page:
|
||||
def __init__(self, browser, sid, w, h, dsf):
|
||||
self.b, self.sid, self.w, self.h, self.dsf = browser, sid, w, h, dsf
|
||||
self.load = None
|
||||
self.frames = None
|
||||
|
||||
def send(self, method, params=None):
|
||||
return self.b.send(method, params, self.sid)
|
||||
@@ -104,15 +100,6 @@ class Page:
|
||||
def _event(self, method, params):
|
||||
if method == "Page.loadEventFired" and self.load and not self.load.done():
|
||||
self.load.set_result(True)
|
||||
elif method == "Page.screencastFrame":
|
||||
if self.frames is not None:
|
||||
ts = params["metadata"].get("timestamp", time.time())
|
||||
path = os.path.join(self.frame_dir, f"{len(self.frames):06d}.jpg")
|
||||
with open(path, "wb") as f:
|
||||
f.write(base64.b64decode(params["data"]))
|
||||
self.frames.append((ts, path))
|
||||
asyncio.get_running_loop().create_task(
|
||||
self.send("Page.screencastFrameAck", {"sessionId": params["sessionId"]}))
|
||||
|
||||
async def goto(self, url):
|
||||
self.load = asyncio.get_running_loop().create_future()
|
||||
@@ -124,60 +111,3 @@ class Page:
|
||||
if "exceptionDetails" in r:
|
||||
raise RuntimeError(r["exceptionDetails"].get("exception", {}).get("description", r["exceptionDetails"]))
|
||||
return r.get("result", {}).get("value")
|
||||
|
||||
async def on_new_document(self, source):
|
||||
await self.send("Page.addScriptToEvaluateOnNewDocument", {"source": source})
|
||||
|
||||
async def record(self, frame_dir):
|
||||
os.makedirs(frame_dir, exist_ok=True)
|
||||
self.frame_dir = frame_dir
|
||||
self.frames = []
|
||||
await self.send("Page.startScreencast", {"format": "jpeg", "quality": 90,
|
||||
"maxWidth": int(self.w * self.dsf), "maxHeight": int(self.h * self.dsf)})
|
||||
|
||||
async def stop(self):
|
||||
if getattr(self, "shooter", None):
|
||||
self.shooting = False
|
||||
try:
|
||||
await asyncio.wait_for(self.shooter, 3)
|
||||
except Exception:
|
||||
self.shooter.cancel()
|
||||
else:
|
||||
try:
|
||||
await asyncio.wait_for(self.send("Page.stopScreencast"), 3)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
async def record_shots(self, frame_dir, fps=24):
|
||||
"""screenshot polling: slower than screencast but honours deviceScaleFactor"""
|
||||
os.makedirs(frame_dir, exist_ok=True)
|
||||
self.frames, self.shooting = [], True
|
||||
|
||||
async def loop():
|
||||
while self.shooting:
|
||||
t = time.time()
|
||||
r = await self.send("Page.captureScreenshot", {"format": "jpeg", "quality": 90})
|
||||
path = os.path.join(frame_dir, f"{len(self.frames):06d}.jpg")
|
||||
with open(path, "wb") as f:
|
||||
f.write(base64.b64decode(r["data"]))
|
||||
self.frames.append((t, path))
|
||||
await asyncio.sleep(max(0, 1 / fps - (time.time() - t)))
|
||||
self.shooter = asyncio.get_running_loop().create_task(loop())
|
||||
|
||||
async def png(self, path, transparent=False):
|
||||
if transparent:
|
||||
await self.send("Emulation.setDefaultBackgroundColorOverride", {"color": {"r": 0, "g": 0, "b": 0, "a": 0}})
|
||||
r = await self.send("Page.captureScreenshot", {"format": "png"})
|
||||
with open(path, "wb") as f:
|
||||
f.write(base64.b64decode(r["data"]))
|
||||
|
||||
async def mouse(self, kind, x, y, button="left", clicks=1):
|
||||
await self.send("Input.dispatchMouseEvent",
|
||||
{"type": kind, "x": x, "y": y, "button": button, "clickCount": clicks})
|
||||
|
||||
async def type(self, text, rng, target="document.activeElement"):
|
||||
# write into the field directly: focus can drift between headless targets while screenshotting
|
||||
await self.js(f"window.__tt = {target}; __tt.focus()")
|
||||
for ch in text:
|
||||
await self.js("__tt.value += " + json.dumps(ch) + "; __tt.dispatchEvent(new Event('input', {bubbles: true}))")
|
||||
await asyncio.sleep(rng.uniform(0.05, 0.14) + (0.18 if ch == " " and rng.random() < 0.2 else 0))
|
||||
|
||||
Reference in New Issue
Block a user