2013-09-04 12:30:04 +02:00
|
|
|
"""Searches wikipedia and returns first sentence of article
|
|
|
|
Scaevolus 2009"""
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2012-10-13 00:49:42 +02:00
|
|
|
from util import hook, http, text
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
api_prefix = "http://en.wikipedia.org/w/api.php"
|
|
|
|
search_url = api_prefix + "?action=opensearch&format=xml"
|
|
|
|
|
|
|
|
paren_re = re.compile('\s*\(.*\)$')
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command('w')
|
|
|
|
@hook.command
|
|
|
|
def wiki(inp):
|
2013-09-04 12:30:04 +02:00
|
|
|
"""wiki <phrase> -- Gets first sentence of Wikipedia article on <phrase>."""
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
x = http.get_xml(search_url, search=inp)
|
|
|
|
|
|
|
|
ns = '{http://opensearch.org/searchsuggest2}'
|
|
|
|
items = x.findall(ns + 'Section/' + ns + 'Item')
|
|
|
|
|
2013-09-04 12:30:04 +02:00
|
|
|
if not items:
|
2011-11-20 10:23:31 +01:00
|
|
|
if x.find('error') is not None:
|
|
|
|
return 'error: %(code)s: %(info)s' % x.find('error').attrib
|
|
|
|
else:
|
2012-02-19 22:09:58 +01:00
|
|
|
return 'No results found.'
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
def extract(item):
|
|
|
|
return [item.find(ns + x).text for x in
|
|
|
|
('Text', 'Description', 'Url')]
|
|
|
|
|
|
|
|
title, desc, url = extract(items[0])
|
|
|
|
|
|
|
|
if 'may refer to' in desc:
|
|
|
|
title, desc, url = extract(items[1])
|
|
|
|
|
|
|
|
title = paren_re.sub('', title)
|
|
|
|
|
|
|
|
if title.lower() not in desc.lower():
|
|
|
|
desc = title + desc
|
|
|
|
|
|
|
|
desc = re.sub('\s+', ' ', desc).strip() # remove excess spaces
|
2012-04-29 16:02:14 +02:00
|
|
|
|
2013-08-20 00:55:58 +02:00
|
|
|
desc = text.truncate_str(desc, 200)
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2013-09-05 04:36:25 +02:00
|
|
|
return '{} :: {}'.format(desc, http.quote(url, ':/'))
|