Created
May 8, 2012 20:06
-
-
Save billyvg/2638903 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Searches wikipedia and returns first sentence of article | |
Scaevolus 2009''' | |
import re | |
from util import hook, http | |
api_prefix = "http://wiki.guildwars2.com/api.php" | |
search_url = api_prefix + "?action=opensearch&format=xml" | |
paren_re = re.compile('\s*\(.*\)$') | |
@hook.command('gw2') | |
@hook.command | |
def gw2wiki(inp): | |
'''.gw2/.gw2wiki <phrase> -- gets first sentence of wikipedia ''' \ | |
'''article on <phrase>''' | |
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: | |
return 'no results found' | |
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 | |
if len(desc) > 300: | |
desc = desc[:300] + '...' | |
return '%s -- %s' % (desc, http.quote(url, ':/')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment