bitbot-3.11-fork/modules/wikipedia.py

29 lines
1 KiB
Python
Raw Normal View History

2016-05-04 12:30:31 +00:00
import Utils
URL_WIKIPEDIA = "https://en.wikipedia.org/w/api.php"
class Module(object):
def __init__(self, bot, events, exports):
2016-05-04 12:30:31 +00:00
self.bot = bot
events.on("received.command").on("wiki", "wi"
2016-05-04 12:30:31 +00:00
).hook(self.wikipedia, min_args=1)
def wikipedia(self, event):
page = Utils.get_url(URL_WIKIPEDIA, get_params={
"action": "query", "prop": "extracts",
"titles": event["args"], "exintro": "",
"explaintext": "", "exchars": "500",
"redirects": "", "format": "json"}, json=True)
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"]
info = info.replace("\n\n", " ").split("\n")[0]
2016-05-04 12:30:31 +00:00
event["stdout"].write("%s: %s" % (title, info))
else:
event["stderr"].write("No results found")
else:
event["stderr"].write("Failed to load results")