コード例 #1
0
ファイル: languages.py プロジェクト: B-Rich/ibid-1
    def _translate (self, event, phrase, src_lang, dest_lang):
        params = {
            'v': '1.0',
            'q': phrase,
            'langpair': src_lang + '|' + dest_lang,
        }
        if self.api_key:
            params['key'] = self.api_key

        headers = {'referer': self.referer}

        response = json_webservice(
            'http://ajax.googleapis.com/ajax/services/language/translate',
            params, headers)

        if response['responseStatus'] == 200:
            translated = unicode(decode_htmlentities(
                response['responseData']['translatedText']))
            return (translated, src_lang or
                    response['responseData']['detectedSourceLanguage'])
        else:
            errors = {
                'invalid translation language pair':
                    u"I don't know that language",
                'invalid text':
                    u"there's not much to go on",
                 'could not reliably detect source language':
                    u"I'm not sure what language that was",
            }

            msg = errors.get(response['responseDetails'],
                            response['responseDetails'])

            raise TranslationException(msg)
コード例 #2
0
ファイル: languages.py プロジェクト: shoosen/ibid
    def _translate (self, event, phrase, src_lang, dest_lang):
        params = {
            'v': '1.0',
            'q': phrase,
            'langpair': src_lang + '|' + dest_lang,
        }
        if self.api_key:
            params['key'] = self.api_key

        headers = {'referer': self.referer}

        response = json_webservice(
            'http://ajax.googleapis.com/ajax/services/language/translate',
            params, headers)

        if response['responseStatus'] == 200:
            translated = unicode(decode_htmlentities(
                response['responseData']['translatedText']))
            return (translated, src_lang or
                    response['responseData']['detectedSourceLanguage'])
        else:
            errors = {
                'invalid translation language pair':
                    u"I don't know that language",
                'invalid text':
                    u"there's not much to go on",
                 'could not reliably detect source language':
                    u"I'm not sure what language that was",
            }

            msg = errors.get(response['responseDetails'],
                            response['responseDetails'])

            raise TranslationException(msg)
コード例 #3
0
    def remote_update(self, service, id):
        status = json_webservice('%sstatuses/show/%s.json' %
                                 (service['endpoint'], id))

        return {
            'screen_name': status['user']['screen_name'],
            'text': decode_htmlentities(status['text'])
        }
コード例 #4
0
ファイル: google.py プロジェクト: GertBurger/ibid
    def search(self, event, country, query):
        try:
            items = self._google_api_search(query, country=country)
        except BadStatusLine:
            event.addresponse(u"Google appears to be broken (or more likely, my connection to it)")
            return

        results = []
        for item in items["responseData"]["results"]:
            title = item["titleNoFormatting"]
            results.append(u'"%s" %s' % (decode_htmlentities(title), item["unescapedUrl"]))

        if results:
            event.addresponse(u" :: ".join(results))
        else:
            event.addresponse(u"Wow! Google couldn't find anything")
コード例 #5
0
    def search(self, event, country, query):
        try:
            items = self._google_api_search(query, country=country)
        except BadStatusLine:
            event.addresponse(u"Google appears to be broken (or more likely, my connection to it)")
            return

        results = []
        for item in items["responseData"]["results"]:
            title = item["titleNoFormatting"]
            results.append(u'"%s" %s' % (decode_htmlentities(title), item["unescapedUrl"]))

        if results:
            event.addresponse(u' :: '.join(results))
        else:
            event.addresponse(u"Wow! Google couldn't find anything")
コード例 #6
0
ファイル: google.py プロジェクト: adrianmoisey/ibid
    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")))
コード例 #7
0
    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')
コード例 #8
0
    def remote_latest(self, service, user):
        if service['api'] == 'twitter':
            # Twitter ommits retweets in the JSON and XML results:
            statuses = generic_webservice(
                '%sstatuses/user_timeline/%s.atom' %
                (service['endpoint'], user.encode('utf-8')), {'count': 1})
            tree = ElementTree.fromstring(statuses)
            latest = tree.find('{http://www.w3.org/2005/Atom}entry')
            if latest is None:
                raise self.NoTweetsException(user)
            return {
                'text':
                latest.findtext('{http://www.w3.org/2005/Atom}content').split(
                    ': ', 1)[1],
                'ago':
                ago(datetime.utcnow() - parse_timestamp(
                    latest.findtext('{http://www.w3.org/2005/Atom}published'))
                    ),
                'url': [
                    x for x in latest.getiterator(
                        '{http://www.w3.org/2005/Atom}link')
                    if x.get('type') == 'text/html'
                ][0].get('href'),
            }
        elif service['api'] == 'laconica':
            statuses = json_webservice(
                '%sstatuses/user_timeline/%s.json' %
                (service['endpoint'], user.encode('utf-8')), {'count': 1})
            if not statuses:
                raise self.NoTweetsException(user)
            latest = statuses[0]
            url = '%s/notice/%i' % (service['endpoint'].split(
                '/api/', 1)[0], latest['id'])

        return {
            'text': decode_htmlentities(latest['text']),
            'ago':
            ago(datetime.utcnow() - parse_timestamp(latest['created_at'])),
            'url': url,
        }
コード例 #9
0
    def remote_latest(self, service, user):
        if service['api'] == 'twitter':
            # Twitter ommits retweets in the JSON and XML results:
            statuses = generic_webservice(
                '%sstatuses/user_timeline.rss' % service['endpoint'], {
                    'screen_name': user,
                    'count': 1
                })
            tree = ElementTree.fromstring(statuses)
            latest = tree.find('.//item')
            if latest is None:
                if tree.find('.//error'):
                    log.info('Twitter user_latest returned: %s',
                             tree.findtext('.//error'))
                raise self.NoTweetsException(user)
            return {
                'text':
                latest.findtext('description').split(': ', 1)[1],
                'ago':
                ago(datetime.utcnow() -
                    parse_timestamp(latest.findtext('pubDate'))),
                'url':
                latest.findtext('guid'),
            }
        elif service['api'] == 'laconica':
            statuses = json_webservice(
                '%sstatuses/user_timeline/%s.json' %
                (service['endpoint'], user.encode('utf-8')), {'count': 1})
            if not statuses:
                raise self.NoTweetsException(user)
            latest = statuses[0]
            url = '%s/notice/%i' % (service['endpoint'].split(
                '/api/', 1)[0], latest['id'])

        return {
            'text': decode_htmlentities(latest['text']),
            'ago':
            ago(datetime.utcnow() - parse_timestamp(latest['created_at'])),
            'url': url,
        }
コード例 #10
0
ファイル: social.py プロジェクト: B-Rich/ibid-1
    def remote_latest(self, service, user):
        if service['api'] == 'twitter':
            # Twitter ommits retweets in the JSON and XML results:
            statuses = generic_webservice('%sstatuses/user_timeline.rss'
                                          % service['endpoint'], {
                                              'screen_name': user,
                                              'count': 1
                                          })
            tree = ElementTree.fromstring(statuses)
            latest = tree.find('.//item')
            if latest is None:
                if tree.find('.//error'):
                    log.info('Twitter user_latest returned: %s',
                             tree.findtext('.//error'))
                raise self.NoTweetsException(user)
            return {
                'text': latest.findtext('description')
                        .split(': ', 1)[1],
                'ago': ago(datetime.utcnow() - parse_timestamp(
                    latest.findtext('pubDate'))),
                'url': latest.findtext('guid'),
            }
        elif service['api'] == 'laconica':
            statuses = json_webservice('%sstatuses/user_timeline/%s.json'
                    % (service['endpoint'], user.encode('utf-8')),
                    {'count': 1})
            if not statuses:
                raise self.NoTweetsException(user)
            latest = statuses[0]
            url = '%s/notice/%i' % (service['endpoint'].split('/api/', 1)[0],
                                    latest['id'])

        return {
            'text': decode_htmlentities(latest['text']),
            'ago': ago(datetime.utcnow()
                       - parse_timestamp(latest['created_at'])),
            'url': url,
        }
コード例 #11
0
ファイル: social.py プロジェクト: adrianmoisey/ibid
    def remote_latest(self, service, user):
        if service['api'] == 'twitter':
            # Twitter ommits retweets in the JSON and XML results:
            statuses = generic_webservice('%sstatuses/user_timeline/%s.atom'
                    % (service['endpoint'], user.encode('utf-8')),
                    {'count': 1})
            tree = ElementTree.fromstring(statuses)
            latest = tree.find('{http://www.w3.org/2005/Atom}entry')
            if latest is None:
                raise self.NoTweetsException(user)
            return {
                'text': latest.findtext('{http://www.w3.org/2005/Atom}content')
                        .split(': ', 1)[1],
                'ago': ago(datetime.utcnow() - parse_timestamp(
                    latest.findtext('{http://www.w3.org/2005/Atom}published'))),
                'url': [x for x
                     in latest.getiterator('{http://www.w3.org/2005/Atom}link')
                     if x.get('type') == 'text/html'
                  ][0].get('href'),
            }
        elif service['api'] == 'laconica':
            statuses = json_webservice('%sstatuses/user_timeline/%s.json'
                    % (service['endpoint'], user.encode('utf-8')),
                    {'count': 1})
            if not statuses:
                raise self.NoTweetsException(user)
            latest = statuses[0]
            url = '%s/notice/%i' % (service['endpoint'].split('/api/', 1)[0],
                                    latest['id'])

        return {
            'text': decode_htmlentities(latest['text']),
            'ago': ago(datetime.utcnow()
                       - parse_timestamp(latest['created_at'])),
            'url': url,
        }
コード例 #12
0
ファイル: google.py プロジェクト: vhata/ibid
    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')))
コード例 #13
0
ファイル: social.py プロジェクト: B-Rich/ibid-1
    def remote_update(self, service, id):
        status = json_webservice('%sstatuses/show/%s.json' % (service['endpoint'], id))

        return {'screen_name': status['user']['screen_name'], 'text': decode_htmlentities(status['text'])}