This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/wikipedia.py

51 lines
1.3 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
'''Searches wikipedia and returns first sentence of article
Scaevolus 2009'''
import re
from util import hook, http
2012-04-23 22:51:54 +02:00
from util.text import truncate_words
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):
2012-05-16 05:07:27 +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')
if items == []:
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
2012-04-21 18:26:24 +02:00
desc = truncate_words(desc, 300)
2011-11-20 10:23:31 +01:00
2012-02-19 22:09:58 +01:00
return '%s -- %s' % (desc, http.quote(url, ':/'))