Created
May 13, 2016 06:28
-
-
Save xecgr/6ef053da2e9f2eaa6ec3481290d43fbf to your computer and use it in GitHub Desktop.
This file contains hidden or 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, urllib, json, urllib3 | |
urllib3.disable_warnings() | |
default_words = ['hola', 'adios'] | |
default_lang = 'es' | |
trans_langs = ['en', 'fr', 'it'] | |
key = "AIzaSyBtfSp9TSlUDCNJ0jTwFc-PelOc24-LuzM" | |
result = { | |
default_lang : default_words | |
} | |
headers = { | |
'Referer': 'https://cloud.google.com/translate/' | |
} | |
# get capturing sample traduction form https://cloud.google.com/translate/ | |
url = 'https://www.googleapis.com/language/translate/v2/' | |
query_string = { | |
'key' : key | |
} | |
for lang in trans_langs: | |
result[lang] = [] | |
query_string['target'] = lang | |
query_string['source'] = default_lang | |
for word in default_words: | |
query_string['q'] = word | |
_query_string = urllib.urlencode(query_string) | |
r = requests.get('{}?{}'.format(url, _query_string), headers=headers) | |
result[lang].append(json.loads(r.text)['data']['translations'][0]['translatedText']) | |
for lang,res in result.iteritems(): | |
print lang,res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment