Created
November 24, 2014 06:45
-
-
Save AlexAltea/3f978d8c3f6938a60f3b to your computer and use it in GitHub Desktop.
Simple Google Translate access from Python
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 httplib, urllib, string | |
# Just a quick, dirty function I made before a proper API appeared (e.g. goslate). | |
def translate(IN, OUT, text): | |
text = urllib.quote(text) | |
conn = httplib.HTTPConnection("translate.google.com") | |
conn.request("GET", "/translate_a/t?client=t&text="+text+"&hl="+IN+"&tl="+OUT) | |
res = conn.getresponse().read() | |
res = res[4:string.find(res,",\"\",\"\"]]")] | |
res = string.split(res, "],[") | |
for i in range(len(res)): | |
if i >= 1: res[i] = res[i][1:string.index(res[i], "\",\"")] | |
else: res[i] = res[i][:string.index(res[i], "\",\"")] | |
return "".join(res) | |
print translate("en", "es", "this really works!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment