-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurl-shortener.el
341 lines (305 loc) · 12.3 KB
/
url-shortener.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
;;; url-shortener.el --- shorten long url and expand tinyurl
;; Time-stamp: <2013-12-10 17:44:20 Tuesday by Yu Yang>
;; Author: Yu Yang <yy2012cn@NOSPAM.gmail.com>
;; URL: https://github.com/yuyang0/url-shortener
;; Version: 0.3
;;
;; This file 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 3 of the License,
;; 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 this file. If not, see <http://www.gnu.org/licenses/>.
;;
;;; Commentary:
;; Usage:
;; this package provides commands to do url shorten and expand:
;; M-x goo-url-shorten
;; M-x goo-url-expand
;; M-x bitly-url-shorten
;; M-x bitly-url-expand
;; M-x dwz-url-shorten
;; M-x dwz-url-expand
;; M-x 126am-url-shorten
;; M-x 126am-url-expand
;;; Code:
(require 'json)
(defgroup bitly nil
"The bitly URL shortening service."
:prefix "bitly-"
:group 'applications)
(defcustom bitly-access-token nil
"The OAuth access token for bitly.
Get your personal token here: https://bitly.com/a/oauth_apps"
:type 'string
:group 'bitly)
(defvar bitly-base-url "https://api-ssl.bitly.com/v3/shorten")
(defvar bitly-shorten-api-url "https://api-ssl.bitly.com/v3/shorten")
(defvar bitly-expand-api-url "https://api-ssl.bitly.com/v3/expand")
(defvar dwz-shorten-api-url "http://dwz.cn/create.php")
(defvar dwz-expand-api-url "http://dwz.cn/query.php")
(defgroup goo nil
"The goo.gl URL shortening service."
:prefix "goo-"
:group 'applications)
(defcustom goo-api-key nil
"The Api key for goo.gl.
Get your personal token here:
https://developers.google.com/url-shortener/v1/getting_started#APIKey"
:type 'string
:group 'goo)
(defvar goo-shorten-api-url "https://www.googleapis.com/urlshortener/v1/url")
(defgroup 126am nil
"The 126.am URL shortening service."
:prefix "126am-"
:group 'applications)
(defcustom 126am-api-key nil
"The Api key for 126.am.
Get your personal token here: http://126.am/apiManage.action"
:type 'string
:group '126am)
(defvar 126am-shorten-api-url "http://126.am/api!shorten.action")
(defvar 126am-expand-api-url "http://126.am/api!expand.action")
(defgroup tcn nil
"The 126.am URL shortening service."
:prefix "tcn-"
:group 'application)
(defcustom tcn-app-key nil
"The App key for t.cn.
Get your personal token here: http://open.weibo.com/apps/new"
:type 'string
:group 'tcn)
(defvar tcn-shorten-api-url "https://api.weibo.com/2/short_url/shorten.json")
(defun url-equal (url1 url2)
(if (or (equal url1 url2)
(equal url1 (concat url2 "/"))
(equal url2 (concat url1 "/")))
t
nil))
(defun smart-insert-url-to-buffer (test-url url-need-insert)
"If url at current point is equal to `test-url', replace the url at current
point with `url--need-insert', otherwise insert the `url-need-insert' to the buffer"
(let ((current-point-url (thing-at-point 'url))
(url-boundaries (bounds-of-thing-at-point 'url)))
(if url-boundaries
(if (url-equal test-url current-point-url)
(progn
(goto-char (car url-boundaries))
(delete-region (car url-boundaries) (cdr url-boundaries))
(insert url-need-insert))
(progn
(goto-char (cdr url-boundaries))
(insert " " url-need-insert)))
(insert "(" url-need-insert ")"))))
(defun my-url-http-post (url args callback cbargs &optional is-json)
"Send ARGS to URL as a POST request."
(let ((url-request-method "POST")
(url-request-extra-headers
(if is-json
'(("Content-Type" . "application/json"))
'(("Content-Type" . "application/x-www-form-urlencoded"))))
(url-request-data
(if is-json
(json-encode args)
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&"))))
;; if you want, replace `my-switch-to-url-buffer' with `my-kill-url-buffer'
(url-retrieve url callback cbargs)))
(defun my-url-http-get (url args callback cbargs)
(let ((url-request-method "GET")
(arg-stuff
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&")))
(url-retrieve (concat url "?" arg-stuff)
callback cbargs)))
(defun http-callback (status origin-buffer cb-fun &rest cb-fun-args)
(let* ((json-buffer (current-buffer))
(resp-json (with-current-buffer json-buffer
(goto-char (point-min))
(search-forward "\n\n" nil t)
(json-read))))
(switch-to-buffer origin-buffer)
(apply cb-fun resp-json cb-fun-args)))
(defun bitly-shorten (long-url)
(defun bitly-shorten-callback (resp-json)
(let* ((status-code (cdr (assq 'status_code resp-json))))
(if (equal status-code 200)
(let ((tinyurl (cdr (assq 'url
(cdr (assq 'data resp-json)))))
(longurl (cdr (assq 'long_url
(cdr (assq 'data resp-json))))))
(smart-insert-url-to-buffer longurl tinyurl))
(error "Error %s calling bitly: %s"
status-code
(cdr (assq 'status_txt resp-json)))
)))
(my-url-http-get bitly-shorten-api-url `(,(cons "access_token" bitly-access-token)
,(cons "longUrl" long-url))
'http-callback `(,(current-buffer) bitly-shorten-callback)))
(defun bitly-expand (short-url)
(defun bitly-expand-callback (resp-json)
(let* ((data (cdr (assq 'data resp-json)))
(expand-vec (cdr (assq 'expand data)))
(tinyurl (cdr (assq 'short_url
(elt expand-vec 0))))
(longurl (cdr (assq 'long_url
(elt expand-vec 0)))))
(smart-insert-url-to-buffer tinyurl longurl)))
(my-url-http-get bitly-expand-api-url `(,(cons "access_token" bitly-access-token)
,(cons "shortUrl" short-url))
'http-callback `(,(current-buffer) bitly-expand-callback)))
(defun tcn-shorten (long-url)
(defun tcn-callback (resp-json)
"process the json returned by http"
(let* ((urls-vector (cdr (assq 'urls resp-json)))
(i 0))
(if urls-vector
(while (< i (length urls-vector))
(let* ((entry (elt urls-vector i))
(tinyurl (cdr (assq 'url_short entry)))
(longurl (cdr (assq 'url_long entry))))
(smart-insert-url-to-buffer longurl tinyurl))
(setq i (1+ i)))
(error "Error %s calling t.cn: %s"
(cdr (assq 'error_code resp-json))
(cdr (assq 'error resp-json))))))
(my-url-http-get tcn-shorten-api-url `(,(cons "source" tcn-app-key)
,(cons "url_long" long-url))
'http-callback `(,(current-buffer) tcn-callback)))
(defun dwz-shorten (long-url)
"Return a shortened URL for LONG-URL."
(defun dwz-shorten-callback (resp-json)
(let* ((longurl (cdr (assq 'longurl resp-json)))
(tinyurl (cdr (assq 'tinyurl resp-json)))
(status-code (cdr (assq 'status resp-json))))
(if (equal status-code 0)
(smart-insert-url-to-buffer longurl tinyurl)
(error "Error calling dwz.cn: %s"
(cdr (assq 'err_msg resp-json))))))
(my-url-http-post dwz-shorten-api-url `(,(cons "url" long-url)) 'http-callback
`(,(current-buffer) dwz-shorten-callback)))
(defun dwz-expand (short-url)
(defun dwz-expand-callback (resp-json short-url)
(let* ((longurl (cdr (assq 'longurl resp-json)))
(tinyurl short-url)
(status-code (cdr (assq 'status resp-json))))
(if (equal status-code 0)
(smart-insert-url-to-buffer tinyurl longurl)
(error "Error calling dwz.cn: %s"
(cdr (assq 'err_msg resp-json))))))
(my-url-http-post dwz-expand-api-url `(,(cons "tinyurl" short-url)) 'http-callback
`(,(current-buffer) dwz-expand-callback ,short-url)))
(defun 126am-shorten (long-url)
"Return a shortened URL for LONG-URL."
(defun 126am-shorten-callback (resp-json)
(let* ((longurl (cdr (assq 'longUrl resp-json)))
(tinyurl (cdr (assq 'url resp-json)))
(status-code (cdr (assq 'status_code resp-json))))
(if (equal status-code 200)
(smart-insert-url-to-buffer longurl tinyurl)
(error "Error %s calling 126.am: %s"
status-code
(cdr (assq 'status_txt resp-json))))
))
(my-url-http-post 126am-shorten-api-url
`(,(cons "longUrl" long-url) ,(cons "key" 126am-api-key))
'http-callback
`(,(current-buffer) 126am-shorten-callback)))
(defun 126am-expand (short-url)
(defun 126am-expand-callback (resp-json short-url)
(let* ((longurl (cdr (assq 'url resp-json)))
(tinyurl short-url)
(status-code (cdr (assq 'status_code resp-json))))
(if (equal status-code 200)
(smart-insert-url-to-buffer tinyurl longurl)
(error "Error %s calling 126.am: %s"
status-code
(cdr (assq 'status_txt resp-json))))))
(my-url-http-post 126am-expand-api-url
`(,(cons "shortUrl" short-url) ,(cons "key" 126am-api-key))
'http-callback
`(,(current-buffer) 126am-expand-callback ,short-url)))
(defun goo-shorten (long-url)
"Return a shortened URL for LONG-URL."
(defun goo-callback (resp-json)
(let ((longurl (cdr (assq 'longUrl resp-json)))
(tinyurl (cdr (assq 'id resp-json))))
(smart-insert-url-to-buffer longurl tinyurl)))
(let ((goo-shorten-api-full-url
(if goo-api-key
(format "%s?key=%s" goo-shorten-api-url goo-api-key)
goo-shorten-api-url)))
(my-url-http-post goo-shorten-api-full-url
`(,(cons "longUrl" long-url))
'http-callback
`(,(current-buffer) goo-callback) t)))
(defun goo-expand (short-url)
"Return a expended URL for tiny url."
(defun goo-callback (resp-json)
(let ((longurl (cdr (assq 'longUrl resp-json)))
(tinyurl (cdr (assq 'id resp-json))))
(smart-insert-url-to-buffer tinyurl longurl)))
(let ((args (if goo-api-key
`(,(cons "shortUrl" short-url)
,(cons "key" goo-api-key))
`(,(cons "shortUrl" short-url)))))
(my-url-http-get goo-shorten-api-url
args
'http-callback
`(,(current-buffer) goo-callback))))
(defun url-shorten-wrapper (shorten-fun)
(let ((long-url (thing-at-point 'url)))
(setq long-url (read-string (format "long url(default: %s)" long-url) nil nil long-url))
(funcall shorten-fun long-url)))
;;;###autoload
(defun dwz-url-shorten ()
(interactive)
(url-shorten-wrapper 'dwz-shorten))
;;;###autoload
(defun dwz-url-expand ()
(interactive)
(url-shorten-wrapper 'dwz-expand))
;;;###autoload
(defun 126am-url-shorten ()
(interactive)
(url-shorten-wrapper '126am-shorten))
;;;###autoload
(defun 126am-url-expand ()
(interactive)
(url-shorten-wrapper '126am-expand))
;;;###autoload
(defun tcn-url-shorten ()
(interactive)
(url-shorten-wrapper 'tcn-shorten))
;;;###autoload
(defun bitly-url-shorten ()
(interactive)
(url-shorten-wrapper 'bitly-shorten))
;;;###autoload
(defun bitly-url-expand ()
(interactive)
(url-shorten-wrapper 'bitly-expand))
;;;###autoload
(defun goo-url-shorten ()
(interactive)
(url-shorten-wrapper 'goo-shorten))
;;;###autoload
(defun goo-url-expand ()
(interactive)
(url-shorten-wrapper 'goo-expand))
(provide 'url-shortener)
;;; url-shortener.el ends here