Skip to content

Instantly share code, notes, and snippets.

@FlowerWrong
Created May 7, 2021 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlowerWrong/3f4aa97da5e3cb72272c874da15fcb18 to your computer and use it in GitHub Desktop.
Save FlowerWrong/3f4aa97da5e3cb72272c874da15fcb18 to your computer and use it in GitHub Desktop.
binance vet price for hammerspoon
local binanceVETMenubar = hs.menubar.new()
BINANCE_URL = 'https://fapi.binance.com'
SYMBOL = 'VETUSDT'
local url = BINANCE_URL .. "/fapi/v1/ticker/price?symbol=" .. SYMBOL
local vetPrices = {}
--------------------------------------------------------------------------------
-- splicing: remove elements from a table
--------------------------------------------------------------------------------
function table.splice(tbl, start, length)
length = length or 1
start = start or 1
local endd = start + length
local spliced = {}
local remainder = {}
for i,elt in ipairs(tbl) do
if i < start or i >= endd then
table.insert(spliced, elt)
else
table.insert(remainder, elt)
end
end
return spliced, remainder
end
local function updateVET()
hs.http.asyncGet(
url,
{},
function(status, response, headers)
local price = '--'
if status == 200 then
local msg = hs.json.decode(response)
price = msg["price"]
end
if #vetPrices > 0 then
table.insert(vetPrices, 1, price)
else
table.insert(vetPrices, price)
end
if #vetPrices > 15 then
_, vetPrices = table.splice(vetPrices, 1, 5)
end
local menus = {}
for i, v in ipairs(vetPrices) do
table.insert(menus, {
title = v,
disabled = true
})
end
binanceVETMenubar:setMenu(menus)
binanceVETMenubar:setTitle('VET $' .. price)
end
)
end
binanceVETMenubar:setTitle('binance_vet')
binanceVETTimer = hs.timer.doEvery(5, updateVET)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment