Add XKCD title lookup

Usage: !xkcd (title)
This commit is contained in:
0xDEADCADE 2022-04-09 13:29:40 +00:00 committed by GitHub
parent ae3efa9ddb
commit 5879bc1826
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

19
main.py
View file

@ -55,6 +55,14 @@ client = None
ratelimits = {} ratelimits = {}
# Common function definitions # 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 # Grab a list of all emojis on disk
def list_emojis(): def list_emojis():
global settings global settings
@ -213,6 +221,17 @@ async def xkcd(args, room, event):
comic = "" comic = ""
if len(args) == 1 and args[0].isdecimal(): if len(args) == 1 and args[0].isdecimal():
comic = args[0] + "/" comic = args[0] + "/"
elif len(args) > 0:
lookup = {}
r = requests.get("https://xkcd.com/archive/")
for line in r.text.split("\n"):
if "<a href=\"" in line and "\" title=\"2" in line:
num = line.split("/")[1]
title = filter_xkcd_title(line.split(">")[1].split("<")[0])
lookup[title] = num
user_title = filter_xkcd_title(" ".join(args))
if user_title in lookup.keys():
comic = lookup[user_title] + "/"
try: try:
r = requests.get(f"https://xkcd.com/{comic}info.0.json") r = requests.get(f"https://xkcd.com/{comic}info.0.json")
rj = json.loads(r.text) rj = json.loads(r.text)