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/disabled_stuff/wikipedia.py

50 lines
1.2 KiB
Python
Raw Permalink Normal View History

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
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
2014-02-14 05:03:08 +01:00
('Text', 'Description', 'Url')]
2011-11-20 10:23:31 +01:00
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
2014-03-29 02:15:39 +01:00
desc = u' '.join(desc.split()) # 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
2014-03-29 02:15:39 +01:00
return u'{} :: {}'.format(desc, http.quote(url, ':/'))