import
urllib2
from
HandleJs
import
Py4Js
from
translate
import
Translator
import
requests
def
find_last(string,
str
):
last_position
=
-
1
while
True
:
position
=
string.find(
str
,last_position
+
1
)
if
position
=
=
-
1
:
return
last_position
last_position
=
position
def
open_url(url):
headers
=
{
'User-Agent'
:
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'
}
req
=
urllib2.Request(url
=
url,headers
=
headers)
response
=
urllib2.urlopen(req)
data
=
response.read().decode(
'utf-8'
)
return
data
def
translate_core(content,tk, language):
if
len
(content) >
4891
:
print
(
"too long byte >4891"
)
return
content
=
urllib2.quote(content)
if
language
=
=
'de'
:
url
=
"http://translate.google.cn/translate_a/single?client=t"
+
"&sl=de&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca"
+
"&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1"
+
"&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s"
%
(tk,content)
else
:
url
=
"http://translate.google.cn/translate_a/single?client=t"
+
"&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca"
+
"&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1"
+
"&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s"
%
(tk,content)
result
=
open_url(url)
if
len
(content) <
10
:
end
=
result.find(
"\","
)
if
end >
4
:
return
result[
4
:end]
else
:
result_all
=
''
if
language
=
=
'de'
:
result_all
=
result.split(
',null,"de",null,null,'
)[
0
].replace(
'[['
, '
').replace('
]]
', '
]')[
1
:]
else
:
result_all
=
result.split(
',null,"en",null,null,'
)[
0
].replace(
'[['
, '
').replace('
]]
', '
]')[
1
:]
output_cn
=
''
list
=
result_all.split(
'],['
)
for
i
in
range
(
len
(
list
)
-
1
):
end
=
list
[i].find(
"\","
)
tmp_buf
=
list
[i][
1
:end]
output_cn
=
output_cn
+
tmp_buf
return
output_cn
def
translate_normal(content, language):
js
=
Py4Js()
tk
=
js.getTk(content)
cn_buf
=
translate_core(content,tk, language)
return
cn_buf
def
translate_cn(content, language):
LEN_LIMIT
=
4891
all_len
=
len
(content)
print
(
'en:'
+
content)
if
all_len > LEN_LIMIT:
content_cn
=
''
while
True
:
content_limit
=
content[
0
:LEN_LIMIT]
limit_end
=
find_last(content_limit,
'.'
)
+
1
if
limit_end
=
=
0
:
limit_end
=
find_last(content_limit,
' '
)
+
1
if
limit_end
=
=
0
:
limit_end
=
LEN_LIMIT
content_en
=
content[
0
:limit_end]
leave_len
=
all_len
-
limit_end
if
content_en
=
=
'':
break
;
content_cn
=
content_cn
+
translate_normal(content_en, language);
content
=
content[limit_end:]
return
content_cn
else
:
return
translate_normal(content, language)
def
translate_cn_api(content):
translator
=
Translator(to_lang
=
"zh"
)
translation
=
translator.translate(content)
return
translation
if
__name__
=
=
"__main__"
:
content
=
content
=
content
=
language
=
'en'
test
=
translate_cn(content.replace(
'\n'
, ''), language)
print
(
'ok:'
+
test)