diff --git a/async-bot.py b/async-bot.py index ccf7d69..f9464b7 100644 --- a/async-bot.py +++ b/async-bot.py @@ -47,7 +47,7 @@ class AsyncBotManager: @staticmethod def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image: left, top, right, bottom = font.getbbox(text) - with Image.new("LA", (int(right - left) + 4, int(bottom - top) + 8), 0) as im: + with Image.new("LA", (int(right - left) + 4, int(bottom - top) + 16), 0) as im: draw = ImageDraw.Draw(im) draw.rectangle((0, 0, im.width, im.height), (0, 0)) draw.text((left + 2, top + 2), text, font=font, fill=(255, 0), @@ -192,7 +192,7 @@ class AsyncBotManager: else: print("unknown event", event, data) now = time_now() - if (now - self._last_printout) > 10: + if (now - self._last_printout) > 5: outgoing = self._written_boxes / (now - self._last_printout) incoming = self._read_boxes / (now - self._last_printout) print() @@ -239,26 +239,30 @@ class AsyncBotManager: ): proxy = ProxyConnector.from_url(proxy_url) if proxy_url else None await self.ready_event.wait() - async with ClientSession(connector=proxy, timeout=TIMEOUT) as http: - async with AsyncSimpleClient(http_session=http) as sio: - await sio.connect(f"{self.base}/socket.io") - print(f"Writer {bot_index} connected") - offset = 0 - while not self._shutdown: - diff = list(self.difference.items()) - diff = sorted(diff, key=lambda kv: kv[0]) - for _ in range(100): - index, expected = diff[offset % len(diff)] - offset += randint(0, 1000) - y, x = divmod(index, 1000) - current = self.canvas.getpixel((x, y)) > 0 # type: ignore - if current != expected: - self._written_boxes += 1 - await sio.emit("toggle_bit", {"index": index}) - await asyncio.sleep(delay) - self._active.add(bot_index) - with suppress(): - await sio.receive(0.1) + try: + async with ClientSession(connector=proxy, timeout=TIMEOUT) as http: + async with AsyncSimpleClient(http_session=http) as sio: + await sio.connect(f"{self.base}/socket.io") + print(f"Writer {bot_index} connected") + offset = 0 + while not self._shutdown: + diff = list(self.difference.items()) + diff = sorted(diff, key=lambda kv: kv[0]) + for _ in range(100): + index, expected = diff[offset % len(diff)] + offset += randint(0, 1000) + y, x = divmod(index, 1000) + current = self.canvas.getpixel((x, y)) > 0 # type: ignore + if current != expected: + self._written_boxes += 1 + await sio.emit("toggle_bit", {"index": index}) + await asyncio.sleep(delay) + self._active.add(bot_index) + with suppress(BaseException): + await sio.receive(0.1) + print(f"Worker {bot_index} stopped: shutdown") + except Exception as e: + print(f"Worker {bot_index} died: {e!r}") async def __aenter__(self): self._listener_task = asyncio.create_task(self.listener())