-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstapaper.el
More file actions
66 lines (60 loc) · 2.41 KB
/
Copy pathinstapaper.el
File metadata and controls
66 lines (60 loc) · 2.41 KB
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
;; -*- no-byte-compile: t; lexical-binding: t; -*-
;;
;; Instapaper — guardar URLs via Simple API
;;
;; Requiere entrada en ~/.authinfo.gpg:
;; machine instapaper.com login EMAIL password PASSWORD
(require 'auth-source)
(require 'url)
(defun my/instapaper--credentials ()
"Return (USER . PASSWORD) from auth-source for instapaper.com."
(let ((found (car (auth-source-search :host "instapaper.com" :max 1))))
(unless found
(user-error "No credentials for instapaper.com in auth-source"))
(let ((user (plist-get found :user))
(secret (plist-get found :secret)))
(cons user (if (functionp secret) (funcall secret) secret)))))
(defun my/instapaper--resolve-url (url callback)
"Resolve redirects for URL asynchronously, then call CALLBACK with final URL."
(let ((output ""))
(make-process
:name "instapaper-resolve"
:command (list "curl" "-Ls" "-o" "/dev/null" "-w" "%{url_effective}" url)
:filter (lambda (_proc string)
(setq output (concat output string)))
:sentinel (lambda (_proc event)
(when (string-match-p "finished" event)
(funcall callback (string-trim output)))))))
(defun my/instapaper--add (url)
"Add URL to Instapaper via Simple API."
(let* ((creds (my/instapaper--credentials))
(url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data
(url-build-query-string
`(("username" ,(car creds))
("password" ,(cdr creds))
("url" ,url)))))
(url-retrieve
"https://www.instapaper.com/api/add"
(lambda (status)
(if (plist-get status :error)
(message "Instapaper error: %S" (plist-get status :error))
(message "Saved to Instapaper: %s" url))
(kill-buffer (current-buffer))))))
(defun my/instapaper-add (url)
"Add URL to Instapaper, resolving redirects first.
If point is on a URL, use it; otherwise prompt for one."
(interactive
(list (or (get-text-property (point) 'shr-url)
(thing-at-point 'url t)
(read-string "URL: "))))
(message "Resolving %s..." url)
(my/instapaper--resolve-url
url
(lambda (resolved)
(message "Adding %s to Instapaper..." resolved)
(my/instapaper--add resolved))))
(with-eval-after-load 'mu4e-view
(keymap-set mu4e-view-mode-map "C-," #'my/instapaper-add))