bitbot-3.11-fork/modules/wikipedia.py

36 lines
1.3 KiB
Python
Raw Normal View History

#--depends-on commands
from src import ModuleManager, utils
2016-05-04 12:30:31 +00:00
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", min_args=1)
2016-05-04 12:30:31 +00:00
def wikipedia(self, event):
"""
:help: Get information from wikipedia
:usage: <term>
"""
page = utils.http.request(URL_WIKIPEDIA, get_params={
2019-11-26 14:18:18 +00:00
"action": "query", "prop": "extracts|info", "inprop": "url",
"titles": event["args"], "exintro": "", "explaintext": "",
"exchars": "500", "redirects": "", "format": "json"}).json()
2016-05-04 12:30:31 +00:00
if page:
pages = page["query"]["pages"]
article = list(pages.items())[0][1]
if not "missing" in article:
2016-05-04 12:30:31 +00:00
title, info = article["title"], article["extract"]
2019-11-26 14:18:18 +00:00
title = article["title"]
info = utils.parse.line_normalise(article["extract"])
url = article["fullurl"]
event["stdout"].write("%s: %s - %s" % (title, info, url))
2016-05-04 12:30:31 +00:00
else:
event["stderr"].write("No results found")
else:
raise utils.EventResultsError()
2016-05-04 12:30:31 +00:00