Add inexact search on xkcd titles

This commit is contained in:
deadcade 2022-06-12 14:52:18 +02:00
parent 82160775b1
commit 2d5d145dc7

View file

@ -9,6 +9,7 @@ import os # Filesystem
import mimetypes # Uploading files to homeserver import mimetypes # Uploading files to homeserver
import html # HTML formatting import html # HTML formatting
import requests # XKCD command import requests # XKCD command
import difflib # XKCD Title lookup
import subprocess # GIF Emojis, ping command (admin) import subprocess # GIF Emojis, ping command (admin)
import random # roll command import random # roll command
import datetime # Ratelimiting import datetime # Ratelimiting
@ -249,8 +250,9 @@ async def xkcd(args, room, event):
title = filter_xkcd_title(line.split(">")[1].split("<")[0]) title = filter_xkcd_title(line.split(">")[1].split("<")[0])
lookup[title] = num lookup[title] = num
user_title = filter_xkcd_title(" ".join(args)) user_title = filter_xkcd_title(" ".join(args))
if user_title in lookup.keys(): title = difflib.get_close_matches(user_title, lookup.keys(), n=1)[0]
comic = lookup[user_title] + "/" if title:
comic = lookup[title] + "/"
r = requests.get(f"https://xkcd.com/{comic}info.0.json") r = requests.get(f"https://xkcd.com/{comic}info.0.json")
if settings["debug"]: if settings["debug"]:
rj = json.loads(r.text) rj = json.loads(r.text)