h/plugins/wikipedia.py

58 lines
1.4 KiB
Python
Raw Normal View History

2009-03-19 23:13:22 +00:00
'''Searches wikipedia and returns first sentence of article
Scaevolus 2009'''
import urllib
from lxml import etree
import re
from util import hook
2009-03-19 23:13:22 +00:00
api_prefix = "http://en.wikipedia.org/w/api.php"
search_url = api_prefix + "?action=opensearch&search=%s&format=xml"
paren_re = re.compile('\s*\(.*\)$')
2009-04-03 18:40:53 +00:00
@hook.command(hook='w(\s+.*|$)')
2009-03-19 23:13:22 +00:00
@hook.command
def wiki(query):
'''.w/.wiki <phrase> -- gets first sentence of wikipedia ''' \
'''article on <phrase>'''
2009-04-03 18:40:53 +00:00
if not query.strip():
return wiki.__doc__
q = search_url % (urllib.quote(query.strip(), safe=''))
2009-03-19 23:13:22 +00:00
x = etree.parse(q)
ns = '{http://opensearch.org/searchsuggest2}'
items = x.findall(ns + 'Section/' + ns + 'Item')
2009-03-24 22:53:56 +00:00
if items == []:
if x.find('error') is not None:
return 'error: %(code)s: %(info)s' % x.find('error').attrib
else:
return 'no results found'
2009-03-24 22:53:56 +00:00
2009-03-19 23:13:22 +00:00
def extract(item):
return [item.find(ns + x).text for x in
2009-03-19 23:13:22 +00:00
('Text', 'Description', 'Url')]
2009-03-19 23:13:22 +00:00
title, desc, url = extract(items[0])
if 'may refer to' in desc:
title, desc, url = extract(items[1])
2009-03-19 23:13:22 +00:00
title = paren_re.sub('', title)
if title.lower() not in desc.lower():
desc = title + desc
2009-03-19 23:13:22 +00:00
desc = re.sub('\s+', ' ', desc).strip() #remove excess spaces
if len(desc) > 300:
desc = desc[:300] + '...'
return '%s -- %s' % (desc, url)