From 83b7f55399fa8fceb6ba59e23937a916d0977248 Mon Sep 17 00:00:00 2001 From: deadcade Date: Sun, 12 Jun 2022 12:31:51 +0200 Subject: [PATCH] Reimplement XKCD Title lookup --- data/settings.json | 2 +- main.py | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/data/settings.json b/data/settings.json index 9380e44..e6b60cd 100644 --- a/data/settings.json +++ b/data/settings.json @@ -23,7 +23,7 @@ "help": "{prefix}help (command): Get information on commands", "ping": "{prefix}ping {Host}: Ping the selfbot (or any host)", "roll": "{prefix}roll (Number): Roll a dice (with number sides)", - "xkcd": "{prefix}xkcd (Number): Get an XKCD comic", + "xkcd": "{prefix}xkcd (Number/Title): Get an XKCD comic", "shrug": "{prefix}shrug {text}: Append ¯\\_(ツ)_/¯ to the text", "emoji_size": "{prefix}emoji_size {size}: Set the default emoji size", "emoji_list": "{prefix}emoji_list: Get a list of usable custom emojis" diff --git a/main.py b/main.py index 941ac00..8c60670 100644 --- a/main.py +++ b/main.py @@ -55,6 +55,14 @@ client = None ratelimits = {} # Common function definitions +# Filter a title for local lookup with XKCD +def filter_xkcd_title(title): + filtered_title = "" + for char in title.lower().split("(")[0]: + if (char.isdecimal() or char.isalpha()) and (not char == " "): + filtered_title += char + return filtered_title + # Grab a list of all emojis on disk def list_emojis(): global settings @@ -221,15 +229,29 @@ async def xkcd(args, room, event): comic = "" if len(args) == 1 and args[0].isdecimal(): comic = args[0] + "/" - try: - r = requests.get(f"https://xkcd.com/{comic}info.0.json") + elif len(args) > 0: + lookup = {} + r = requests.get("https://xkcd.com/archive/") + for line in r.text.split("\n"): + if "")[1].split("<")[0]) + lookup[title] = num + user_title = filter_xkcd_title(" ".join(args)) + if user_title in lookup.keys(): + comic = lookup[user_title] + "/" + r = requests.get(f"https://xkcd.com/{comic}info.0.json") + if settings["debug"]: rj = json.loads(r.text) - filename = download_file(rj["img"], settings["cache_path"] + "/" + str(rj["num"]) + "." + rj["img"].split(".")[-1]) - image = await send_file(filename) - await send_text(room.room_id, f"{rj['year']}/{rj['month']}/{rj['day']}, {str(rj['num'])}: {rj['safe_title']}") - return await send_image(room.room_id, image, rj['alt']) - except Exception: - return await send_text(room.room_id, "Failed to get XKCD!") + else: + try: + rj = json.loads(r.text) + except Exception: + return await send_text(room.room_id, "Failed to get XKCD!") + filename = download_file(rj["img"], settings["cache_path"] + "/" + str(rj["num"]) + "." + rj["img"].split(".")[-1]) + image = await send_file(filename) + await send_text(room.room_id, f"{rj['year']}/{rj['month']}/{rj['day']}, {str(rj['num'])}: {rj['safe_title']}") + return await send_image(room.room_id, image, rj['alt']) async def message_callback(room: nio.MatrixRoom, event: nio.RoomMessageText) -> None: global client, settings, emojis