import asyncio, json, sys, time
import websockets

async def main():
    uri="wss://ourworldoftext.com/ws/"
    async with websockets.connect(uri, origin="https://ourworldoftext.com",
            additional_headers={"User-Agent":"Mozilla/5.0 Chrome/120"}, max_size=None, open_timeout=20) as ws:
        # request a 16x16 tile block (256 tiles) in one rectangle
        block={"minX":0,"minY":0,"maxX":15,"maxY":15}
        await ws.send(json.dumps({"kind":"fetch","fetchRectangles":[block]}))
        got={}; fetch_msgs=0; t0=time.monotonic()
        while time.monotonic()-t0 < 10:
            try: msg=await asyncio.wait_for(ws.recv(), timeout=4)
            except asyncio.TimeoutError: break
            d=json.loads(msg)
            if d.get("kind")=="fetch":
                fetch_msgs+=1
                for k,v in d["tiles"].items():
                    got[k]= (v is not None)
        expected=16*16
        nonempty=sum(1 for v in got.values() if v)
        print(f"fetch messages: {fetch_msgs}")
        print(f"tiles returned: {len(got)} / expected {expected}")
        print(f"non-empty: {nonempty}")
        print("sample keys:", sorted(got.keys(), key=lambda s:tuple(map(int,s.split(','))))[:6])
asyncio.run(main())
