Last active
August 11, 2023 08:58
-
-
Save kkew3/42a2c7caa575ba2502607d9396bf762f to your computer and use it in GitHub Desktop.
Python script that invokes Google translation, depending on `requests` 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
import json | |
import sys | |
import requests | |
tl, query = sys.argv[1:] | |
sl = 'auto' | |
ua = ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) ' | |
'Gecko/20100101 Firefox/109.0') | |
query = query.strip() | |
# json response from Google transaltion server | |
jr = requests.get( | |
'https://translate.googleapis.com/translate_a/single', | |
params={ | |
'client': 'gtx', | |
'sl': sl, | |
'tl': tl, | |
'dt': 't', | |
'q': query, | |
}, | |
headers={ | |
'user-agent': ua, | |
}).json() | |
# the translation result | |
tr = ''.join(x[0] for x in jr[0]) | |
print(tr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment