r8limiter
This commit is contained in:
parent
448aa9d411
commit
3ab07b6967
17
main.py
17
main.py
|
@ -3,6 +3,7 @@ from ircked.bot import irc_bot
|
|||
from ircked.message import *
|
||||
import traceback
|
||||
import html
|
||||
import ratelimiter
|
||||
|
||||
class bird_inst():
|
||||
def __init__(self, endpoint, httpendpoint, config):
|
||||
|
@ -15,6 +16,8 @@ class bird_inst():
|
|||
self.irc.connect_register(self.config["irc_serb"], self.config["irc_port"])
|
||||
self.send_queue = []
|
||||
|
||||
self.limiter = ratelimiter.ratelimit(.1)
|
||||
|
||||
def irc_handler(msg, ctx):
|
||||
#print("<><><><>", str(msg))
|
||||
if msg.command == "PING":
|
||||
|
@ -43,11 +46,13 @@ class bird_inst():
|
|||
async with websockets.connect(self.endpoint, extra_headers=self.headers) as self.ws:
|
||||
print(self.ws)
|
||||
asyncio.get_event_loop().create_task(self._send_post())
|
||||
asyncio.get_event_loop().create_task(self.ship_queued_messages())
|
||||
while True:
|
||||
data = json.loads(await self.ws.recv())
|
||||
print(">>>", data)
|
||||
try: getattr(self, "handle_"+data["type"], None)(data)
|
||||
except Exception as e: print("hey buddy your shits fucked thought you might want to know", e)
|
||||
getattr(self, "handle_"+data["type"], None)(data)
|
||||
#try: getattr(self, "handle_"+data["type"], None)(data)
|
||||
#except Exception as e: print("hey buddy your shits fucked thought you might want to know", e)
|
||||
def handle_message(self, ctx):
|
||||
print("btw i just got this", ctx["data"]["message"]["text"])
|
||||
ctx["data"]["message"]["text"] = html.unescape(ctx["data"]["message"]["text"])
|
||||
|
@ -55,13 +60,13 @@ class bird_inst():
|
|||
mesg = ctx["data"]["message"]["text"].replace("\n", " ")
|
||||
chunks = list(mesg[0+i:400+i] for i in range(0, len(mesg), 400))
|
||||
for m in chunks:
|
||||
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], ctx["data"]["message"]["name"]+": "+m).msg)
|
||||
self.limiter.action(self.irc.sendraw, (privmsg.build(self.config["irc_nick"], self.config["irc_chan"], ctx["data"]["message"]["name"]+": "+m).msg,))
|
||||
def handle_avatar(self, ctx): pass
|
||||
def handle_files(self, ctx):
|
||||
ctx["data"]["message"]["text"] = html.unescape(ctx["data"]["message"]["text"])
|
||||
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], ctx["data"]["message"]["name"]+": "+ctx["data"]["message"]["text"]).msg)
|
||||
for f in ctx["data"]["files"]:
|
||||
self.irc.sendraw(privmsg.build(self.config["irc_nick"], self.config["irc_chan"], f"({ctx['name']} uploaded file: {self.httpendpoint}/storage/files/{f['name']})").msg)
|
||||
self.limiter.action(self.irc.sendraw, (privmsg.build(self.config["irc_nick"], self.config["irc_chan"], f"({ctx['name']} uploaded file: {self.httpendpoint}/storage/files/{f['name']})").msg,))
|
||||
def handle_exit(self, ctx): pass
|
||||
def handle_enter(self, ctx): pass
|
||||
def handle_userLoaded(self, ctx): pass
|
||||
|
@ -76,6 +81,10 @@ class bird_inst():
|
|||
self.send_queue.remove(msg)
|
||||
print("shipped", msg)
|
||||
await asyncio.sleep(.1)
|
||||
async def ship_queued_messages(self):
|
||||
while True:
|
||||
self.limiter.lazyrun()
|
||||
await asyncio.sleep(.1)
|
||||
cfg = json.loads(open("config.json", "r").read())
|
||||
bi = bird_inst("wss://deekchat.ml/ws", "https://deekchat.ml", cfg)
|
||||
print("yes hello birdchat here")
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
#naive ratelimiter
|
||||
from time import time
|
||||
|
||||
class ratelimit():
|
||||
def __init__(self, min_delay):
|
||||
self.delay = min_delay
|
||||
self.last_action = 0
|
||||
self.action_queue = []
|
||||
def action(self, target, args):
|
||||
print("action~", target, args)
|
||||
if time() - self.last_action > self.delay:
|
||||
self.last_action = time()
|
||||
target(*args)
|
||||
else:
|
||||
print("deferred")
|
||||
self.action_queue.append((f, args))
|
||||
def lazyrun(self):
|
||||
if len(self.action_queue) <= 0: return
|
||||
f = self.action_queue[0][0]
|
||||
args = self.action_queue[0][1]
|
||||
self.action(f, args)
|
Loading…
Reference in New Issue