-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-trans2.py
89 lines (73 loc) · 2.94 KB
/
google-trans2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import grequests
import logging
import json
from googletrans import Translator
from googletrans.utils import format_json
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'}
translator = Translator(service_urls=['translate.google.cn'])
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
filename='log.txt')
logger = logging.getLogger()
def exception_handler(request, exception):
logger.warning('exception when at %s :%s', request.url, exception)
def work(urls):
reqs = (grequests.get(u, verify=True, allow_redirects=True, timeout=4) for u in urls)
res = grequests.map(reqs, exception_handler=exception_handler, size=20)
return res
def total_translate():
file2 = open('en.txt', mode='w', encoding='utf-8')
with open('cn.txt', mode='r', encoding='utf-8') as f:
urls = []
num = 0
for line in f:
line = line.strip()
token = translator.token_acquirer.do(line)
url = "https://translate.google.cn/translate_a/single?client=t&sl=zh-cn&tl=en&hl=en&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&otf=1&ssel=3&tsel=0&kc=1&tk={0}&q={1}".format(
token, line)
urls.append(url)
res = work(urls)
for r in res:
num += 1
if hasattr(r, 'status_code'):
if r.status_code == 200:
try:
a = format_json(r.text)
target = ''.join([d[0] if d[0] else '' for d in a[0]])
source = ''.join([d[1] if d[1] else '' for d in a[0]])
except Exception as e:
logger.error('when format:%s', e)
logger.error('%s\n%s', r.text)
source = ''
target = ''
if len(source) != 0 and len(target) != 0:
file2.write(target + '\n')
else:
file2.write('\n')
else:
file2.write('\n')
logger.info('finish %s sentence, now at %s', len(res), num)
file2.close()
def sentence_translate(line):
line = line.strip()
text = translator.translate(line, src='zh-cn', dest='en').text
return text
def complete_translate():
file1 = open('en.txt', mode='r', encoding='utf-8')
file2 = open('new_en.txt', mode='w', encoding='utf-8')
i = 1
with open('cn.txt', mode='r', encoding='utf-8') as f:
for line in f:
t = file1.readline()
if len(t) == 1: # 'only \n'
text = sentence_translate(line)
file2.write(text + '\n')
else:
file2.write(t)
i += 1
if i % 100 == 0:
print(i)
file1.close()
file2.close()
if __name__ == "__main__":
total_translate()
complete_translate()