2012-10-18 20:37:38 +02:00
|
|
|
import re
|
|
|
|
|
2014-02-14 04:36:57 +01:00
|
|
|
from util import hook, http, text
|
|
|
|
|
|
|
|
|
2014-02-12 22:10:34 +01:00
|
|
|
api_url = "http://minecraft.gamepedia.com/api.php?action=opensearch"
|
2014-02-12 22:41:01 +01:00
|
|
|
mc_url = "http://minecraft.gamepedia.com/"
|
2012-10-18 20:37:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command
|
|
|
|
def mcwiki(inp):
|
2013-09-04 12:30:04 +02:00
|
|
|
"""mcwiki <phrase> -- Gets the first paragraph of
|
|
|
|
the Minecraft Wiki article on <phrase>."""
|
2012-10-18 20:37:38 +02:00
|
|
|
|
2014-02-12 22:54:20 +01:00
|
|
|
try:
|
|
|
|
j = http.get_json(api_url, search=inp)
|
|
|
|
except (http.HTTPError, http.URLError) as e:
|
|
|
|
return "Error fetching search results: {}".format(e)
|
|
|
|
except ValueError as e:
|
|
|
|
return "Error reading search results: {}".format(e)
|
2013-02-06 12:35:03 +01:00
|
|
|
|
2012-10-18 20:37:38 +02:00
|
|
|
if not j[1]:
|
|
|
|
return "No results found."
|
2014-02-12 22:41:01 +01:00
|
|
|
|
|
|
|
# we remove items with a '/' in the name, because
|
2014-02-13 03:09:22 +01:00
|
|
|
# gamepedia uses sub-pages for different languages
|
2014-02-12 22:41:01 +01:00
|
|
|
# for some stupid reason
|
|
|
|
items = [item for item in j[1] if not "/" in item]
|
|
|
|
|
|
|
|
if items:
|
|
|
|
article_name = items[0].replace(' ', '_').encode('utf8')
|
|
|
|
else:
|
|
|
|
# there are no items without /, just return a / one
|
|
|
|
article_name = j[1][0].replace(' ', '_').encode('utf8')
|
2012-10-18 20:37:38 +02:00
|
|
|
|
2013-02-06 12:35:03 +01:00
|
|
|
url = mc_url + http.quote(article_name, '')
|
2014-02-12 22:54:20 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
page = http.get_html(url)
|
|
|
|
except (http.HTTPError, http.URLError) as e:
|
|
|
|
return "Error fetching wiki page: {}".format(e)
|
2012-10-18 20:37:38 +02:00
|
|
|
|
|
|
|
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)
|
2013-08-20 00:55:58 +02:00
|
|
|
summary = text.truncate_str(summary, 200)
|
2014-02-12 21:36:28 +01:00
|
|
|
return u"{} :: {}".format(summary, url)
|
2012-10-18 20:37:38 +02:00
|
|
|
|
2014-02-12 22:41:01 +01:00
|
|
|
# this shouldn't happen
|
2012-10-18 20:37:38 +02:00
|
|
|
return "Unknown Error."
|