28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
import asyncio,time,aiohttp
|
|||
|
|
from core.text import split
|
||
|
|
class Hook:
|
||
|
|
def __init__(s,url):
|
||
|
|
s.url=url;s.q=asyncio.Queue();s.task=None;s.http=None
|
||
|
|
async def start(s):
|
||
|
|
s.http=aiohttp.ClientSession();s.task=asyncio.create_task(s.run())
|
||
|
|
def put(s,text):
|
||
|
|
line=f"`{time.strftime('%d/%m %H:%M:%S')}` {text}"
|
||
|
|
print(line)
|
||
|
|
if s.url:
|
||
|
|
for c in split(line,1900):s.q.put_nowait(c)
|
||
|
|
async def run(s):
|
||
|
|
while True:
|
||
|
|
item=await s.q.get()
|
||
|
|
for i in range(6):
|
||
|
|
try:
|
||
|
|
async with s.http.post(s.url,json={"content":item,"allowed_mentions":{"parse":[]}}) as r:
|
||
|
|
if r.status==429:
|
||
|
|
await asyncio.sleep(float((await r.json()).get("retry_after",1))+.1);continue
|
||
|
|
if r.status<500:break
|
||
|
|
await asyncio.sleep(1+i)
|
||
|
|
except Exception:await asyncio.sleep(1+i)
|
||
|
|
s.q.task_done();await asyncio.sleep(.25)
|
||
|
|
async def close(s):
|
||
|
|
try:await asyncio.wait_for(s.q.join(),15)
|
||
|
|
except asyncio.TimeoutError:pass
|
||
|
|
s.task.cancel();await s.http.close()
|