def calc(self, event, expression): tree = self._google_scrape_search(expression) nodes = [node for node in tree.findall(".//h2/b")] if len(nodes) == 1: # ElementTree doesn't support inline tags: # May return ASCII unless an encoding is specified. # "utf8" will result in an xml header node = ElementTree.tostring(nodes[0], encoding="utf-8") node = node.decode("utf-8") node = re.sub(r"<sup>(.*?)</sup>", lambda x: u"^" + x.group(1), node) node = re.sub(r"<.*?>", "", node) node = re.sub(r"(\d)\s+(\d)", lambda x: x.group(1) + x.group(2), node) node = decode_htmlentities(node) event.addresponse(node) else: event.addresponse(u"%s, Google wasn't interested in calculating that", choice(("Sorry", "Whoops")))
def calc(self, event, expression): tree = self._google_scrape_search(expression) nodes = [node for node in tree.findall('.//h2/b')] if len(nodes) == 1: # ElementTree doesn't support inline tags: # May return ASCII unless an encoding is specified. # "utf8" will result in an xml header node = ElementTree.tostring(nodes[0], encoding='utf-8') node = node.decode('utf-8') node = re.sub(r'<sup>(.*?)</sup>', lambda x: u'^' + x.group(1), node) node = re.sub(r'<.*?>', '', node) node = re.sub(r'(\d)\s+(\d)', lambda x: x.group(1) + x.group(2), node) node = decode_htmlentities(node) event.addresponse(node) else: event.addresponse(u'No result')
def calc(self, event, expression): tree = self._google_scrape_search(expression) nodes = [ node for node in tree.findall('.//h2') if node.get('class') == 'r' ] if len(nodes) == 1: # ElementTree doesn't support inline tags: # May return ASCII unless an encoding is specified. # "utf8" will result in an xml header node = ElementTree.tostring(nodes[0], encoding='utf-8') node = node.decode('utf-8') node = re.sub(r'<sup>(.*?)</sup>', lambda x: u'^' + x.group(1), node) node = re.sub(r'<.*?>', '', node) node = re.sub(r'(\d)\s+(\d)', lambda x: x.group(1) + x.group(2), node) node = decode_htmlentities(node) node = re.sub(r'\s+', ' ', node) event.addresponse(node) else: event.addresponse( u"%s, Google wasn't interested in calculating that", choice(('Sorry', 'Whoops')))