forked from lorniu/go-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgts-engine-google.el
424 lines (372 loc) · 16.9 KB
/
gts-engine-google.el
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
;;; gts-engine-google.el --- Google translate module -*- lexical-binding: t -*-
;; Copyright (C) 2021 lorniu <[email protected]>
;; SPDX-License-Identifier: MIT
;;; Commentary:
;; http://translate.google.com
;;; Code:
(require 'gts-implements)
;;; Components
(defclass gts-google-parser (gts-parser)
((tag :initform "Detail")))
(defclass gts-google-summary-parser (gts-google-parser)
((tag :initform "Summary")))
(defclass gts-google-engine (gts-engine)
((tag :initform "Google")
(base-url :initform "http://translate.googleapis.com")
(sub-url :initform "/translate_a/single")
(token :initform (cons 430675 2721866130) :initarg token) ; hard code
(token-time :initform t)
(token-expired-time :initform (* 30 60))
(parser :initform (gts-google-parser))))
;;; Engine
(defvar gts-google-request-headers '(("Connection" . "Keep-Alive")))
(cl-defmethod gts-gen-url ((engine gts-google-engine) text from to)
"Generate the url with TEXT, FROM and TO. Return a (url text from to) list."
(format "%s%s?%s"
(oref engine base-url)
(oref engine sub-url)
(mapconcat (lambda (p)
(format "%s=%s" (url-hexify-string (car p)) (url-hexify-string (cdr p))))
`(("client" . "gtx")
("ie" . "UTF-8")
("oe" . "UTF-8")
("dt" . "bd")
("dt" . "ex")
("dt" . "ld")
("dt" . "md")
("dt" . "qc")
("dt" . "rw")
("dt" . "rm")
("dt" . "ss")
("dt" . "t")
("dt" . "at")
("pc" . "1")
("otf" . "1")
("srcrom" . "1")
("ssel" . "0")
("tsel" . "0")
("hl" . ,to)
("sl" . ,from)
("tl" . ,to)
("q" . ,text)
("tk" . ,(gts-google-tkk (oref engine token) text)))
"&")))
(cl-defmethod gts-token-available-p ((engine gts-google-engine))
(with-slots (token token-time token-expired-time) engine
(and token
(or (eq token-time t)
(and token-time
(<= (float-time (time-subtract (current-time) token-time))
token-expired-time))))))
(cl-defmethod gts-with-token ((engine gts-google-engine) done fail)
(with-slots (token token-time base-url) engine
(if (gts-token-available-p engine)
(funcall done)
(gts-do-request base-url
:headers gts-google-request-headers
:done (lambda ()
(let ((tk (progn
(re-search-forward ",tkk:'\\([0-9]+\\)\\.\\([0-9]+\\)")
(cons (string-to-number (match-string 1))
(string-to-number (match-string 2))))))
(setf token tk)
(setf token-time (current-time))
(funcall done)))
:fail fail))))
(cl-defmethod gts-translate ((engine gts-google-engine) task rendercb)
(gts-with-token engine
(lambda ()
(with-slots (text from to) task
(gts-do-request (gts-gen-url engine text from to)
:headers gts-google-request-headers
:done (lambda ()
(gts-update-raw task (buffer-string))
(gts-parse (oref engine parser) task)
(funcall rendercb))
:fail (lambda (err)
(gts-render-fail task err)))))
(lambda (err)
(gts-render-fail task
(format "Error when fetching Token-Key, check your network and proxy, or retry later\n\n%s" err)))))
;; tts
(cl-defmethod gts-tts-gen-urls ((engine gts-google-engine) text lang)
"Generate the tts urls for TEXT to LANGUAGE."
(cl-loop with texts = (gts-tts-text-splitter engine text)
for total = (length texts)
for index from 0
for piece in texts
collect
(let ((params `(("ie" . "UTF-8")
("client" . "gtx")
("prev" . "input")
("q" . ,text)
("tl" . ,lang)
("total" . ,(number-to-string total))
("idx" . ,(number-to-string index))
("textlen" . ,(number-to-string (length piece)))
("tk" . ,(gts-google-tkk (oref engine token) piece)))))
(format "%s/translate_tts?%s"
(oref engine base-url)
(mapconcat (lambda (p)
(format "%s=%s"
(url-hexify-string (car p))
(url-hexify-string (cdr p))))
params "&")))))
(cl-defmethod gts-tts-text-splitter ((_ gts-google-engine) text)
"Split TEXT by maxlen at applicable point for translating.
Code from `google-translate', maybe improve it someday."
(let (result (maxlen 200))
(if (or (null maxlen) (<= maxlen 0))
(push text result)
;; split long text?
(with-temp-buffer
(save-excursion (insert text))
;; strategy to split at applicable point
;; 1) fill-region remaining text by maxlen
;; 2) find end of sentence, end of punctuation, word boundary
;; 3) consume from remaining text between start and (2)
;; 4) repeat
(let ((fill-column (* maxlen 3))
(sentence-end-double-space nil)
(pos (point-min)))
(while (< pos (point-max))
(save-restriction
(narrow-to-region pos (point-max))
(fill-region pos (point-max))
(let ((limit (+ pos maxlen)))
(if (>= limit (point-max))
(setq limit (point-max))
(goto-char limit)
;; try to split at end of sentence
(if (> (backward-sentence) pos)
(setq limit (point))
;; try to split at end of punctuation
(goto-char limit)
(if (re-search-backward "[,、]" pos t)
(setq limit (1+ (point))) ; include punctuation
(goto-char limit)
;; try to split at word boundary
(forward-word-strictly -1)
(when (> (point) pos)
(setq limit (point))))))
(push (buffer-substring-no-properties pos limit) result)
(goto-char limit)
(setq pos limit)))))))
(reverse result)))
(cl-defmethod gts-tts ((engine gts-google-engine) text lang)
(let ((urls (gts-tts-gen-urls engine text lang))
(speaker (split-string gts-tts-speaker)))
(with-temp-message "Speaking..."
(gts-tts-try-interrupt-playing-process)
(apply #'call-process (car speaker) nil nil nil (append (cdr speaker) urls)))))
;;; Parser
;; detail-mode, use as default
(cl-defmethod gts-parse ((parser gts-google-parser) task)
(let* ((json (gts-resp-to-json parser (oref task raw)))
(brief (gts-result--brief parser json))
(sphonetic (gts-result--sphonetic parser json))
(tphonetic (gts-result--tphonetic parser json))
(details (gts-result--details parser json))
(definitions (gts-result--definitions parser json))
(suggestion (gts-result--suggestion parser json))
(suggestionp (> (length suggestion) 0)) tbeg tend)
(cl-flet ((phonetic (ph)
(if (and (or definitions definitions) (> (length ph) 0))
(propertize (format " [%s]" ph) 'face 'gts-google-buffer-phonetic-face)
""))
(headline (line)
(propertize (format "[%s]\n" line) 'face 'gts-google-buffer-headline-face)))
(with-temp-buffer
;; suggestion
(when suggestionp
(insert (propertize "Do you mean:" 'face 'gts-google-buffer-suggestion-desc-face) " "
(propertize suggestion 'face 'gts-google-buffer-suggestion-text-face) "?\n\n"))
;; phonetic & translate
(if (or details definitions)
(progn
(insert (if suggestionp suggestion (oref (oref task translator) text)))
(insert (phonetic sphonetic) " ")
(setq tbeg (point))
(insert (propertize brief 'face 'gts-google-buffer-brief-result-face))
(setq tend (point))
(insert (phonetic tphonetic) "\n\n"))
(insert brief))
;; details
(when details
(insert (headline "Details"))
(cl-loop for (label . items) in details
unless (= 0 (length label))
do (insert (format "\n%s:\n" label))
do (cl-loop with index = 0
for trans in items
do (insert
(format "%2d. " (cl-incf index))
(car trans)
" (" (mapconcat #'identity (cdr trans) ", ") ")"
"\n")))
(insert "\n"))
;; definitions
(when definitions
(insert (headline "Definitions"))
(cl-loop for (label . items) in definitions
unless (= 0 (length label))
do (insert (format "\n%s:\n" label))
do (cl-loop with index = 0
for (exp . eg) in items
do (insert (format "%2d. " (cl-incf index)) exp)
when (> (length eg) 0)
do (insert
"\n > "
(propertize (or eg "") 'face 'gts-google-buffer-detail-demo-face))
do (insert "\n"))))
;; at last, fill and return
(gts-update-parsed task (buffer-string) (list :tbeg tbeg :tend tend))))))
;; summary-mode
(cl-defmethod gts-parse ((parser gts-google-summary-parser) task)
(let* ((json (gts-resp-to-json parser (oref task raw)))
(result (string-trim (gts-result--brief parser json))))
(gts-update-parsed task result (list :tbeg 1 :tend (+ 1 (length result))))))
;; Extract results from response
(cl-defmethod gts-resp-to-json ((_ gts-google-parser) resp)
"Convert the buffer RESP into JSON."
(condition-case err
(json-read-from-string (decode-coding-string resp 'utf-8))
(error (user-error "Result conversion error: %s" err))))
(cl-defmethod gts-result--brief ((_ gts-google-parser) json)
"Get the translation text from JSON."
(mapconcat (lambda (item) (aref item 0)) (aref json 0) ""))
(cl-defmethod gts-result--sphonetic ((_ gts-google-parser) json)
"Get the text phonetic from JSON."
(mapconcat (lambda (item) (if (> (length item) 3) (aref item 3) "")) (aref json 0) ""))
(cl-defmethod gts-result--tphonetic ((_ gts-google-parser) json)
"Get the translation phonetic from JSON."
(mapconcat (lambda (item) (if (> (length item) 2) (aref item 2) "")) (aref json 0) ""))
(cl-defmethod gts-result--details ((_ gts-google-parser) json)
"Get the details from JSON.
Result style: ((noun (a (x y z))) (verb (b (m n o))))."
(cl-loop for i across (aref json 1)
collect
(cons
(aref i 0)
(cl-loop for j across (aref i 2)
collect
(cons
(aref j 0)
(cl-loop for k across (aref j 1) collect k))))))
(cl-defmethod gts-result--definitions ((_ gts-google-parser) json)
"Get the definitions from JSON.
Result style: ((noun (a b)) (verb (c d)))."
(cl-loop with defs = (ignore-errors (aref json 12))
for i across defs
collect
(cons
(aref i 0)
(cl-loop for j across (aref i 1)
collect
(cons
(aref j 0)
(ignore-errors (aref j 2)))))))
(cl-defmethod gts-result--suggestion ((_ gts-google-parser) json)
"Get the suggestion from JSON."
(let ((info (aref json 7))) (unless (seq-empty-p info) (aref info 1))))
;;; Token Key algorithm (deprecated)
;; This algorithm is from `google-translate' project.
;; https://github.com/atykhonov/google-translate/blob/master/google-translate-tk.el
(defvar gts-google-token--bit-v-len 32)
(defun gts-google-token--bit-v-2comp (v)
"Return the two's complement of V."
(let* ((vc (vconcat v))
(len (length vc)))
;; Complement of v
(cl-loop for i from 0 below len do
(aset vc i (logxor (aref vc i) 1)))
;; vc = complement of v + 1
(cl-loop for i downfrom (1- len) to 0
do (aset vc i (logxor (aref vc i) 1))
when (> (aref vc i) 0) return nil)
vc))
(defun gts-google-token--number-to-bit-v (n)
"Return a bit vector from N."
(if (< n 0) (gts-google-token--bit-v-2comp
(gts-google-token--number-to-bit-v (abs n)))
(let ((v (make-vector gts-google-token--bit-v-len 0)))
(cl-loop for i downfrom (1- gts-google-token--bit-v-len) to 0
with q
when (< n 1) return nil do
(setq q (ffloor (* n 0.5)))
(aset v i (floor (- n (* 2.0 q))))
(setq n q))
v)))
(defun gts-google-token--bit-v-to-number (v)
"Return a floating-point number from V."
(if (and (> (aref v 0) 0)
;; Exclude [1 0 ... 0]
(cl-loop for i from 1 below gts-google-token--bit-v-len
thereis (> (aref v i) 0)))
(- (gts-google-token--bit-v-to-number (gts-google-token--bit-v-2comp v)))
(funcall (if (> (aref v 0) 0) #'- #'+)
(cl-reduce (lambda (acc e) (+ (* acc 2.0) e))
v :initial-value 0.0))))
(defun gts-google-token--logfn (fn n1 n2)
"Helper function for logical FN with N1 and N2."
(let ((v1 (gts-google-token--number-to-bit-v n1))
(v2 (gts-google-token--number-to-bit-v n2))
(v (make-vector gts-google-token--bit-v-len 0)))
(cl-loop for i from 0 below gts-google-token--bit-v-len do
(aset v i (funcall fn (aref v1 i) (aref v2 i))))
(gts-google-token--bit-v-to-number v)))
(defun gts-google-token--logand (n1 n2)
"Return a floating-point number from N1 and N2."
(gts-google-token--logfn #'logand n1 n2))
(defun gts-google-token--logxor (n1 n2)
"Return a floating-point number from N1 and N2."
(gts-google-token--logfn #'logxor n1 n2))
(defun gts-google-token--lsh (n d)
"Return a floating-point number.
Shift the bits in N to the left or rihgt D places.
D is an integer."
(let ((v (gts-google-token--number-to-bit-v n))
(v-result (make-vector gts-google-token--bit-v-len 0)))
(if (< d 0)
;; Shift Right Logical
(cl-loop for i from (abs d) below gts-google-token--bit-v-len
for j from 0 do
(aset v-result i (aref v j)))
;; Shift Left Logical
(cl-loop for i from d below gts-google-token--bit-v-len
for j from 0 do
(aset v-result j (aref v i))))
(gts-google-token--bit-v-to-number v-result)))
(defun gts-google-token--gen-rl (a b)
"Gen rl from A and B."
(cl-loop for c from 0 below (- (length b) 2) by 3
for d = (aref b (+ c 2)) do
(setq d (if (>= d ?a) (- d 87) (- d ?0)))
(setq d (if (= (aref b (1+ c)) ?+)
(gts-google-token--lsh a (- d))
(gts-google-token--lsh a d)))
(setq a (if (= (aref b c) ?+)
(gts-google-token--logand (+ a d) 4294967295.0)
(gts-google-token--logxor a d))))
a)
(defun gts-google-tkk (token text)
"Calculate the Token for search TEXT.
It will use the tkk from Google translate page."
(let* ((b (car token))
(d1 (cdr token))
(ub "+-3^+b+-f")
(vb "+-a^+6")
(a (cl-loop for c across (encode-coding-string text 'utf-8)
for rr = (gts-google-token--gen-rl (+ (or rr b) c) vb)
finally (return rr))))
(setq a (gts-google-token--gen-rl a ub))
(setq a (gts-google-token--logxor a d1))
(if (< a 0) (setq a (+ (gts-google-token--logand a 2147483647.0) 2147483648.0)))
(setq a (ffloor (mod a 1e6)))
(format "%s.%s"
(car (split-string (number-to-string a) "\\."))
(car (split-string (number-to-string
(gts-google-token--logxor a b))
"\\.")))))
(provide 'gts-engine-google)
;;; gts-engine-google.el ends here