mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
[MOD] Externalize stuff out. The main file shouldn't have much.
This commit is contained in:
parent
062885ab2b
commit
1f49413e18
3 changed files with 82 additions and 67 deletions
39
callbacks.py
Normal file
39
callbacks.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
from nio import *
|
||||||
|
|
||||||
|
import utils
|
||||||
|
|
||||||
|
|
||||||
|
# Our message callback. It should be passed through a router.
|
||||||
|
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||||
|
import re
|
||||||
|
import latex
|
||||||
|
client = utils.get_client()
|
||||||
|
if event.sender == client.user_id:
|
||||||
|
return
|
||||||
|
print("ae", client)
|
||||||
|
|
||||||
|
for tex in re.findall(r'((?:\$[^\$]+\$)|(?:\$\$[^\$]+\$\$)|(?:\\\[[^\]]+\\\]))', event.body, re.M):
|
||||||
|
print("Text:", tex)
|
||||||
|
try:
|
||||||
|
filename = latex.render("", tex)
|
||||||
|
await utils.send_png(room, filename)
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
content = {
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"body": f"Couldn't parse LaTeX correctly.\n```{e.args[0]}\n```",
|
||||||
|
"formatted_body": f"Couldn't parse LaTeX correctly.<br><code><pre>{e.args[0]}</pre></code>",
|
||||||
|
}
|
||||||
|
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
||||||
|
except OSError:
|
||||||
|
content = {
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"body": "???",
|
||||||
|
"formatted_body": "???"
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
||||||
|
|
||||||
|
# Our file callback.
|
||||||
|
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
|
||||||
|
url = event.url
|
||||||
|
filename = event.body
|
73
main.py
73
main.py
|
@ -1,76 +1,14 @@
|
||||||
from nio import *
|
from nio import *
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import yaml
|
import utils
|
||||||
|
|
||||||
|
|
||||||
async def send_png(room: MatrixRoom, filename: str) -> None:
|
|
||||||
import imagesize
|
|
||||||
import os
|
|
||||||
basename = os.path.basename(filename)
|
|
||||||
w, h = imagesize.get(filename)
|
|
||||||
|
|
||||||
file = open(filename, "r+b")
|
|
||||||
size = os.path.getsize(filename)
|
|
||||||
|
|
||||||
filename = os.path.basename(filename)
|
|
||||||
resp, keys = await client.upload(file, content_type="image/png", filename=basename)
|
|
||||||
|
|
||||||
if not isinstance(resp, UploadResponse):
|
|
||||||
raise OSError("Couldn't upload file.")
|
|
||||||
|
|
||||||
content = {
|
|
||||||
"body": basename,
|
|
||||||
"info": {
|
|
||||||
"size": size,
|
|
||||||
"mimetype": "image/png",
|
|
||||||
"w": w, "h": h
|
|
||||||
},
|
|
||||||
"msgtype": "m.image",
|
|
||||||
"url": resp.content_uri,
|
|
||||||
}
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
|
||||||
|
|
||||||
|
|
||||||
# Our message callback. It should be passed through a router.
|
|
||||||
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
|
||||||
import re
|
|
||||||
import latex
|
|
||||||
if event.sender == client.user_id:
|
|
||||||
return
|
|
||||||
|
|
||||||
for tex in re.findall(r'((?:\$[^\$]+\$)|(?:\$\$[^\$]+\$\$)|(?:\\\[[^\]]+\\\]))', event.body, re.M):
|
|
||||||
print("Text:", tex)
|
|
||||||
try:
|
|
||||||
filename = latex.render("", tex)
|
|
||||||
await send_png(room, filename)
|
|
||||||
except FileNotFoundError as e:
|
|
||||||
content = {
|
|
||||||
"msgtype": "m.text",
|
|
||||||
"body": f"Couldn't parse LaTeX correctly.\n```{e.args[0]}\n```",
|
|
||||||
"formatted_body": f"Couldn't parse LaTeX correctly.<br><code><pre>{e.args[0]}</pre></code>",
|
|
||||||
}
|
|
||||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
|
||||||
except OSError:
|
|
||||||
content = {
|
|
||||||
"msgtype": "m.text",
|
|
||||||
"body": "???",
|
|
||||||
"formatted_body": "???"
|
|
||||||
}
|
|
||||||
|
|
||||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
|
||||||
|
|
||||||
# Our file callback.
|
|
||||||
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
|
|
||||||
url = event.url
|
|
||||||
filename = event.body
|
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
|
import callbacks
|
||||||
|
import yaml
|
||||||
# Load our config file.
|
# Load our config file.
|
||||||
global client
|
|
||||||
|
|
||||||
config = {}
|
config = {}
|
||||||
try:
|
try:
|
||||||
|
@ -98,8 +36,9 @@ async def main() -> None:
|
||||||
await client.sync(timeout=3000)
|
await client.sync(timeout=3000)
|
||||||
|
|
||||||
# Register all of the callbacks
|
# Register all of the callbacks
|
||||||
client.add_event_callback(msg_cb, RoomMessageText)
|
utils.set_client(client)
|
||||||
client.add_event_callback(file_cb, RoomMessageFile)
|
client.add_event_callback(callbacks.msg_cb, RoomMessageText)
|
||||||
|
client.add_event_callback(callbacks.file_cb, RoomMessageFile)
|
||||||
|
|
||||||
await client.sync_forever(timeout=10000, full_state=True)
|
await client.sync_forever(timeout=10000, full_state=True)
|
||||||
|
|
||||||
|
|
37
utils.py
Normal file
37
utils.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
from nio import *
|
||||||
|
|
||||||
|
def set_client(c: AsyncClient) -> None:
|
||||||
|
global client
|
||||||
|
client = c
|
||||||
|
|
||||||
|
def get_client() -> AsyncClient:
|
||||||
|
return client
|
||||||
|
|
||||||
|
async def send_png(room: MatrixRoom, filename: str) -> None:
|
||||||
|
import imagesize
|
||||||
|
import os
|
||||||
|
basename = os.path.basename(filename)
|
||||||
|
w, h = imagesize.get(filename)
|
||||||
|
|
||||||
|
file = open(filename, "r+b")
|
||||||
|
size = os.path.getsize(filename)
|
||||||
|
|
||||||
|
filename = os.path.basename(filename)
|
||||||
|
resp, keys = await client.upload(file, content_type="image/png", filename=basename)
|
||||||
|
|
||||||
|
if not isinstance(resp, UploadResponse):
|
||||||
|
raise OSError("Couldn't upload file.")
|
||||||
|
|
||||||
|
content = {
|
||||||
|
"body": basename,
|
||||||
|
"info": {
|
||||||
|
"size": size,
|
||||||
|
"mimetype": "image/png",
|
||||||
|
"w": w, "h": h
|
||||||
|
},
|
||||||
|
"msgtype": "m.image",
|
||||||
|
"url": resp.content_uri,
|
||||||
|
}
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
Loading…
Add table
Reference in a new issue