Signed-off-by: Sebastian Schmittner <sebastian.schmittner@eecc.de>
This commit is contained in:
Sebastian Schmittner 2023-01-31 21:04:26 +01:00
parent 656f0748ea
commit 91dfcf8328
No known key found for this signature in database
GPG Key ID: DF6DF57D5F9D539F
1 changed files with 3 additions and 9 deletions

View File

@ -1,10 +1,7 @@
import os
import re import re
import html import html
import requests import requests
from nio import AsyncClient, UploadError
from nio import UploadResponse
from modules.common.module import BotModule from modules.common.module import BotModule
from modules.common.exceptions import UploadFailed from modules.common.exceptions import UploadFailed
@ -20,7 +17,6 @@ class Xkcd:
self.alt = alt self.alt = alt
self.num = num self.num = num
@staticmethod @staticmethod
def create_from_json(json): def create_from_json(json):
return Xkcd(json.get("title"), json.get("img"), json.get("alt"), json.get("num")) return Xkcd(json.get("title"), json.get("img"), json.get("alt"), json.get("num"))
@ -38,7 +34,6 @@ class MatrixModule(BotModule):
super().__init__(name) super().__init__(name)
self.uri_get_latest = 'https://xkcd.com/info.0.json' self.uri_get_latest = 'https://xkcd.com/info.0.json'
async def matrix_message(self, bot, room, event): async def matrix_message(self, bot, room, event):
self.logger.debug(f"room: {room.name} sender: {event.sender} queried the xkcd module with body: {event.body}") self.logger.debug(f"room: {room.name} sender: {event.sender} queried the xkcd module with body: {event.body}")
@ -51,7 +46,7 @@ class MatrixModule(BotModule):
await self.command_help(bot, room) await self.command_help(bot, room)
else: else:
xkcd_id = args[1] xkcd_id = args[1]
if re.match("\d+", xkcd_id) is not None: if re.match("\\d+", xkcd_id) is not None:
await self.send_xkcd(bot, room, self.uri_get_by_id(xkcd_id)) await self.send_xkcd(bot, room, self.uri_get_by_id(xkcd_id))
else: else:
await bot.send_text(room, "Invalid comic id. ids must be positive integers.") await bot.send_text(room, "Invalid comic id. ids must be positive integers.")
@ -76,7 +71,7 @@ class MatrixModule(BotModule):
try: try:
matrix_uri, mimetype, w, h, size = await bot.upload_image(img_url) matrix_uri, mimetype, w, h, size = await bot.upload_image(img_url)
except (UploadFailed, TypeError, ValueError): except (UploadFailed, TypeError, ValueError):
await bot.send_text(room, f"Something went wrong uploading {apimg_url}.") await bot.send_text(room, f"Something went wrong uploading {img_url}.")
await bot.send_html(room, f"<b>{html.escape(xkcd.title)} ({html.escape(str(xkcd.num))})</b>", f"{xkcd.title} ({str(xkcd.num)})") await bot.send_html(room, f"<b>{html.escape(xkcd.title)} ({html.escape(str(xkcd.num))})</b>", f"{xkcd.title} ({str(xkcd.num)})")
await bot.send_image(room, matrix_uri, img_url, None, mimetype, w, h, size) await bot.send_image(room, matrix_uri, img_url, None, mimetype, w, h, size)
@ -91,8 +86,7 @@ class MatrixModule(BotModule):
async def command_help(self, bot, room): async def command_help(self, bot, room):
msg = """commands: msg = """commands:
- no arguments: fetch latest xkcd comic - no arguments: fetch latest xkcd comic
- (\d+) fetch and display the xkcd comic with the given id - (\\d+) fetch and display the xkcd comic with the given id
- help - show command help - help - show command help
""" """
await bot.send_text(room, msg) await bot.send_text(room, msg)