2024-02-21 12:03:28 +00:00
|
|
|
import requests, websockets, asyncio, json, re, traceback, subprocess, os, aiohttp, sys
|
2024-02-20 06:14:06 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from pathlib import Path
|
|
|
|
|
2024-02-20 06:57:49 +00:00
|
|
|
HERE = Path(__file__).parent
|
2024-02-20 06:14:06 +00:00
|
|
|
username = os.environ.get("GLUEBOT_USERNAME")
|
|
|
|
password = os.environ.get("GLUEBOT_PASSWORD")
|
|
|
|
|
|
|
|
def get_time():
|
|
|
|
return datetime.now().timestamp()
|
|
|
|
|
2024-02-20 11:45:09 +00:00
|
|
|
def remove_file(path):
|
|
|
|
try:
|
|
|
|
path.unlink()
|
|
|
|
except Exception as e:
|
|
|
|
print(f"(Remove) Error: {e}")
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
def get_extension(path):
|
|
|
|
return Path(path).suffix.lower().lstrip(".")
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"User-Agent": "renabot",
|
|
|
|
"Origin": "https://deek.chat",
|
|
|
|
"DNT": "1",
|
|
|
|
}
|
|
|
|
|
|
|
|
url = "https://deek.chat"
|
|
|
|
ws_url = "wss://deek.chat/ws"
|
|
|
|
prefix = ","
|
|
|
|
token = None
|
|
|
|
session = None
|
2024-02-21 02:49:04 +00:00
|
|
|
delay = 3
|
2024-02-20 06:14:06 +00:00
|
|
|
|
2024-02-20 06:57:49 +00:00
|
|
|
gifmaker = "/usr/bin/gifmaker"
|
2024-02-21 12:03:28 +00:00
|
|
|
gm_common = "--width 350 --nogrow --output /tmp/gifmaker"
|
2024-02-20 06:14:06 +00:00
|
|
|
|
2024-02-20 11:45:09 +00:00
|
|
|
cmd_date = get_time()
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
def update_time():
|
|
|
|
global cmd_date
|
|
|
|
cmd_date = get_time()
|
|
|
|
|
|
|
|
def blocked():
|
|
|
|
return (get_time() - cmd_date) < delay
|
|
|
|
|
|
|
|
def auth():
|
|
|
|
global token, session, headers
|
2024-02-21 12:03:28 +00:00
|
|
|
|
|
|
|
if not username or not password:
|
|
|
|
print("Missing environment variables")
|
|
|
|
exit(1)
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
data = {"name": username, "password": password, "submit": "log+in"}
|
|
|
|
res = requests.post(url + "/login/submit", headers=headers, data=data, allow_redirects=False)
|
|
|
|
token = re.search("(?:api_token)=[^;]+", res.headers.get("Set-Cookie")).group(0)
|
|
|
|
session = re.search("(?:session_id)=[^;]+", res.headers.get("Set-Cookie")).group(0)
|
|
|
|
headers["Cookie"] = token + "; " + session
|
|
|
|
|
|
|
|
async def run():
|
|
|
|
async with websockets.connect(ws_url, extra_headers=headers) as ws:
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
message = await ws.recv()
|
|
|
|
await on_message(ws, message)
|
|
|
|
except KeyboardInterrupt:
|
2024-02-20 06:57:49 +00:00
|
|
|
exit(0)
|
2024-02-20 06:14:06 +00:00
|
|
|
except websockets.exceptions.ConnectionClosedOK:
|
|
|
|
print("WebSocket connection closed")
|
|
|
|
except Exception as e:
|
|
|
|
print("(WebSocket) Error:", e)
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
async def on_message(ws, message):
|
|
|
|
if blocked(): return
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = json.loads(message)
|
|
|
|
except:
|
|
|
|
return
|
|
|
|
|
|
|
|
if data["type"] == "message":
|
|
|
|
if data["data"]["name"] == username:
|
|
|
|
return
|
|
|
|
|
|
|
|
text = data["data"]["text"].strip()
|
|
|
|
|
|
|
|
if not text.startswith(prefix):
|
|
|
|
return
|
|
|
|
|
|
|
|
room_id = data["roomId"]
|
|
|
|
words = text.lstrip(prefix).split(" ")
|
|
|
|
cmd = words[0]
|
|
|
|
args = words[1:]
|
|
|
|
|
|
|
|
if cmd == "ping":
|
|
|
|
update_time()
|
|
|
|
await send_message(ws, "Pong!", room_id)
|
|
|
|
|
2024-02-20 11:50:23 +00:00
|
|
|
elif cmd == "help":
|
|
|
|
update_time()
|
|
|
|
await send_message(ws, f"Commands: describe | wins | numbers | date", room_id)
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
elif cmd == "describe":
|
|
|
|
if len(args) >= 1:
|
|
|
|
update_time()
|
|
|
|
await gif_describe(args[0], room_id)
|
|
|
|
|
2024-02-20 11:50:23 +00:00
|
|
|
elif cmd == "wins" or cmd == "win":
|
2024-02-20 06:14:06 +00:00
|
|
|
if len(args) >= 1:
|
|
|
|
update_time()
|
|
|
|
await gif_wins(args[0], room_id)
|
|
|
|
|
2024-02-21 02:01:30 +00:00
|
|
|
elif cmd == "numbers" or cmd == "number" or cmd == "nums" or cmd == "num":
|
2024-02-20 06:14:06 +00:00
|
|
|
update_time()
|
|
|
|
await gif_numbers(None, room_id)
|
|
|
|
|
2024-02-20 12:30:27 +00:00
|
|
|
elif cmd == "date" or cmd == "data" or cmd == "time" or cmd == "datetime":
|
2024-02-20 06:14:06 +00:00
|
|
|
update_time()
|
|
|
|
await gif_date(None, room_id)
|
|
|
|
|
2024-02-20 06:57:49 +00:00
|
|
|
def get_input_path(name):
|
|
|
|
return str(Path(HERE, name))
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
async def gif_describe(who, room_id):
|
2024-02-20 06:57:49 +00:00
|
|
|
input_path = get_input_path("describe.jpg")
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
command = [
|
|
|
|
gifmaker,
|
|
|
|
gm_common,
|
2024-02-20 06:57:49 +00:00
|
|
|
f"--input '{input_path}'",
|
2024-02-21 02:49:04 +00:00
|
|
|
f"--words '{who} is\\n[Random] [x5]'",
|
2024-02-21 12:05:00 +00:00
|
|
|
"--filter anyhue2 --opacity 1 --fontsize 66 --delay 700",
|
2024-02-21 12:03:28 +00:00
|
|
|
"--padding 50 --fontcolor light2 --bgcolor dark2",
|
2024-02-20 06:14:06 +00:00
|
|
|
]
|
|
|
|
|
2024-02-20 12:23:18 +00:00
|
|
|
await run_gifmaker(command, room_id)
|
2024-02-20 06:14:06 +00:00
|
|
|
|
|
|
|
async def gif_wins(who, room_id):
|
2024-02-20 06:57:49 +00:00
|
|
|
input_path = get_input_path("wins.gif")
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
command = [
|
|
|
|
gifmaker,
|
|
|
|
gm_common,
|
2024-02-20 06:57:49 +00:00
|
|
|
f"--input '{input_path}'",
|
2024-02-21 12:03:28 +00:00
|
|
|
f"--words '{who} wins a ; [repeat] ; [RANDOM] ; [repeat]' --bgcolor 0,0,0",
|
|
|
|
"--bottom 0 --filter anyhue2 --framelist 11,11,33,33",
|
2024-02-20 06:14:06 +00:00
|
|
|
]
|
|
|
|
|
2024-02-20 12:23:18 +00:00
|
|
|
await run_gifmaker(command, room_id)
|
2024-02-20 06:14:06 +00:00
|
|
|
|
|
|
|
async def gif_numbers(who, room_id):
|
2024-02-20 06:57:49 +00:00
|
|
|
input_path = get_input_path("numbers.png")
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
command = [
|
|
|
|
gifmaker,
|
|
|
|
gm_common,
|
2024-02-20 06:57:49 +00:00
|
|
|
f"--input '{input_path}'",
|
2024-02-21 12:06:43 +00:00
|
|
|
"--top 0 --words '[number 0-999] [x3]' --fontcolor 0,0,0 --fontsize 66",
|
2024-02-20 06:14:06 +00:00
|
|
|
]
|
|
|
|
|
2024-02-20 12:23:18 +00:00
|
|
|
await run_gifmaker(command, room_id)
|
2024-02-20 06:14:06 +00:00
|
|
|
|
|
|
|
async def gif_date(who, room_id):
|
2024-02-20 06:57:49 +00:00
|
|
|
input_path = get_input_path("time.jpg")
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
command = [
|
|
|
|
gifmaker,
|
|
|
|
gm_common,
|
2024-02-20 06:57:49 +00:00
|
|
|
f"--input '{input_path}'",
|
2024-02-20 06:14:06 +00:00
|
|
|
"--words 'Date: [date %A %d] ; [repeat] ; Time: [date %I:%M %p] ; [repeat]'",
|
2024-02-21 12:03:28 +00:00
|
|
|
"--filter anyhue2 --bottom 0 --bgcolor 0,0,0 --fontsize 80",
|
2024-02-20 06:14:06 +00:00
|
|
|
]
|
|
|
|
|
2024-02-20 12:23:18 +00:00
|
|
|
await run_gifmaker(command, room_id)
|
2024-02-20 06:14:06 +00:00
|
|
|
|
2024-02-20 12:23:18 +00:00
|
|
|
async def run_gifmaker(command, room_id):
|
2024-02-20 06:14:06 +00:00
|
|
|
process = await asyncio.create_subprocess_shell(
|
|
|
|
" ".join(command),
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
stdout, stderr = await process.communicate()
|
|
|
|
|
|
|
|
if process.returncode != 0:
|
|
|
|
print(f"(Process) Error: {stderr.decode()}")
|
|
|
|
return
|
|
|
|
|
|
|
|
await upload(Path(stdout.decode().strip()), room_id)
|
|
|
|
|
|
|
|
async def upload(path, room_id):
|
2024-02-20 11:45:09 +00:00
|
|
|
if (not path.exists()) or (not path.is_file()):
|
2024-02-20 06:14:06 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
cookies = {
|
|
|
|
"session_id": session.split("=")[1],
|
|
|
|
"api_token": token.split("=")[1],
|
|
|
|
}
|
|
|
|
|
|
|
|
ext = get_extension(path)
|
2024-02-20 11:45:09 +00:00
|
|
|
url = "https://deek.chat/message/send/" + str(room_id)
|
|
|
|
data = aiohttp.FormData()
|
|
|
|
data.add_field(name="files[]", value=open(path, "rb"), \
|
|
|
|
filename=path.name, content_type=f"image/{ext}")
|
2024-02-20 06:14:06 +00:00
|
|
|
|
|
|
|
try:
|
2024-02-20 11:45:09 +00:00
|
|
|
async with aiohttp.ClientSession(cookies=cookies) as sess:
|
|
|
|
async with sess.post(url, data=data, headers={}) as response:
|
|
|
|
await response.text()
|
2024-02-20 06:14:06 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"(Upload) Error: {e}")
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2024-02-20 11:45:09 +00:00
|
|
|
remove_file(path)
|
|
|
|
|
2024-02-20 06:14:06 +00:00
|
|
|
async def send_message(ws, text, room_id):
|
|
|
|
await ws.send(json.dumps({"type": "message", "data": text, "roomId": room_id}))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
auth()
|
|
|
|
print("Authenticated")
|
|
|
|
asyncio.run(run())
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print("(Main) Error:", e)
|
|
|
|
traceback.print_exc()
|