-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedium.el
60 lines (53 loc) · 1.98 KB
/
medium.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
(defvar medium-user-id nil)
(defconst medium-create-post-curl-template
(string-join
(list
"curl -s"
"--request POST \"https://api.medium.com/v1/users/%s/posts\""
"--header \"Authorization: Bearer %s\""
"--header \"Content-Type: application/json\""
"--header \"Accept: application/json\""
"--header \"Accept-Charset: utf-8\""
"--data %s") " "))
(defun medium-create-post-json (title content tags)
"Format create post request json body to string"
(json-encode
(list
:title title
:content content
:tags tags
:contentFormat "markdown"
:publishStatus "draft")))
(defun medium-retrieve-secret ()
"Retrieve first access token for api.medium.com host from auth-sources"
(let* ((credentials (auth-source-search :host "api.medium.com"))
(secret (plist-get (car credentials) :secret)))
(if (functionp secret) (funcall secret) secret)))
(defun medium-retrieve-user-id ()
"Call medium api with access token to retrieve details about user"
(interactive)
(let ((access-token (medium-retrieve-secret))
(command-template
"curl -s 'https://api.medium.com/v1/me' --header 'Authorization: Bearer %s'"))
(shell-command (format command-template access-token))))
(defun medium-create-post-curl-command (user-id token title content tags)
"Format medium create post cURL command and return as string"
(format
medium-create-post-curl-template
user-id
token
(shell-quote-argument (medium-create-post-json title content tags))))
(defun medium-create-post (title tags)
"Ask for title and tags and create post on medium from current buffer"
(interactive "sTitle: \nsTags: \n")
(setq tags (split-string tags " "))
(let* ((access-token (medium-retrieve-secret))
(command
(medium-create-post-curl-command
medium-user-id
access-token
title
(buffer-string)
tags)))
(shell-command command)))
(provide 'medium)