bitbot-3.11-fork/modules/wikipedia.py
2020-01-29 17:23:38 +00:00

33 lines
1.3 KiB
Python

#--depends-on commands
from src import ModuleManager, utils
URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php"
class Module(ModuleManager.BaseModule):
@utils.hook("received.command.wi", alias_of="wiki")
@utils.hook("received.command.wiki", alias_of="wikipedia")
@utils.hook("received.command.wikipedia")
@utils.kwarg("help", "Get information from wikipedia")
@utils.spec("!<term>lstring")
def wikipedia(self, event):
page = utils.http.request(URL_WIKIPEDIA, get_params={
"action": "query", "prop": "extracts|info", "inprop": "url",
"titles": event["spec"][0], "exintro": "", "explaintext": "",
"exchars": "500", "redirects": "", "format": "json"}).json()
if page:
pages = page["query"]["pages"]
article = list(pages.items())[0][1]
if not "missing" in article:
title, info = article["title"], article["extract"]
title = article["title"]
info = utils.parse.line_normalise(article["extract"])
url = article["fullurl"]
event["stdout"].write("%s: %s - %s" % (title, info, url))
else:
event["stderr"].write("No results found")
else:
raise utils.EventResultsError()