From 4978e660288eed558e2470147513225adeaf3904 Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 3 Jul 2024 22:36:09 +0300 Subject: [PATCH] Added proxy support (?) Also uploaded listener --- .gitignore | 10 ++++++++++ async-bot.py | 19 ++++++++++++++----- listen.py | 13 +++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 listen.py diff --git a/.gitignore b/.gitignore index b5a26e0..afb47ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,13 @@ *.py[cow] __pycache__/ venv/ +index.5da4dcc7.js +index.5da4dcc7.js.map +index.js +state.* +bot.py +boykisser-big.png +image.png +logs.txt +rgb111-full.png +state-new.json diff --git a/async-bot.py b/async-bot.py index 647cee9..857cb0a 100644 --- a/async-bot.py +++ b/async-bot.py @@ -1,7 +1,9 @@ import asyncio +from typing import Optional from socketio import AsyncSimpleClient from aiohttp import ClientSession +from aiohttp_proxy import ProxyConnector from PIL import Image, ImageFont, ImageDraw, ImageFilter from base64 import b64decode from random import choice @@ -15,6 +17,7 @@ class AsyncBotManager: self.font = ImageFont.load_default(8) self.difference: dict[int, bool] = {} + self._last_update = 0 self._shutdown: bool = False def put_text(self, x: int, y: int, text: str): @@ -41,6 +44,7 @@ class AsyncBotManager: data = await req.json() buffer = b64decode(data["full_state"].encode() + b"=") self.canvas.paste(Image.frombytes("1", (1000, 1000), buffer)) + self._last_update: int = data["timestamp"] async with AsyncSimpleClient(http_session=http) as sio: await sio.connect(f"{self.base}/socket.io") while not self._shutdown: @@ -50,8 +54,11 @@ class AsyncBotManager: image = Image.frombytes("1", (1000, 1000), buffer) self.canvas.paste(image) image.close() + self._last_update: int = data["timestamp"] elif event == "batched_bit_toggles": bits_on, bits_off, timestamp = data + if timestamp < self._last_update: + print("SKIPPING UPDATES: TOO OLD") for ndx in bits_on: y, x = divmod(ndx, 1000) self.canvas.putpixel((x, y), 255) @@ -61,8 +68,9 @@ class AsyncBotManager: else: print("unknown event", event, data) - async def writer(self): - async with ClientSession() as http: + async def writer(self, proxy_url: Optional[str] = None): + proxy = ProxyConnector.from_url(proxy_url) if proxy_url else None + async with ClientSession(connector=proxy) as http: async with AsyncSimpleClient(http_session=http) as sio: await sio.connect(f"{self.base}/socket.io") while not self._shutdown: @@ -119,9 +127,10 @@ async def amain(): mgr.difference[ndx_start + 1] = g > 128 mgr.difference[ndx_start + 2] = b > 128 - await asyncio.gather(*[ - mgr.writer() for _ in range(settings["n_bots"]) - ], return_exceptions=True) + if (n_proxies := len(settings["proxies"])): + await asyncio.gather(*[ + mgr.writer(settings["proxies"][i % n_proxies]) for i in range(settings["n_bots"]) + ], return_exceptions=True) diff --git a/listen.py b/listen.py new file mode 100644 index 0000000..598348a --- /dev/null +++ b/listen.py @@ -0,0 +1,13 @@ +from socketio import SimpleClient + +from json import dumps +from datetime import datetime + +with open("logs.txt", "a") as fp: + with SimpleClient() as sio: + sio.connect("https://onemillioncheckboxes.com/socket.io") + while True: + event = sio.receive() + now = datetime.utcnow() + print(now, event) + fp.write(str(now.timestamp()) + " " + dumps(event) + "\n")