def translate(self, text, proxies=None, **kwargs): params = { "text": text, "format": "plain", "lang": self.target if self.source == "auto" else "{}-{}".format(self.source, self.target), "key": self.api_key } try: url = self.__base_url.format(version=self.api_version, endpoint="translate") response = requests.post(url, data=params, proxies=proxies) except ConnectionError: raise ServerException(503) else: response = response.json() if response['code'] == 429: raise TooManyRequests() if response['code'] != 200: raise ServerException(response['code']) if not response['text']: raise TranslationNotFound() return response['text']
def translate(self, text, **kwargs): """ function that uses google translate to translate a text @param text: desired text to translate @return: str: translated text """ if self._validate_payload(text): text = text.strip() if self.payload_key: self._url_params[self.payload_key] = text response = requests.get(self.__base_url, params=self._url_params, headers={'User-agent': 'your bot 0.1'}) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: # print("status code", response.status_code) raise RequestError() soup = BeautifulSoup(response.text, 'html.parser') element = soup.find(self._element_tag, self._element_query) if not element: element = soup.find(self._element_tag, self._alt_element_query) if not element: raise TranslationNotFound(text) return element.get_text(strip=True)
def translate(self, word, return_all=False, **kwargs): """ function that uses PONS to translate a word @param word: word to translate @type word: str @param return_all: set to True to return all synonym of the translated word @type return_all: bool @return: str: translated word """ if self._validate_payload(word, max_chars=50): url = "{}{}-{}/{}".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: raise RequestError() soup = BeautifulSoup(response.text, 'html.parser') elements = soup.findAll(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(word) filtered_elements = [] for el in elements: temp = '' for e in el.findAll('a'): if e.parent.name == 'div': if e and "/translate/{}-{}/".format( self._target, self._source) in e.get('href'): temp += e.get_text() + ' ' filtered_elements.append(temp) if not filtered_elements: raise ElementNotFoundInGetRequest(word) word_list = [ word for word in filtered_elements if word and len(word) > 1 ] if not word_list: raise TranslationNotFound(word) return word_list if return_all else word_list[0]
def translate(self, text, **kwargs): """ function that uses google translate to translate a text @param text: desired text to translate @return: str: translated text """ if self._validate_payload(text): text = text.strip() if self.payload_key: self._url_params[self.payload_key] = text response = requests.get(self.__base_url, params=self._url_params, headers={'User-agent': 'your bot 0.1'}) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: raise RequestError() soup = BeautifulSoup(response.text, 'html.parser') element = soup.find(self._element_tag, self._element_query) if not element: element = soup.find(self._element_tag, self._alt_element_query) if not element: raise TranslationNotFound(text) if element.get_text(strip=True) == text.strip(): to_translate_alpha = ''.join(ch for ch in text.strip() if ch.isalnum()) translated_alpha = ''.join( ch for ch in element.get_text(strip=True) if ch.isalnum()) if to_translate_alpha and translated_alpha and to_translate_alpha == translated_alpha: self._url_params["tl"] = self._target if "hl" not in self._url_params: return text.strip() del self._url_params["hl"] return self.translate(text) else: return element.get_text(strip=True)
def translate(self, word, return_all=False, **kwargs): """ function that uses linguee to translate a word @param word: word to translate @type word: str @param return_all: set to True to return all synonym of the translated word @type return_all: bool @return: str: translated word """ if self._validate_payload(word, max_chars=50): # %s-%s/translation/%s.html url = "{}{}-{}/translation/{}.html".format(self.__base_url, self._source, self._target, word) url = requote_uri(url) response = requests.get(url) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: raise RequestError() soup = BeautifulSoup(response.text, 'html.parser') elements = soup.find_all(self._element_tag, self._element_query) if not elements: raise ElementNotFoundInGetRequest(elements) filtered_elements = [] for el in elements: try: pronoun = el.find('span', { 'class': 'placeholder' }).get_text(strip=True) except AttributeError: pronoun = '' filtered_elements.append( el.get_text(strip=True).replace(pronoun, '')) if not filtered_elements: raise TranslationNotFound(word) return filtered_elements if return_all else filtered_elements[0]
def translate(self, text, return_all=False, **kwargs): """ function that uses the mymemory translator to translate a text @param text: desired text to translate @type text: str @param return_all: set to True to return all synonym/similars of the translated text @return: str or list """ if self._validate_payload(text, max_chars=500): text = text.strip() if self.payload_key: self._url_params[self.payload_key] = text if self.email: self._url_params['de'] = self.email response = requests.get(self.__base_url, params=self._url_params, headers=self.headers, proxies=self.proxies, timeout=self.timeout) if response.status_code == 429: raise TooManyRequests() if response.status_code != 200: raise RequestError() data = response.json() if not data: TranslationNotFound(text) translation = data.get('responseData').get('translatedText') if translation: return translation elif not translation: all_matches = data.get('matches') matches = (match['translation'] for match in all_matches) next_match = next(matches) return next_match if not return_all else list(all_matches)