Moved to AsyncClient

This commit is contained in:
Casey 2024-07-05 20:25:46 +03:00
parent 86a3fd9b26
commit 42e88fce44
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
5 changed files with 36 additions and 18 deletions

View File

@ -1,6 +1,6 @@
import asyncio import asyncio
from typing import NewType, Optional from typing import NewType, Optional
from socketio import AsyncSimpleClient from socketio import AsyncClient, AsyncSimpleClient
from aiohttp import ClientSession from aiohttp import ClientSession
from aiohttp_socks import ProxyConnector from aiohttp_socks import ProxyConnector
from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageSequence, ImageChops from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageSequence, ImageChops
@ -27,6 +27,8 @@ class AsyncBotManager:
self.avoid: set[int] = set() self.avoid: set[int] = set()
self.animations: list[Animation] = [] self.animations: list[Animation] = []
self._change_count = 0
self._last_change_printout = time_now()
@staticmethod @staticmethod
def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image: def get_text_image(text: str, font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> Image.Image:
@ -135,8 +137,14 @@ class AsyncBotManager:
self.canvas.putpixel((x, y), 0) self.canvas.putpixel((x, y), 0)
else: else:
print("unknown event", event, data) print("unknown event", event, data)
now = time_now()
if (now - self._last_change_printout) > 1:
cps = self._change_count / (now - self._last_change_printout)
print(f"Speed: {cps:.2f}/s")
self._change_count = 0
self._last_change_printout = now
for pixmaps, spf in self.animations: for pixmaps, spf in self.animations:
frame_index = int(time_now() / spf) frame_index = int(now / spf)
self.difference.update( self.difference.update(
pixmaps[frame_index % len(pixmaps)] pixmaps[frame_index % len(pixmaps)]
) )
@ -149,7 +157,7 @@ class AsyncBotManager:
): ):
proxy = ProxyConnector.from_url(proxy_url) if proxy_url else None proxy = ProxyConnector.from_url(proxy_url) if proxy_url else None
async with ClientSession(connector=proxy) as http: async with ClientSession(connector=proxy) as http:
async with AsyncSimpleClient(http_session=http) as sio: sio = AsyncClient(http_session=http)
await sio.connect(f"{self.base}/socket.io") await sio.connect(f"{self.base}/socket.io")
while not self._shutdown: while not self._shutdown:
diff = list(self.difference.items()) diff = list(self.difference.items())
@ -158,7 +166,8 @@ class AsyncBotManager:
y, x = divmod(index, 1000) y, x = divmod(index, 1000)
current = self.canvas.getpixel((x, y)) > 0 # type: ignore current = self.canvas.getpixel((x, y)) > 0 # type: ignore
if current != expected: if current != expected:
print(f"[{bot_index:2d}] swap {x:3d} {y:3d}") # print(f"[{bot_index:2d}] swap {x:3d} {y:3d}")
self._change_count += 1
await sio.emit("toggle_bit", {"index": index}) await sio.emit("toggle_bit", {"index": index})
await asyncio.sleep(delay) await asyncio.sleep(delay)
break break
@ -274,6 +283,7 @@ async def amain() -> None:
mgr.get_difference_image().save("result.png") mgr.get_difference_image().save("result.png")
mgr.get_avoid_image().save("avoid.png") mgr.get_avoid_image().save("avoid.png")
print("Starting writers...")
if n_proxies := len(settings["proxies"]): if n_proxies := len(settings["proxies"]):
await asyncio.gather( await asyncio.gather(
*[ *[

View File

@ -9,7 +9,7 @@ from datetime import datetime
COLORS = [(0x33, 0x33, 0x66), (0x96, 0x96, 0xe0)] COLORS = [(0x33, 0x33, 0x66), (0x96, 0x96, 0xe0)]
COLORS_UNPACKED = COLORS[0] + ((0, 0, 0) * 254) + COLORS[1] COLORS_UNPACKED = COLORS[0] + ((0, 0, 0) * 254) + COLORS[1]
SCREENSHOTS_TMPL = "./screenshots/%Y%m%d_%H%M%S.png" SCREENSHOTS_TMPL = "./screenshots/%Y%m%d_%H%M%S.webp"
class App(tk.Tk): class App(tk.Tk):
def __init__(self, url: str = "https://onemillioncheckboxes.com") -> None: def __init__(self, url: str = "https://onemillioncheckboxes.com") -> None:
@ -48,7 +48,7 @@ class App(tk.Tk):
print("SAVED", path) print("SAVED", path)
self._image.save(path) self._image.save(path)
self.after(1000, self._save_image) self.after(10000, self._save_image)
def _on_key_down(self, event: tk.Event): def _on_key_down(self, event: tk.Event):
# <KeyPress event state=Control keysym=r keycode=27 char='\x12' x=538 y=556> # <KeyPress event state=Control keysym=r keycode=27 char='\x12' x=538 y=556>

1
pictures/fnf.bin Normal file

File diff suppressed because one or more lines are too long

BIN
pictures/hamis.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 646 B

View File

@ -91,6 +91,13 @@
"path": "./pictures/niko.png", "path": "./pictures/niko.png",
"x": 48, "x": 48,
"y": 12 "y": 12
},
{
"type": "animation",
"path": "./pictures/loading.gif",
"x": 436,
"y": 436,
"spf": 20
} }
] ]
} }