mirror of
https://git.freetards.xyz/array.in.a.matrix/TexLiLy.git
synced 2025-04-02 13:21:42 -04:00
Merge branch 'dev' into dev
This commit is contained in:
commit
f0d8dd1c7b
1 changed files with 35 additions and 23 deletions
58
main.py
58
main.py
|
@ -4,35 +4,47 @@ import asyncio
|
||||||
import yaml
|
import yaml
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
async def send_png(room: MatrixRoom, filename: str) -> None:
|
||||||
|
import imagesize
|
||||||
|
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.
|
# Our message callback. It should be passed through a router.
|
||||||
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||||
if event.body.startswith("$"):
|
if event.body.startswith("$"):
|
||||||
import imagesize
|
|
||||||
import latex
|
import latex
|
||||||
|
|
||||||
try:
|
try:
|
||||||
filename = latex.render("", event.body)
|
filename = latex.render("", event.body)
|
||||||
w, h = imagesize.get(filename)
|
await send_png(room, filename)
|
||||||
|
except (FileNotFoundError, OSError):
|
||||||
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=filename)
|
|
||||||
content = {
|
content = {
|
||||||
"body": filename,
|
"msgtype": "m.text",
|
||||||
"info": {
|
|
||||||
"size": size,
|
|
||||||
"w": w, "h": h,
|
|
||||||
"mimetype": "image/png"
|
|
||||||
},
|
|
||||||
"msgtype": "m.image",
|
|
||||||
"url": resp.content_uri,
|
|
||||||
}
|
|
||||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
|
||||||
except FileNotFoundError:
|
|
||||||
content = {
|
|
||||||
"msgtype": "m.text",
|
|
||||||
"body": "Couldn't parse LaTeX correctly."
|
"body": "Couldn't parse LaTeX correctly."
|
||||||
}
|
}
|
||||||
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
await client.room_send(room.room_id, message_type="m.room.message", content=content)
|
||||||
|
@ -41,7 +53,7 @@ async def msg_cb(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||||
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
|
async def file_cb(room: MatrixRoom, event: RoomMessageFile) -> None:
|
||||||
url = event.url
|
url = event.url
|
||||||
filename = event.body
|
filename = event.body
|
||||||
|
|
||||||
|
|
||||||
async def main() -> None:
|
async def main() -> None:
|
||||||
# Load our config file.
|
# Load our config file.
|
||||||
|
|
Loading…
Add table
Reference in a new issue