-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgur.lua
148 lines (115 loc) · 5.13 KB
/
imgur.lua
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
function authedRequest(method, url, payload, callback)
local accessToken = hs.settings.get('imgur_access_token')
local headers = {Authorization = "Bearer " .. accessToken}
return hs.http.doAsyncRequest(url, method, payload, headers, callback)
end
function checkAuth(callback)
local clientID = hs.settings.get('imgur_client_id')
local clientSecret = hs.settings.get('imgur_client_secret')
local refreshToken = hs.settings.get('imgur_refresh_token')
local accessToken = hs.settings.get('imgur_access_token')
if clientID ~= nil and clientSecret ~= nil and refreshToken ~= nil and accessToken ~= nil then
return callback(true)
end
local result = nil
result = hs.dialog.blockAlert('Do you want to authenticate with Imgur?', '', 'YES', 'NO')
if result ~= 'YES' then
return callback(false)
end
result = hs.dialog.blockAlert('Please register an imgur application', 'https://api.imgur.com/oauth2/addclient', 'Done', 'Cancel')
if result ~= 'Done' then
return callback(false)
end
result, clientID = hs.dialog.textPrompt('Please enter the Client ID', '', '', 'OK', 'Cancel')
if result ~= 'OK' or #clientID == 0 then
return callback(false)
end
result, clientSecret = hs.dialog.textPrompt('Please enter the Client secret', '', '', 'OK', 'Cancel')
if result ~= 'OK' or #clientSecret == 0 then
return callback(false)
end
local authURL = 'https://api.imgur.com/oauth2/authorize?client_id=' .. clientID .. '&response_type=pin'
local pin = nil
result, pin = hs.dialog.textPrompt('Please authenticate with Imgur and enter PIN', authURL, '', 'Done', 'Cancel')
if result ~= 'Done' or #pin == 0 then
return callback(false)
end
local url = "https://api.imgur.com/oauth2/token"
local payload = 'client_id=' .. clientID .. '&client_secret=' .. clientSecret .. '&pin=' .. pin .. '&grant_type=pin'
return hs.http.doAsyncRequest(url, 'POST', payload, nil, function (responseStatus, responseJSON)
if responseStatus ~= 200 then
hs.dialog.blockAlert('Error while trying to authenticate with Imgur', responseJSON)
return checkAuth(callback)
end
local response = hs.json.decode(responseJSON)
refreshToken = response['refresh_token']
accessToken = response['access_token']
hs.settings.set('imgur_client_id', clientID)
hs.settings.set('imgur_client_secret', clientSecret)
hs.settings.set('imgur_refresh_token', refreshToken)
hs.settings.set('imgur_access_token', accessToken)
return callback(true)
end)
end
function checkToken(callback)
return checkAuth(function (ok)
if not ok then
return
end
return authedRequest('GET', 'https://api.imgur.com/3/account/me/settings', nil, function (responseStatus, responseJSON)
if responseStatus == 200 then
return callback(true)
end
local clientID = hs.settings.get('imgur_client_id')
local clientSecret = hs.settings.get('imgur_client_secret')
local refreshToken = hs.settings.get('imgur_refresh_token')
local accessToken = hs.settings.get('imgur_access_token')
local url = "https://api.imgur.com/oauth2/token"
local payload = 'client_id=' .. clientID .. '&client_secret=' .. clientSecret .. '&refresh_token=' .. refreshToken .. '&grant_type=refresh_token&expires_in=0'
return hs.http.doAsyncRequest(url, 'POST', payload, nil, function (responseStatus, responseJSON)
if responseStatus ~= 200 then
hs.dialog.blockAlert('Error while trying to authenticate with Imgur', responseJSON)
hs.settings.clear('imgur_client_id')
hs.settings.clear('imgur_client_secret')
hs.settings.clear('imgur_refresh_token')
hs.settings.clear('imgur_access_token')
return checkToken(callback)
end
local response = hs.json.decode(responseJSON)
refreshToken = response['refresh_token']
accessToken = response['access_token']
hs.settings.set('imgur_refresh_token', refreshToken)
hs.settings.set('imgur_access_token', accessToken)
return callback(true)
end)
end)
end)
end
function uploadImageFromClipboard()
return checkToken(function (ok)
if not ok then
return
end
local image = hs.pasteboard.readImage()
if not image then
hs.alert.show('No image on clipboard, could not upload it to Imgur')
return false
end
local tempfile = "/tmp/tmp.png"
image:saveToFile(tempfile)
local b64 = hs.execute("base64 -i "..tempfile)
b64 = hs.http.encodeForQuery(string.gsub(b64, "\n", ""))
local payload = 'type=base64&image=' .. b64
return authedRequest('POST', 'https://api.imgur.com/3/upload.json', payload, function (responseStatus, responseJSON)
if responseStatus ~= 200 then
hs.dialog.blockAlert('Error while trying to upload Imgur', responseJSON)
return false
end
local response = hs.json.decode(responseJSON)
local imageURL = response.data.link
hs.pasteboard.setContents(imageURL)
hs.alert.show('Uploaded to Imgur, URL on clipboard')
end)
end)
end
hs.hotkey.bind(hyper, "4", uploadImageFromClipboard)