コード例 #1
0
ファイル: translate.py プロジェクト: thomasryck/xgoogle
    def detect(self, message):
        """
        Given a 'message' detects its language.
        Returns Language object.
        """

        message = quote_plus(message)
        real_url = LanguageDetector.detect_url % {'message': message}

        try:
            detection = self.browser.get_page(real_url)
            data = json.loads(detection)

            if data['responseStatus'] != 200:
                raise DetectionError("Failed detecting language: %s" %
                                     data['responseDetails'])

            rd = data['responseData']
            return Language(rd['language'], rd['confidence'], rd['isReliable'])

        except BrowserError(e):
            raise DetectionError(
                "Failed detecting language (getting %s failed): %s" %
                (e.url, e.error))
        except ValueError(e):
            raise DetectionErrro(
                "Failed detecting language (json failed): %s" % e.message)
        except KeyError(e):
            raise DetectionError(
                "Failed detecting language, response didn't contain the necessary data"
            )

        return None
コード例 #2
0
ファイル: googlesets.py プロジェクト: thomasryck/xgoogle
    def _get_results_page(self, set_type):
        if set_type == LARGE_SET:
            url = GoogleSets.URL_LARGE
        else:
            url = GoogleSets.URL_SMALL

        safe_items = [urllib.quote_plus(i) for i in self.items]
        blank_items = 5 - len(safe_items)
        if blank_items > 0:
            safe_items += ['']*blank_items

        safe_url = url % tuple(safe_items)

        try:
            page = self.browser.get_page(safe_url)
        except BrowserError(e):
            raise GSError("Failed getting %s: %s" % (e.url, e.error))

        return BeautifulSoup(page)
コード例 #3
0
ファイル: translate.py プロジェクト: thomasryck/xgoogle
    def translate(self, message, lang_to='en', lang_from=''):
        """
        Given a 'message' translate it from 'lang_from' to 'lang_to'.
        If 'lang_from' is empty, auto-detects the language.
        Returns the translated message.
        """

        if lang_to not in _languages:
            raise TranslationError("Language %s is not supported as lang_to." %
                                   lang_to)
        if lang_from not in _languages and lang_from != '':
            raise TranslationError(
                "Language %s is not supported as lang_from." % lang_from)

        message = quote_plus(message)
        real_url = Translator.translate_url % {
            'message': message,
            'from': lang_from,
            'to': lang_to
        }

        try:
            translation = self.browser.get_page(real_url)
            data = json.loads(translation)

            if data['responseStatus'] != 200:
                raise TranslationError("Failed translating: %s" %
                                       data['responseDetails'])

            return data['responseData']['translatedText']
        except BrowserError(e):
            raise TranslationError(
                "Failed translating (getting %s failed): %s" %
                (e.url, e.error))
        except ValueError(e):
            raise TranslationError("Failed translating (json failed): %s" %
                                   e.message)
        except KeyError(e):
            raise TranslationError(
                "Failed translating, response didn't contain the translation")

        return None
コード例 #4
0
ファイル: realtime.py プロジェクト: lu43n/xgoogle
    def _get_results_page(self):
        if not self.older:
            url = RealtimeSearch.SEARCH_URL
            safe_url = [
                url % {
                    'query': urllib.quote_plus(self.query),
                    'tld': self._tld,
                    'lang': self._lang
                }
            ]
            safe_url = "".join(safe_url)
            self.older = safe_url
        else:
            safe_url = self.older

        self._last_search_url = safe_url
        try:
            page = self.browser.get_page(safe_url)
        except BrowserError(e):
            raise RealtimeSearchError("Failed getting %s: %s" %
                                      (e.url, e.error))
        return BeautifulSoup(page)
コード例 #5
0
ファイル: search.py プロジェクト: dalyk/characters
    def _get_results_page(self):
        if self._page == 0:
            if self._results_per_page == 10:
                url = GoogleSearch.SEARCH_URL_0
            else:
                url = GoogleSearch.SEARCH_URL_1
        else:
            if self._results_per_page == 10:
                url = GoogleSearch.NEXT_PAGE_0
            else:
                url = GoogleSearch.NEXT_PAGE_1

        safe_url = [
            url % {
                'query': urllib.quote_plus(self.query),
                'start': self._page * self._results_per_page,
                'num': self._results_per_page,
                'tld': self._tld,
                'lang': self._lang
            }
        ]

        # possibly extend url with optional properties
        if self._first_indexed_in_previous:
            safe_url.extend(["&as_qdr=", self._first_indexed_in_previous])
        if self._filetype:
            safe_url.extend(["&as_filetype=", self._filetype])

        safe_url = "".join(safe_url)
        self._last_search_url = safe_url

        try:
            page = self.browser.get_page(safe_url)
        except BrowserError(e):
            raise SearchError("Failed getting %s: %s" % (e.url, e.error))

        return BeautifulSoup(page)
コード例 #6
0
    def _get_results_page(self):
        if self._page == 0:
            if self._results_per_page == 10:
                url = SponsoredLinks.SEARCH_URL_0
            else:
                url = SponsoredLinks.SEARCH_URL_1
        else:
            if self._results_per_page == 10:
                url = SponsoredLinks.NEXT_PAGE_0
            else:
                url = SponsoredLinks.NEXT_PAGE_1

        safe_url = url % {
            'query': urllib.quote_plus(self.query),
            'start': self._page * self._results_per_page,
            'num': self._results_per_page
        }

        try:
            page = self.browser.get_page(safe_url)
        except BrowserError(e):
            raise SLError("Failed getting %s: %s" % (e.url, e.error))

        return BeautifulSoup(page)