import json
import re
import web
r_tr = re.compile(r'(?ims)
]*>.*?
')
r_paragraph = re.compile(r'(?ims)]*>.*?
|]*>.*?')
r_tag = re.compile(r'<(?!!)[^>]+>')
r_whitespace = re.compile(r'[\t\r\n ]+')
r_redirect = re.compile(
r'(?ims)class=.redirectText.>\s*')
s = s.replace('<', '<')
s = s.replace('&', '&')
s = s.replace(' ', ' ')
s = s.replace('"', '"')
return s
@staticmethod
def text(html):
html = r_tag.sub('', html)
html = r_whitespace.sub(' ', html)
return Wiki.unescape(html).strip()
def search(self, term, last=False):
url = self.api.format(term)
bytes = web.get(url)
try:
result = json.loads(bytes)
result = result['query']['search']
if len(result) <= 0:
return None
except ValueError:
return None
term = result[0]['title']
term = term.replace(' ', '_')
snippet = self.text(result[0]['snippet'])
return "{0} - {1}".format(snippet, self.url.format(term))