wikipedia.py: If the command is used without arguments, fetch a random page

This commit is contained in:
Chris Swetenham 2014-05-21 20:44:35 +08:00 committed by Ryan Hitchman
parent 2fd1def5f4
commit f4accb15e5
1 changed files with 6 additions and 2 deletions

View File

@ -5,19 +5,23 @@ import re
from util import hook, http
api_prefix = "http://en.wikipedia.org/w/api.php"
search_url = api_prefix + "?action=opensearch&format=xml"
random_url = api_prefix + "?action=query&format=xml&list=random&rnlimit=1&rnnamespace=0"
paren_re = re.compile('\s*\(.*\)$')
@hook.command('w')
@hook.command
@hook.command(autohelp=False)
def wiki(inp):
'''.w/.wiki <phrase> -- gets first sentence of wikipedia ''' \
'''article on <phrase>'''
if inp == "":
r = http.get_xml(random_url)
inp = r.find('.//page').get('title')
x = http.get_xml(search_url, search=inp)
ns = '{http://opensearch.org/searchsuggest2}'