Created
August 15, 2020 02:46
-
-
Save doge/8a0cd5a4b6c47a83c0575a0e77042653 to your computer and use it in GitHub Desktop.
access the google translate api without an api key
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
import requests | |
API_URL = "https://translate.googleapis.com/translate_a/single" | |
class Translator(object): | |
def __init__(self, translate_from, translate_to): | |
self.payload = { | |
"client": "gtx", | |
"dt": "t", | |
"sl": translate_from, | |
"tl": translate_to, | |
"q": "" | |
} | |
def translate(self, word): | |
self.payload['q'] = word | |
with requests.Session() as sess: | |
request = sess.post(API_URL, self.payload) | |
post_json = request.json() | |
if request.status_code == 200: | |
return post_json[0][0][0], post_json[0][0][1] | |
translator = Translator("auto", "en") | |
print(translator.translate("Hola")) | |
# ('Hello', 'Hola') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i only get Hola and Hola