forked from austin-----/weibo.emacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweibo.el
181 lines (156 loc) · 5.84 KB
/
weibo.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
;; Copyright (C) 2011 Austin<[email protected]>
;; This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
(eval-when-compile (require 'cl))
(require 'url)
(require 'json)
(require 'weibo-authorize)
(require 'weibo-timeline)
(require 'weibo-status)
(require 'weibo-user)
(require 'weibo-image)
(require 'weibo-post)
(require 'weibo-comment)
(require 'weibo-status-comment)
(defconst weibo-api-url "https://api.weibo.com/2/" "API base url")
(defvar weibo-directory "~/.t.weibo.emacs.d")
(defvar weibo-token nil)
(defvar weibo-token-expire nil)
(defun weibo-get-token-file ()
(unless (file-exists-p (expand-file-name weibo-directory))
(make-directory (expand-file-name weibo-directory) t))
(expand-file-name "token2" weibo-directory))
(defun weibo-get-token ()
(unless (and weibo-token (not (weibo-token-expired)))
(weibo-authorize))
weibo-token)
(defun weibo-parse-token (string)
(when (string-match "\\([^:]*\\):\\(.*\\)" string)
(setq weibo-token (match-string 1 string))
(setq weibo-token-expire (match-string 2 string))))
(defun weibo-token-expired ()
(when weibo-token-expire
(> (float-time) (string-to-number weibo-token-expire))))
(defun weibo-authorize (&optional reauthorize)
(when (file-exists-p (weibo-get-token-file))
(save-excursion
(find-file (weibo-get-token-file))
(weibo-parse-token (buffer-substring-no-properties (point-min) (point-max)))
(save-buffer)
(kill-this-buffer)))
(when (or reauthorize (not weibo-token) (weibo-token-expired))
(weibo-parse-token (weibo-authorize-app))
(setq weibo-token-expire (number-to-string (+ (float-time) (string-to-number weibo-token-expire)))))
(save-excursion
(find-file (weibo-get-token-file))
(erase-buffer)
(insert (format "%s:%s\n" weibo-token weibo-token-expire))
(save-buffer)
(kill-this-buffer))
weibo-token)
(defun weibo-check-result (root)
(if (and root (length root)
(if (and (listp root) (weibo-get-node root 'error)) nil t))
t
(print (weibo-get-node-text root 'error))
nil))
(defun weibo-get-node (pnode tag)
(assoc tag pnode))
(defun weibo-get-node-text (node tag)
(let ((data (cdr (weibo-get-node node tag))))
(cond ((numberp data) (format "%d" data))
(t data))))
(defun weibo-get-body ()
(goto-char (point-min))
(let ((start
(or (search-forward "\r\n\r\n" nil t)
(search-forward "\n\n" nil t)))
(buffer (current-buffer))
(max (point-max)))
(when start
(with-temp-buffer
(insert-buffer-substring buffer start max)
(mm-decode-coding-region (point-min) (point-max) 'utf-8)
(goto-char (point-min))
(while (re-search-forward "\"id\":\\([0-9]+\\)," nil t)
(replace-match "\"id\":\"\\1\"," nil nil))
(goto-char (point-min))
(condition-case nil
(json-read)
((error nil) `((error . ,(buffer-substring (point-min) (point-max))))))))))
(defun weibo-retrieve-url (url)
(let ((url-request-method "GET")
(url-request-extra-headers
`(("Authorization" . ,(format "OAuth2 %s" (url-hexify-string (weibo-get-token)))))))
(flet ((message (&rest args) nil))
(url-retrieve-synchronously url))))
(defun weibo-send-url (url args)
(let ((url-request-method "POST")
(url-request-extra-headers
`(("Content-Type" . "application/x-www-form-urlencoded")
("Authorization" . ,(format "OAuth2 %s" (url-hexify-string (weibo-get-token))))))
(url-request-data
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&")))
(flet ((message (&rest args) nil))
(url-retrieve-synchronously url))))
(defun weibo-get-data (item callback &optional param &rest cbdata)
(let ((root (with-current-buffer
(weibo-retrieve-url (concat (format "%s%s.json" weibo-api-url item) param))
(weibo-get-body))))
(apply callback (cons root cbdata))))
(defun weibo-post-data (item callback vars &optional param &rest cbdata)
(let ((root (with-current-buffer
(weibo-send-url (concat (format "%s%s.json" weibo-api-url item) param) vars)
(weibo-get-body))))
(apply callback (cons root cbdata))))
(defun weibo-parse-data-result (root &rest data)
(when root
(print root)))
(defun weibo-string-decrement (str)
(let ((strl (reverse (delete "" (split-string str ""))))
(result nil)
(done nil))
(while strl
(let ((c (car strl)))
(push
(if done c
(if (string= "0" c) "9"
(progn
(setq done t)
(number-to-string (- (string-to-number c) 1)))))
result))
(setq strl (cdr strl)))
(mapconcat 'identity result "")))
(defun weibo-bury-close-window ()
(interactive)
(bury-buffer)
(condition-case err
(delete-window)
(error nil)))
(defun weibo-kill-close-window ()
(interactive)
(kill-buffer)
(condition-case err
(delete-window)
(error nil)))
(weibo-timeline-register-provider (weibo-friends-timeline-provider))
(weibo-timeline-register-provider (weibo-user-timeline-provider))
(weibo-timeline-register-provider (weibo-mention-timeline-provider))
(weibo-timeline-register-provider (weibo-comments-mentions-timeline-provider))
(weibo-timeline-register-provider (weibo-comments-by-me-timeline-provider))
(weibo-timeline-register-provider (weibo-comments-to-me-timeline-provider))
(weibo-timeline-register-provider (weibo-public-timeline-provider))
(provide 'weibo)