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 *
|
from ircked.message import *
|
||||||
import traceback
|
import traceback
|
||||||
import html
|
import html
|
||||||
|
import ratelimiter
|
||||||
|
|
||||||
class bird_inst():
|
class bird_inst():
|
||||||
def __init__(self, endpoint, httpendpoint, config):
|
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.irc.connect_register(self.config["irc_serb"], self.config["irc_port"])
|
||||||
self.send_queue = []
|
self.send_queue = []
|
||||||
|
|
||||||
|
self.limiter = ratelimiter.ratelimit(.1)
|
||||||
|
|
||||||
def irc_handler(msg, ctx):
|
def irc_handler(msg, ctx):
|
||||||
#print("<><><><>", str(msg))
|
#print("<><><><>", str(msg))
|
||||||
if msg.command == "PING":
|
if msg.command == "PING":
|
||||||
|
@ -43,11 +46,13 @@ class bird_inst():
|
||||||
async with websockets.connect(self.endpoint, extra_headers=self.headers) as self.ws:
|
async with websockets.connect(self.endpoint, extra_headers=self.headers) as self.ws:
|
||||||
print(self.ws)
|
print(self.ws)
|
||||||
asyncio.get_event_loop().create_task(self._send_post())
|
asyncio.get_event_loop().create_task(self._send_post())
|
||||||
|
asyncio.get_event_loop().create_task(self.ship_queued_messages())
|
||||||
while True:
|
while True:
|
||||||
data = json.loads(await self.ws.recv())
|
data = json.loads(await self.ws.recv())
|
||||||
print(">>>", data)
|
print(">>>", data)
|
||||||
try: getattr(self, "handle_"+data["type"], None)(data)
|
getattr(self, "handle_"+data["type"], None)(data)
|
||||||
except Exception as e: print("hey buddy your shits fucked thought you might want to know", e)
|
#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):
|
def handle_message(self, ctx):
|
||||||
print("btw i just got this", ctx["data"]["message"]["text"])
|
print("btw i just got this", ctx["data"]["message"]["text"])
|
||||||
ctx["data"]["message"]["text"] = html.unescape(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", " ")
|
mesg = ctx["data"]["message"]["text"].replace("\n", " ")
|
||||||
chunks = list(mesg[0+i:400+i] for i in range(0, len(mesg), 400))
|
chunks = list(mesg[0+i:400+i] for i in range(0, len(mesg), 400))
|
||||||
for m in chunks:
|
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_avatar(self, ctx): pass
|
||||||
def handle_files(self, ctx):
|
def handle_files(self, ctx):
|
||||||
ctx["data"]["message"]["text"] = html.unescape(ctx["data"]["message"]["text"])
|
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)
|
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"]:
|
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_exit(self, ctx): pass
|
||||||
def handle_enter(self, ctx): pass
|
def handle_enter(self, ctx): pass
|
||||||
def handle_userLoaded(self, ctx): pass
|
def handle_userLoaded(self, ctx): pass
|
||||||
|
@ -76,6 +81,10 @@ class bird_inst():
|
||||||
self.send_queue.remove(msg)
|
self.send_queue.remove(msg)
|
||||||
print("shipped", msg)
|
print("shipped", msg)
|
||||||
await asyncio.sleep(.1)
|
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())
|
cfg = json.loads(open("config.json", "r").read())
|
||||||
bi = bird_inst("wss://deekchat.ml/ws", "https://deekchat.ml", cfg)
|
bi = bird_inst("wss://deekchat.ml/ws", "https://deekchat.ml", cfg)
|
||||||
print("yes hello birdchat here")
|
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