This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/minecraft_wiki.py

30 lines
860 B
Python

from util import hook, http, text
import re
api_url = "http://minecraftwiki.net/api.php?action=opensearch"
mc_url = "http://minecraftwiki.net/wiki/"
@hook.command
def mcwiki(inp):
"""mcwiki <phrase> -- Gets the first paragraph of
the Minecraft Wiki article on <phrase>."""
j = http.get_json(api_url, search=inp)
if not j[1]:
return "No results found."
article_name = j[1][0].replace(' ', '_').encode('utf8')
url = mc_url + http.quote(article_name, '')
page = http.get_html(url)
for p in page.xpath('//div[@class="mw-content-ltr"]/p'):
if p.text_content():
summary = " ".join(p.text_content().splitlines())
summary = re.sub("\[\d+\]", "", summary)
summary = text.truncate_str(summary, 200)
return "{} :: {}".format(summary, url)
return "Unknown Error."