summaryrefslogtreecommitdiff
path: root/ATRIbot/plugins/Module/google_translate
diff options
context:
space:
mode:
authorKyomotoi <[email protected]>2020-07-17 21:36:55 +0800
committerKyomotoi <[email protected]>2020-07-17 21:36:55 +0800
commit77514e99f0308ed90dc031810f71c67771971878 (patch)
tree1611339c44a607dc313c11500c5b0284431fb623 /ATRIbot/plugins/Module/google_translate
parent0efc5d2b0f3d7395715c30bd453d0c4a78d6101d (diff)
downloadATRI-77514e99f0308ed90dc031810f71c67771971878.tar.gz
ATRI-77514e99f0308ed90dc031810f71c67771971878.tar.bz2
ATRI-77514e99f0308ed90dc031810f71c67771971878.zip
[FIX]
Diffstat (limited to 'ATRIbot/plugins/Module/google_translate')
-rw-r--r--ATRIbot/plugins/Module/google_translate/__init__.py123
-rw-r--r--ATRIbot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pycbin0 -> 3205 bytes
2 files changed, 123 insertions, 0 deletions
diff --git a/ATRIbot/plugins/Module/google_translate/__init__.py b/ATRIbot/plugins/Module/google_translate/__init__.py
new file mode 100644
index 0000000..b1d0c43
--- /dev/null
+++ b/ATRIbot/plugins/Module/google_translate/__init__.py
@@ -0,0 +1,123 @@
+# -*- coding:utf-8 -*-
+import re
+import requests
+import demjson
+from pprint import pformat, pprint
+from urllib.parse import urlencode
+
+
+def int_overflow(val):
+ maxint = 2147483647
+ if not -maxint - 1 <= val <= maxint:
+ val = (val + (maxint + 1)) % (2 * (maxint + 1)) - maxint - 1
+ return val
+
+
+def ansii(a):
+ return a.encode('gbk')
+
+
+def kr(a: int, b):
+ c = 0
+ b = ansii(b)
+ while c < len(b) - 2:
+ d = b[c + 2]
+ d = d - 87 if ansii("a")[0] <= d else int(chr(d))
+ d = a >> d if ansii("+")[0] == b[c + 1] else a << d
+ d = int_overflow(d)
+ a = a + d & 4294967295 if ansii("+")[0] == b[c] else a ^ d
+ c += 3
+ return int_overflow(a)
+
+
+def mr(q, TKK):
+ e = q.encode()
+ d = str(TKK).split('.')
+ a = int(d[0])
+ b = int(d[0])
+
+ for f in e:
+ a += f
+ a = kr(a, "+-a^+6")
+ a = kr(a, "+-3^+b+-f")
+ a &= 0xffffffff # 出错了,转回无符号
+ a ^= (int(d[1]) or 0)
+ if 0 > a:
+ a = (a & 2147483647) + 2147483648
+ a %= 1E6
+ a = int(a)
+
+ # c = '&tk='
+ # return c + (str(a) + "." + str(a ^ b))
+ return (str(a) + "." + str(a ^ b))
+
+
+session = requests.session()
+
+
+def translate(q='hello', source='en', to='zh-CN', tkk=None):
+ """
+ 限制最大5000,按utf-8算,一个汉字算1个,1个英文算一个,超过会失败
+ """
+ if not tkk:
+ tkk = '426151.3141811846'
+ tk = mr(q, tkk)
+ params = {
+ 'client': 't',
+ 'sl': source,
+ 'tl': to,
+ 'hl': source,
+ 'dt': [
+ 'at', 'qca', 'rw', 'rm', 'ss', 't'
+ ],
+ 'tk': tk,
+ 'ie': 'UTF-8',
+ 'oe': 'UTF-8',
+ 'pc': 1,
+ 'kc': 1,
+ 'ssel': 0,
+ 'otf': 1
+ }
+ data = {
+ 'q': q
+ }
+ headers = {
+ 'Referer': 'https://translate.google.cn/',
+ 'Host': 'translate.google.cn',
+ }
+ resp = requests.post('https://translate.google.cn/translate_a/single', params=params, data=data, headers=headers)
+ if resp.status_code == 200:
+ resp.encoding = 'utf-8'
+ data = resp.json()
+
+ result = []
+ result.append(''.join(map(lambda x: x[0], data[0][:-1])))
+ result.append(data[0][-1][-1])
+ return result
+ else:
+ return None
+
+
+def ref_words(q='hello', source='en', to='zh-CN'):
+ params = {
+ 'q': q,
+ 'client': 'translate-web',
+ 'ds': 'translate',
+ 'hl': source,
+ 'requiredfields': f'tl:{to}',
+ 'callback': 'window.google.ref_words'
+ }
+ url = 'https://clients1.google.com/complete/search?'
+ headers = {
+ 'Referer': 'https://translate.google.cn/',
+ 'Host': 'clients1.google.cn',
+ }
+ resp = session.get(url, params=params, headers=headers)
+ if resp.status_code == 200:
+ resp.encoding = 'utf-8'
+ result = re.search(r'window.google.ref_words\((.*)\)', resp.text).group(1)
+ json_data = demjson.decode(result)
+ data_list = list(map(lambda x: x[0], json_data[1]))
+ return data_list
+ else:
+ return None \ No newline at end of file
diff --git a/ATRIbot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pyc b/ATRIbot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pyc
new file mode 100644
index 0000000..c9cca07
--- /dev/null
+++ b/ATRIbot/plugins/Module/google_translate/__pycache__/__init__.cpython-37.pyc
Binary files differ