guest-tools/osmike-surf

46 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""osmike-surf: a real browser for every agent (the same for every user; your person's own browser profile, so their
logins stay theirs). Runs on the OsMike browser service through the bus with your agent key; no machine access.
osmike-surf open URL osmike-surf text [N] osmike-surf links
osmike-surf click "text|css" osmike-surf clickat X Y osmike-surf type "field|css" "text"
osmike-surf key Enter osmike-surf scroll [px] osmike-surf back | url | check
osmike-surf eval "js" osmike-surf shot [file.png] (saves a screenshot here)
"""
import base64, json, os, sys, urllib.error, urllib.request
def main(a):
if not a or a[0] in ("-h", "--help", "help"):
print(__doc__); return
api, key = os.environ.get("A2A_API", "").rstrip("/"), os.environ.get("A2A_AGENT_KEY", "")
if not api or not key:
sys.exit("A2A_API and A2A_AGENT_KEY must be set (they are in every agent).")
cmd, args = a[0], a[1:]
save = None
if cmd == "shot":
save, args = (args[0] if args else "surf-shot.png"), []
req = urllib.request.Request(api + "/v1/runs/surf", json.dumps({"cmd": cmd, "args": args}).encode(),
{"X-API-KEY": key, "Content-Type": "application/json"})
try:
d = json.load(urllib.request.urlopen(req, timeout=200))
except urllib.error.HTTPError as e:
try:
msg = json.load(e).get("detail")
except Exception:
msg = e.reason
sys.exit("osmike-surf: %s (HTTP %s)" % (msg, e.code))
except urllib.error.URLError as e:
sys.exit("osmike-surf: could not reach OsMike (%s)" % e.reason)
if save and d.get("png_base64"):
open(save, "wb").write(base64.b64decode(d["png_base64"]))
print(os.path.abspath(save))
elif d.get("output"):
print(d["output"].rstrip("\n"))
if not d.get("ok"):
sys.exit(d.get("error") or "osmike-surf: the browser command failed")
if __name__ == "__main__":
main(sys.argv[1:])