Moved to AsyncClient
This commit is contained in:
parent
86a3fd9b26
commit
42e88fce44
42
async-bot.py
42
async-bot.py
|
@ -1,6 +1,6 @@
|
|||
import asyncio
|
||||
from typing import NewType, Optional
|
||||
from socketio import AsyncSimpleClient
|
||||
from socketio import AsyncClient, AsyncSimpleClient
|
||||
from aiohttp import ClientSession
|
||||
from aiohttp_socks import ProxyConnector
|
||||
from PIL import Image, ImageFont, ImageDraw, ImageFilter, ImageSequence, ImageChops
|
||||
|
@ -27,6 +27,8 @@ class AsyncBotManager:
|
|||
self.avoid: set[int] = set()
|
||||
|
||||
self.animations: list[Animation] = []
|
||||
self._change_count = 0
|
||||
self._last_change_printout = time_now()
|
||||
|
||||
@staticmethod
|
||||
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)
|
||||
else:
|
||||
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:
|
||||
frame_index = int(time_now() / spf)
|
||||
frame_index = int(now / spf)
|
||||
self.difference.update(
|
||||
pixmaps[frame_index % len(pixmaps)]
|
||||
)
|
||||
|
@ -149,20 +157,21 @@ class AsyncBotManager:
|
|||
):
|
||||
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:
|
||||
diff = list(self.difference.items())
|
||||
for _ in range(100):
|
||||
index, expected = choice(diff)
|
||||
y, x = divmod(index, 1000)
|
||||
current = self.canvas.getpixel((x, y)) > 0 # type: ignore
|
||||
if current != expected:
|
||||
print(f"[{bot_index:2d}] swap {x:3d} {y:3d}")
|
||||
await sio.emit("toggle_bit", {"index": index})
|
||||
await asyncio.sleep(delay)
|
||||
break
|
||||
await asyncio.sleep(0.1)
|
||||
sio = AsyncClient(http_session=http)
|
||||
await sio.connect(f"{self.base}/socket.io")
|
||||
while not self._shutdown:
|
||||
diff = list(self.difference.items())
|
||||
for _ in range(100):
|
||||
index, expected = choice(diff)
|
||||
y, x = divmod(index, 1000)
|
||||
current = self.canvas.getpixel((x, y)) > 0 # type: ignore
|
||||
if current != expected:
|
||||
# print(f"[{bot_index:2d}] swap {x:3d} {y:3d}")
|
||||
self._change_count += 1
|
||||
await sio.emit("toggle_bit", {"index": index})
|
||||
await asyncio.sleep(delay)
|
||||
break
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
async def __aenter__(self):
|
||||
self._listener_task = asyncio.create_task(self.listener())
|
||||
|
@ -274,6 +283,7 @@ async def amain() -> None:
|
|||
mgr.get_difference_image().save("result.png")
|
||||
mgr.get_avoid_image().save("avoid.png")
|
||||
|
||||
print("Starting writers...")
|
||||
if n_proxies := len(settings["proxies"]):
|
||||
await asyncio.gather(
|
||||
*[
|
||||
|
|
|
@ -9,7 +9,7 @@ from datetime import datetime
|
|||
COLORS = [(0x33, 0x33, 0x66), (0x96, 0x96, 0xe0)]
|
||||
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):
|
||||
def __init__(self, url: str = "https://onemillioncheckboxes.com") -> None:
|
||||
|
@ -48,7 +48,7 @@ class App(tk.Tk):
|
|||
print("SAVED", 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):
|
||||
# <KeyPress event state=Control keysym=r keycode=27 char='\x12' x=538 y=556>
|
||||
|
|
File diff suppressed because one or more lines are too long
Binary file not shown.
After Width: | Height: | Size: 646 B |
|
@ -91,6 +91,13 @@
|
|||
"path": "./pictures/niko.png",
|
||||
"x": 48,
|
||||
"y": 12
|
||||
},
|
||||
{
|
||||
"type": "animation",
|
||||
"path": "./pictures/loading.gif",
|
||||
"x": 436,
|
||||
"y": 436,
|
||||
"spf": 20
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue