Skip to content

Instantly share code, notes, and snippets.

@doge
Created August 15, 2020 02:46
Show Gist options
  • Save doge/8a0cd5a4b6c47a83c0575a0e77042653 to your computer and use it in GitHub Desktop.
Save doge/8a0cd5a4b6c47a83c0575a0e77042653 to your computer and use it in GitHub Desktop.
access the google translate api without an api key
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')
@NB6G
Copy link

NB6G commented Nov 12, 2023

i only get Hola and Hola

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment