-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_shortener.lua
32 lines (31 loc) · 1.14 KB
/
url_shortener.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
-- Requires an API Access Token from Bit.ly
local secrets = require 'secrets'
local KUTT_IT_PASSWORD = secrets.kuttIt.password;
local KUTT_IT_TOKEN = secrets.kuttIt.token;
local utils = require('utils')
local HEADERS = {};
HEADERS['X-API-KEY'] = KUTT_IT_TOKEN;
HEADERS['Content-Type']= 'application/json';
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "S", function()
local board = hs.pasteboard.getContents()
if board:match("^https?://") then
local response = hs.http.asyncPost(
"https://kutt.it/api/v2/links",
hs.json.encode({
target=board,
description= "Hammerspoon:"..utils.getFocusedWindowApplicationTitle() .. ':'.. utils.getFocusedWindowTitle()
}), HEADERS,
function(status, response, headers)
if (status == 200 or status == 201) then
local msg = hs.json.decode(response)
hs.pasteboard.setContents(msg.link)
hs.notify.new({title="kutt.it URL Shorten: Success", informativeText=msg.link}):send()
else
hs.notify.new({title="kutt.it URL Shorten: Failure", informativeText=response}):send()
end
end
)
else
hs.notify.new({title="Bitly URL Shorten: Failure", informativeText="Expected: URL"}):send()
end
end)