Skip to content

Instantly share code, notes, and snippets.

@blacklee
Created April 9, 2019 07:27
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 blacklee/7425b1f7b3e16acbfa9d1bbf04bdea28 to your computer and use it in GitHub Desktop.
Save blacklee/7425b1f7b3e16acbfa9d1bbf04bdea28 to your computer and use it in GitHub Desktop.
Hammerspoon-AQI
local url = "https://api.waqi.info/feed/@UID/?token=Demo-Token"
-- fill your UID and token
local menubar = hs.menubar.new()
function getColor(aqi)
if aqi <= 50 then
return {red = 0, blue = 0, green = 1}
end
if aqi <= 100 then
return {red = 1, blue = 0.2, green = 0.7}
end
if aqi <= 150 then
return {red = 1, blue = 0, green = 0}
end
return {red = 0.27, blue = 0.6, green = 0.2}
end
function getAQI()
code, body, htable = hs.http.get(url, nil)
if code ~= 200 then
print("get AQI error:"..code)
return
end
json = hs.json.decode(body)
aqi = json.data.aqi
textColor = getColor(aqi)
menubar:setTitle(hs.styledtext.new(aqi, { color = textColor}))
end
menubar:setTitle("AQI..")
getAQI()
timer = hs.timer.new(900, getAQI)
timer:start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment