-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_trans.py
125 lines (105 loc) · 4.25 KB
/
google_trans.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
MIT License
Copyright (c) 2016 Arnaud Aliès
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import sys
import re
import utils
from pygtrans import Translate
if (sys.version_info[0] < 3):
import urllib2
import urllib
import HTMLParser
else:
import html
import urllib.request
import urllib.parse
agent = {'User-Agent':
"Mozilla/4.0 (\
compatible;\
MSIE 6.0;\
Windows NT 5.1;\
SV1;\
.NET CLR 1.1.4322;\
.NET CLR 2.0.50727;\
.NET CLR 3.0.04506.30\
)"}
class GoogleTrans:
def __init__(self, from_lang, to_lang):
self.name = 'google'
self.PROXY_URL = utils.read_config('appconf.ini')['google']['proxy']
self.from_lang = from_lang
self.to_lang = to_lang
def unescape(self, text):
if (sys.version_info[0] < 3):
parser = HTMLParser.HTMLParser()
else:
parser = html
return (parser.unescape(text))
def batch_translate(self, to_translate):
client = Translate(proxies={'https': self.PROXY_URL})
google_res = client.translate(to_translate, target=self.to_lang)
res = []
for g in google_res:
res.append(g.translatedText)
return res
def translate(self, to_translate):
to_language = self.to_lang
from_language = self.from_lang
"""Returns the translation using google translate
you must shortcut the language you define
(French = fr, English = en, Spanish = es, etc...)
if not defined it will detect it or use english by default
Example:
print(translate("salut tu vas bien?", "en"))
hello you alright?
"""
base_link = "http://translate.google.com/m?tl=%s&sl=%s&q=%s"
# print('sys.version_info=' + str(sys.version_info[0]))
if (sys.version_info[0] < 3):
to_translate = urllib.quote_plus(to_translate)
link = base_link % (to_language, from_language, to_translate)
proxy_handler = urllib2.ProxyHandler({'http': self.PROXY_URL, 'https': self.PROXY_URL})
opener = urllib2.build_opener(proxy_handler)
req = urllib2.Request(link, headers=agent)
raw_data = opener.open(req).read()
else:
to_translate = urllib.parse.quote(to_translate)
link = base_link % (to_language, from_language, to_translate)
proxy = urllib.request.ProxyHandler({'http': self.PROXY_URL, 'https': self.PROXY_URL})
opener = urllib.request.build_opener(proxy)
request = urllib.request.Request(link, headers=agent)
resp = opener.open(request)
raw_data = resp.read()
data = raw_data.decode("utf-8")
expr = r'(?s)class="(?:t0|result-container)">(.*?)<'
re_result = re.findall(expr, data)
if (len(re_result) == 0):
result = ""
else:
result = self.unescape(re_result[0])
return (result)
if __name__ == '__main__':
# text = "[76], I want to know why we're here. I never let anyone use some... [21] on me, taking the group away from [78] and her [27]!"
# text = "hello world"
# res = GoogleTrans("auto", "zh-CN").translate(text)
# print(res)
texts = ['hello', 'world']
print(GoogleTrans("auto", "zh-CN").batch_translate(texts))