Update !wiki to use the new wiki

This commit is contained in:
jacob1 2016-05-01 23:13:54 -04:00
parent 2dac49f741
commit 5ef750aa2e
2 changed files with 55 additions and 24 deletions

View File

@ -575,9 +575,10 @@
"command_not_found": "Command not found.", "command_not_found": "Command not found.",
"commands_list": "Commands: {0}", "commands_list": "Commands: {0}",
"admin_commands_list": "Admin Commands: {0}", "admin_commands_list": "Admin Commands: {0}",
"wiki_request_timed_out": "Request to https://github.com/lykoss/lykos/wiki timed out.", "wiki_request_timed_out": "Request to wiki timed out. Try loading the wiki in your browser: https://werewolf.chat",
"wiki_no_open": "Could not open https://github.com/lykoss/lykos/wiki", "wiki_open_failure": "Error loading page information. Try loading the wiki in your browser: https://werewolf.chat",
"wiki_no_role_info": "Could not find information on that role in https://github.com/lykoss/lykos/wiki", "wiki_no_info": "Could not find information about that topic in the wiki: https://werewolf.chat",
"wiki_invalid_page": "Invalid page title. Try opening the wiki at https://werewolf.chat",
"not_an_admin": "You are not an admin.", "not_an_admin": "You are not an admin.",
"fpart_usage": "Usage: fpart <channel>", "fpart_usage": "Usage: fpart <channel>",
"fpart_bot_error": "No, that won't be allowed.", "fpart_bot_error": "No, that won't be allowed.",

View File

@ -37,6 +37,7 @@ import time
import traceback import traceback
import urllib.request import urllib.request
from collections import defaultdict, deque from collections import defaultdict, deque
import json
from datetime import datetime, timedelta from datetime import datetime, timedelta
from oyoyo.parse import parse_nick from oyoyo.parse import parse_nick
@ -718,7 +719,7 @@ def replace(cli, nick, chan, rest):
@cmd("pingif", "pingme", "pingat", "pingpref", pm=True) @cmd("pingif", "pingme", "pingat", "pingpref", pm=True)
def altpinger(cli, nick, chan, rest): def altpinger(cli, nick, chan, rest):
"""Pings you when the number of players reaches your preference. Usage: "pingif <players>". https://github.com/lykoss/lykos/wiki/Pingif""" """Pings you when the number of players reaches your preference. Usage: "pingif <players>". https://werewolf.chat/Pingif"""
players = is_user_altpinged(nick) players = is_user_altpinged(nick)
rest = rest.split() rest = rest.split()
if nick in var.USERS: if nick in var.USERS:
@ -6682,42 +6683,71 @@ def get_help(cli, rnick, chan, rest):
afns.sort() afns.sort()
reply(cli, nick, chan, messages["admin_commands_list"].format(break_long_message(afns, ", ")), private=True) reply(cli, nick, chan, messages["admin_commands_list"].format(break_long_message(afns, ", ")), private=True)
def get_wiki_page(URI):
print(URI)
try:
response = urllib.request.urlopen(URI, timeout=2).read().decode("utf-8", errors="replace")
except (urllib.error.URLError, socket.timeout):
return False, messages["wiki_request_timed_out"]
if not response:
return False, messages["wiki_open_failure"]
parsed = json.loads(response)
if not parsed:
return False, messages["wiki_open_failure"]
return True, parsed
@cmd("wiki", pm=True) @cmd("wiki", pm=True)
def wiki(cli, nick, chan, rest): def wiki(cli, nick, chan, rest):
"""Prints information on roles from the wiki.""" """Prints information on roles from the wiki."""
# no arguments, just print a link to the wiki # no arguments, just print a link to the wiki
if not rest: if not rest:
reply(cli, nick, chan, "https://github.com/lykoss/lykos/wiki") reply(cli, nick, chan, "https://werewolf.chat")
return
# Check for valid page name
if not re.fullmatch("[\w ]+", rest):
reply(cli, nick, chan, messages["wiki_invalid_page"])
return
rest = rest.replace(" ", "_").lower()
# Get suggestions, for autocompletion
URI = "https://werewolf.chat/w/api.php?action=opensearch&format=json&search={0}".format(rest)
success, suggestionjson = get_wiki_page(URI)
if not success:
reply(cli, nick, chan, suggestionjson, private=True)
return
# Parse suggested pages, take the first result
try:
suggestion = suggestionjson[1][0].replace(" ", "_")
except IndexError:
reply(cli, nick, chan, messages["wiki_no_info"], private=True)
return
# Fetch a page from the api, in json format
URI = "https://werewolf.chat/w/api.php?action=query&prop=extracts&exintro=true&explaintext=true&titles={0}&format=json".format(suggestion)
success, pagejson = get_wiki_page(URI)
if not success:
reply(cli, nick, chan, pagejson, private=True)
return return
try: try:
page = urllib.request.urlopen("https://raw.githubusercontent.com/wiki/lykoss/lykos/Home.md", timeout=2).read().decode("ascii", errors="replace") page = pagejson["query"]["pages"].popitem()[1]["extract"]
except (urllib.error.URLError, socket.timeout): except (KeyError, IndexError):
reply(cli, nick, chan, messages["wiki_request_timed_out"], private=True) reply(cli, nick, chan, messages["wiki_no_info"], private=True)
return
if not page:
reply(cli, nick, chan, messages["wiki_no_open"], private=True)
return return
query = re.escape(rest.strip()) # We only want the first paragraph
# look for exact match first, then for a partial match if page.find("\n") >= 0:
match = re.search(r"^##+ ({0})$\r?\n\r?\n^(.*)$".format(query), page, re.MULTILINE + re.IGNORECASE) page = page[:page.find("\n")]
if not match:
match = re.search(r"^##+ ({0}.*)$\r?\n\r?\n^(.*)$".format(query), page, re.MULTILINE + re.IGNORECASE)
if not match:
reply(cli, nick, chan, messages["wiki_no_role_info"], private=True)
return
# wiki links only have lowercase ascii chars, and spaces are replaced with a dash wikilink = "https://werewolf.chat/{0}".format(suggestion.capitalize())
wikilink = "https://github.com/lykoss/lykos/wiki#{0}".format("".join(
x.lower() for x in match.group(1).replace(" ", "-") if x in string.ascii_letters+"-"))
if nick == chan: if nick == chan:
pm(cli, nick, wikilink) pm(cli, nick, wikilink)
pm(cli, nick, break_long_message(match.group(2).split())) pm(cli, nick, break_long_message(page.split()))
else: else:
cli.msg(chan, wikilink) cli.msg(chan, wikilink)
cli.notice(nick, break_long_message(match.group(2).split())) cli.notice(nick, break_long_message(page.split()))
@hook("invite") @hook("invite")
def on_invite(cli, raw_nick, something, chan): def on_invite(cli, raw_nick, something, chan):