This commit is contained in:
Auric Vente 2024-02-20 05:45:09 -06:00
parent e904c5bf43
commit 6b7d8c87b5
2 changed files with 22 additions and 19 deletions

38
main.py
View File

@ -1,4 +1,4 @@
import requests, websockets, asyncio, json, re, traceback, subprocess, os import requests, websockets, asyncio, json, re, traceback, subprocess, os, aiohttp
from datetime import datetime from datetime import datetime
from pathlib import Path from pathlib import Path
@ -13,6 +13,13 @@ if not username or not password:
def get_time(): def get_time():
return datetime.now().timestamp() return datetime.now().timestamp()
def remove_file(path):
try:
path.unlink()
except Exception as e:
print(f"(Remove) Error: {e}")
traceback.print_exc()
def get_extension(path): def get_extension(path):
return Path(path).suffix.lower().lstrip(".") return Path(path).suffix.lower().lstrip(".")
@ -22,7 +29,6 @@ headers = {
"DNT": "1", "DNT": "1",
} }
cmd_date = get_time()
url = "https://deek.chat" url = "https://deek.chat"
ws_url = "wss://deek.chat/ws" ws_url = "wss://deek.chat/ws"
prefix = "," prefix = ","
@ -33,6 +39,8 @@ delay = 5
gifmaker = "/usr/bin/gifmaker" gifmaker = "/usr/bin/gifmaker"
gm_common = "--font triplex --width 555 --nogrow --output /tmp/gifmaker" gm_common = "--font triplex --width 555 --nogrow --output /tmp/gifmaker"
cmd_date = get_time()
def update_time(): def update_time():
global cmd_date global cmd_date
cmd_date = get_time() cmd_date = get_time()
@ -177,7 +185,7 @@ async def run_command(command, room_id):
await upload(Path(stdout.decode().strip()), room_id) await upload(Path(stdout.decode().strip()), room_id)
async def upload(path, room_id): async def upload(path, room_id):
if not path.exists() or path.is_dir(): if (not path.exists()) or (not path.is_file()):
return return
cookies = { cookies = {
@ -186,27 +194,21 @@ async def upload(path, room_id):
} }
ext = get_extension(path) ext = get_extension(path)
url = "https://deek.chat/message/send/" + str(room_id)
files = { data = aiohttp.FormData()
"text": (None, ""), data.add_field(name="files[]", value=open(path, "rb"), \
"files[]": (path.name, open(path, "rb").read(), f"image/{ext}"), filename=path.name, content_type=f"image/{ext}")
}
def remove_file():
try:
path.unlink()
except Exception as e:
print(f"(Remove) Error: {e}")
traceback.print_exc()
try: try:
requests.post("https://deek.chat/message/send/" + str(room_id), cookies=cookies, headers={}, files=files) async with aiohttp.ClientSession(cookies=cookies) as sess:
remove_file() async with sess.post(url, data=data, headers={}) as response:
await response.text()
except Exception as e: except Exception as e:
remove_file()
print(f"(Upload) Error: {e}") print(f"(Upload) Error: {e}")
traceback.print_exc() traceback.print_exc()
remove_file(path)
async def send_message(ws, text, room_id): async def send_message(ws, text, room_id):
await ws.send(json.dumps({"type": "message", "data": text, "roomId": room_id})) await ws.send(json.dumps({"type": "message", "data": text, "roomId": room_id}))

View File

@ -1,2 +1,3 @@
requests ~= 2.31.0 requests ~= 2.31.0
websockets ~= 12.0 websockets ~= 12.0
aiohttp ~= 3.9.3