Added proxy support (?)

Also uploaded listener
This commit is contained in:
Casey 2024-07-03 22:36:09 +03:00
parent d23df9c51c
commit 4978e66028
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
3 changed files with 37 additions and 5 deletions

10
.gitignore vendored
View File

@ -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

View File

@ -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,8 +127,9 @@ async def amain():
mgr.difference[ndx_start + 1] = g > 128
mgr.difference[ndx_start + 2] = b > 128
if (n_proxies := len(settings["proxies"])):
await asyncio.gather(*[
mgr.writer() for _ in range(settings["n_bots"])
mgr.writer(settings["proxies"][i % n_proxies]) for i in range(settings["n_bots"])
], return_exceptions=True)

13
listen.py Normal file
View File

@ -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")