Commit 846476b2 authored by nextime's avatar nextime

Update for websockets

parent 5779325d
import platform
import asyncio import asyncio
from playwright.async_api import async_playwright from playwright.async_api import async_playwright
import argparse import argparse
import os import os
from datetime import datetime from datetime import datetime
from pathlib import Path
async def monitor_streamate(log_path): async def monitor_stream(log_path, domain):
async with async_playwright() as p: async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp("http://localhost:9222") browser = await p.chromium.connect_over_cdp("http://localhost:9222")
while True: while True:
contexts = browser.contexts contexts = browser.contexts
for context in contexts: for context in contexts:
for page in context.pages: for page in context.pages:
if "performerclient.streamatemodels.com" in page.url: if domain in page.url:
print(f"Found Streamate tab") print(f"Found Streamate tab")
await monitor_requests(page, log_path) await monitor_requests(page, log_path)
...@@ -39,20 +39,40 @@ async def monitor_requests(page, log_path): ...@@ -39,20 +39,40 @@ async def monitor_requests(page, log_path):
f.write(str(log_body)) f.write(str(log_body))
f.write("\n\n") f.write("\n\n")
async def log_websocket(ws):
def writelog(payload, origin, url):
print(origin+": "+str(payload)+"\n")
log_entry = f"[{datetime.now()}] WS: {origin} {url} -> Message: "
with open(log_path, 'a') as f:
f.write(log_entry)
f.write(origin+": ")
f.write(str(payload)+"\n\n")
print(f"WebSocket opened: {ws.url}")
ws.on("framesent", lambda payload: writelog(payload, 'framesent', ws.url))
ws.on("framereceived", lambda payload: writelog( payload, 'framereceived', ws.url))
ws.on("close", lambda payload: print("WebSocket closed"))
page.on("request", log_request) page.on("request", log_request)
page.on("response", log_response) page.on("response", log_response)
page.on("websocket", log_websocket)
while True: while True:
await asyncio.sleep(1) await asyncio.sleep(1)
if __name__ == "__main__": if __name__ == "__main__":
lpath=str(Path(Path.home(), Path('streamon.log')))
parser = argparse.ArgumentParser(description="Monitor Streamate requests") parser = argparse.ArgumentParser(description="Monitor Streamate requests")
parser.add_argument("--log_path", type=str, default="streamate_requests.log", parser.add_argument("--log_path", type=str, default=lpath,
help="Path to the log file") help="Path to the log file")
parser.add_argument("--domain", type=str, default="performerclient.streamatemodels.com",
help="URL to monitor")
args = parser.parse_args() args = parser.parse_args()
log_dir = os.path.dirname(args.log_path) log_dir = os.path.dirname(args.log_path)
if log_dir and not os.path.exists(log_dir): if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir) os.makedirs(log_dir)
asyncio.run(monitor_streamate(args.log_path)) asyncio.run(monitor_stream(args.log_path, args.domain))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment