forked from x89/Shreddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshreddit.el
60 lines (48 loc) · 2.11 KB
/
shreddit.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
(require 'url)
(require 'json)
(defun get-alist (symbols alist)
(if symbols
(alist-get (cdr symbols)
(assoc (car symbols) alist))
(cdr alist)))
(defun http-post (url args)
(let ((url-request-method "POST")
(url-request-extra-headers '(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data
(mapconcat (lambda (arg)
(concat (url-hexify-string (car arg))
"="
(url-hexify-string (cdr arg))))
args
"&")))
(with-current-buffer
(url-retrieve-synchronously url)
(goto-char (+ url-http-end-of-headers 1))
(json-read-object))))
(defun login (username password)
(cdaddr
(cadar
(http-post "http://www.reddit.com/api/login" (list (cons "api_type" "json") (cons "user" username) (cons "passwd" password))))))
(defun get-comment-ids (username)
(mapcar (lambda (x) (get-alist '(data name) x))
(get-alist '(data children)
(with-current-buffer
(url-retrieve-synchronously (format "http://www.reddit.com/user/%s/comments.json" username))
(goto-char (+ url-http-end-of-headers 1))
(json-read-object)))))
(defun edit-comment (comment-id text modhash)
(http-post "http://www.reddit.com/api/editusertext"
(list (cons "api_type" "json")
(cons "text" text)
(cons "thing_id" comment-id)
(cons "uh" modhash))))
(defun edit-all-comments (username password)
(let ((modhash (login username password)))
(mapcar (lambda (x) (edit-comment x "Feel The Mighty Thrust Of Emacs" modhash)) (get-comment-ids username))))
(defun delete-comment (comment-id modhash)
(http-post "http://www.reddit.com/api/del"
(list (cons "thing_id" comment-id)
(cons "uh" modhash))))
(defun delete-all-comments (username password)
(let ((modhash (login username password)))
(mapcar (lambda (x) (delete-comment x modhash)) (get-comment-ids username))))