forked from atykhonov/google-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-translate-backend.el
142 lines (109 loc) · 4.99 KB
/
google-translate-backend.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
;;; google-translate-backend.el --- Backend methods for url retrieve.
;; Copyright (C) 2019 Tomotaka SUWA <tomotaka.suwa@gmail.com>
;; Author: Oleksandr Manzyuk <manzyuk@gmail.com>
;; Maintainer: Andrey Tykhonov <atykhonov@gmail.com>
;; URL: https://github.com/atykhonov/google-translate
;; Version: 0.11.17
;; Keywords: convenience
;; This file is NOT part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Provide backend facilities to cope with google translate.
;;; Code:
(defvar google-translate-backend-method 'emacs
"The backend method for URL retrieve.
Valid symbols are below:
- emacs: use built in `url-retrieve-synchronously'
- curl: invoke curl command
- wget: invoke wget command
and any other keys of `google-translate-backend-commands'.")
(defvar google-translate-backend-user-agent "Emacs"
"The user agent string for HTTP request header.")
(defvar google-translate-backend-commands
'((curl :args ("-s" "-L" "-A"))
(wget :args ("-q" "-O" "-" "-U")))
"An alist of external program specifications.
The form of each element is (KEY P-LIST). The variable
`google-translate-backend-method' may have one of KEYs and is
used for determine the command to execute. The P-LIST of each
element represents command specific information.
Available properties:
- Property `:name': the program name(optional)
- Property `:args': a list of arguments passed to the program
If you omit the `:name' property, (symbol-name KEY) will be used
as the program name. The `:args' property must be composed to
satisfy all the following conditions:
- Output content to standard output
- Suppress non-content(HTTP headers, progress messages, etc)
- Handle location response header
- Place User-Agent option at last
So if you would like to use another program \"foo\", for example:
\(push \\='(foo :name \"foo-x86\"
:args (\"-q\" \"--agent\"))
google-translate-backend-commands)
\(setq google-translate-backend-method \\='foo)
And the command line looks like:
foo-x86 -q --agent ['google-translate-backend-user-agent] [URL]")
(defvar google-translate-backend-debug nil
"Non-nil means log http activities to the *google-translate-debug* buffer.")
(defun google-translate-backend--log (&rest args)
"Log http activities to the *google-translate-debug* buffer along with ARGS.
Disabled if `google-translate-backend-debug' is nil."
(when google-translate-backend-debug
(let ((message (mapconcat 'identity
(list (current-time-string)
(prin1-to-string args)
"-- begin --"
(buffer-string)
"-- end --")
"\n")))
(with-current-buffer
(get-buffer-create "*google-translate-backend-debug*")
(goto-char (point-max))
(insert message)
(newline)))))
(defun google-translate-backend--emacs (url)
"Get URL contents by `url-retrieve-synchronously'."
(insert
(let ((url-user-agent google-translate-backend-user-agent))
(with-current-buffer (url-retrieve-synchronously url nil nil 15)
(set-buffer-multibyte t)
(google-translate-backend--log url 'emacs)
(goto-char (point-min))
(re-search-forward "\n\n")
(prog1 (buffer-substring (point)
(point-max))
(kill-buffer))))))
(defun google-translate-backend--process (url key spec)
"Get URL contents by `call-process'.
\(KEY SPEC) would be exist in `google-translate-backend-commands'."
(let ((name (or (plist-get spec :name)
(symbol-name key)))
(args (plist-get spec :args))
(agent google-translate-backend-user-agent))
(apply 'call-process
(append (list name nil t nil)
args
(list agent url)))
(google-translate-backend--log url key spec)))
(defun google-translate-backend-retrieve (url)
"Get URL contents via `google-translate-backend-method'."
(let ((method google-translate-backend-method))
(if (eq method 'emacs)
(google-translate-backend--emacs url)
(let ((spec (cdr (assq method
google-translate-backend-commands))))
(if (null spec)
(error "Unknown backend method: %s" method)
(google-translate-backend--process url method spec))))))
(provide 'google-translate-backend)
;;; google-translate-backend.el ends here