Skip to content

Instantly share code, notes, and snippets.

@samundra
Last active June 10, 2016 10:53
Show Gist options
  • Select an option

  • Save samundra/d07bd2e312f7c3d0b65de4b68c91b22d to your computer and use it in GitHub Desktop.

Select an option

Save samundra/d07bd2e312f7c3d0b65de4b68c91b22d to your computer and use it in GitHub Desktop.
Mutate_google_translate.py
#!/usr/bin/python2
#!encoding = utf-8
# filename: ~/.config/Mutate/scripts
import re
import urllib
import urllib2
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
#text command icon subtext
def has_chinese_charactar(content):
try:
iconvcontent = unicode(content)
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
match = zhPattern.search(iconvcontent)
res = False
if match:
res = True
return res
except:
return True
for i in range(1, len(sys.argv)):
if has_chinese_charactar(sys.argv[i]):
source_languege = "zh-CN"
target_languege = "en"
else:
source_languege = "en"
target_languege = "th" #change final translation language here
url = 'http://translate.google.cn/translate_a/t'
payload = {
'client': 'p',
'text' : sys.argv[i],
'hl' : 'en-EN',
'sl' : source_languege,
'tl' : target_languege,
'multires': '1',
'ssel' : '0',
'tsel' : '0',
'sc' : '1',
'ie' : 'UTF-8',
'oe' : 'UTF-8'
}
response = urllib2.Request(url,urllib.urlencode(payload))
browser = "Mozilla/5.0 (Windows NT 6.1; WOW64)"
response.add_header('User-Agent', browser)
response = urllib2.urlopen(response)
get_page = response.read()
print '[' + get_page + ']'
print 'command=copy'
print 'icon='
print 'subtext='
@fawaf
Copy link
Copy Markdown

fawaf commented Jun 10, 2016

why not submit it back as a PR?

@samundra
Copy link
Copy Markdown
Author

@gnowxilef PR is open 👍 Waiting to be merged.

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