Created
June 4, 2022 20:50
-
-
Save armanokka/bf686615f17ccca994bc16bd4aaf61a2 to your computer and use it in GitHub Desktop.
Translo API Python library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[Main file] | |
import requests | |
class PyTranslate: | |
"Main class" | |
def __init__(self, token: str): | |
":token: token from Translo API (https://rapidapi.com/armangokka/api/translo)" | |
self.token = token | |
self.url = "https://translo.p.rapidapi.com/api/v3/" | |
def translate(self, translate_text: str, from_lang: str, to_lang: str, fast: bool = False): | |
""" | |
Translate function | |
Arguments: | |
:translate_text: text of translate | |
:from_lang: from the language of the text (CHECK LANG CODE FROM: rapidapi.com/armangokka/api/translo/details) | |
:to_lang: to language of the text (CHECK LANG CODE FROM: rapidapi.com/armangokka/api/translo/details) | |
:fast: fast mode translate | |
:return: | |
""" | |
payload = f"from={from_lang}&to={to_lang}&text={translate_text}&fast={str(fast).lower()}" | |
headers = { | |
"content-type": "application/x-www-form-urlencoded", | |
"X-RapidAPI-Host": "translo.p.rapidapi.com", | |
"X-RapidAPI-Key": self.token, | |
} | |
response = requests.request("POST", self.url + "translate", data=payload, headers=headers) | |
json = response.json() | |
try: | |
return json["translated_text"] | |
except: | |
raise Exception(json["message"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment