Created
October 15, 2012 20:39
-
-
Save zerko/3895286 to your computer and use it in GitHub Desktop.
hello, lor
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, re, sys | |
MAX_DEPTH = 2 | |
def argparser(): | |
return sys.argv[1] | |
def retrieve(host, path): | |
request = httplib.HTTPConnection(host) | |
request.request("GET", path, "", {'User-Agent' : 'Mozilla/5.0'}) | |
response = request.getresponse() | |
if response.status > 200: | |
return '[["{}"],"ERROR"]'.format(response.reason) | |
return response.read() | |
def parse_item(item, depth = 0): | |
if depth > MAX_DEPTH: | |
return '' | |
if type(item) is str: | |
if len(item) == 0: | |
return '' | |
return ("\t" * depth) + item + "\n" | |
result = '' | |
if hasattr(item, '__iter__'): | |
for sub_item in item: | |
result += parse_item(sub_item, depth + 1) | |
return result | |
phrase = argparser() | |
host = 'translate.google.com' | |
URL = 'translate_a/t?client=t&sl=auto&tl=ru&' + urllib.urlencode({'text' : phrase}) | |
response = re.sub(',{2,}', ',', retrieve(host, "/" + URL)) | |
try: | |
translated = eval(response) | |
result = '' | |
for item in translated: | |
if type(item) is str: | |
result = "Translation: " + item + " > ru\n\n" + result | |
break | |
result += parse_item(item, -1) | |
print result | |
except RuntimeError as ex: | |
print "Something went wrong ({}): {}".format(response, ex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment