2013-09-04 18:30:04 +08:00
""" Searches wikipedia and returns first sentence of article
Scaevolus 2009 """
2011-11-20 22:23:31 +13:00
import re
2012-10-13 11:49:42 +13:00
from util import hook , http , text
2011-11-20 22:23:31 +13: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 18:30:04 +08:00
""" wiki <phrase> -- Gets first sentence of Wikipedia article on <phrase>. """
2011-11-20 22:23:31 +13:00
x = http . get_xml ( search_url , search = inp )
ns = ' { http://opensearch.org/searchsuggest2} '
items = x . findall ( ns + ' Section/ ' + ns + ' Item ' )
2013-09-04 18:30:04 +08:00
if not items :
2011-11-20 22:23:31 +13:00
if x . find ( ' error ' ) is not None :
return ' error: %(code)s : %(info)s ' % x . find ( ' error ' ) . attrib
else :
2012-02-20 10:09:58 +13:00
return ' No results found. '
2011-11-20 22:23:31 +13:00
def extract ( item ) :
return [ item . find ( ns + x ) . text for x in
2014-02-14 17:03:08 +13:00
( ' Text ' , ' Description ' , ' Url ' ) ]
2011-11-20 22:23:31 +13: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-28 18:15:39 -07:00
desc = u ' ' . join ( desc . split ( ) ) # remove excess spaces
2012-04-30 02:02:14 +12:00
2013-08-20 10:55:58 +12:00
desc = text . truncate_str ( desc , 200 )
2011-11-20 22:23:31 +13:00
2014-03-28 18:15:39 -07:00
return u ' {} :: {} ' . format ( desc , http . quote ( url , ' :/ ' ) )