onemillioncheckboxes/stream.py

37 lines
1001 B
Python
Raw Permalink Normal View History

from base64 import b64decode
from socketio import SimpleClient
from requests import get
from sys import stdout
BASE_URL = "https://onemillioncheckboxes.com"
canvas = bytearray(1000000)
with get(f"{BASE_URL}/api/initial-state") as req:
data = req.json()
buffer = b64decode(data["full_state"].encode() + b"=")
canvas[:] = buffer
with SimpleClient() as sio:
sio.connect(f"{BASE_URL}/socket.io")
while True:
name, data = sio.receive()
if name == "batched_bit_toggles":
bits_on, bits_off, timestamp = data
for ndx in bits_on:
byte, bit = divmod(ndx, 8)
canvas[byte] |= (1 << bit)
for ndx in bits_off:
byte, bit = divmod(ndx, 8)
canvas[byte] &= 0xFF ^ (1 << bit)
elif name == "full_state":
buffer = b64decode(data["full_state"].encode() + b"=")
canvas[:] = buffer
stdout.buffer.write(canvas)
stdout.buffer.flush()