基于网络上的版本修改的,找不到出处了。第一个摸索的lua脚本,调了很久。
主要修改:如果风速不大,就默认不显示,以及调整为了一些格式
local urlApi = 'http://..'
--这个urlApi去申请个免费的就可以了 然后打开对应的json数据修改local menubar = hs.menubar.new()
local menuData = {}local weaEmoji = {lei = '🌩️',qing = '☀️',shachen = '😷',wu = '🌫',xue = '❄️',yu = '🌧',yujiaxue = '🌨',yun = '☁️',zhenyu = '🌧',yin = '⛅️',default = ''
}function updateMenubar()menubar:setTooltip("Weather Info")menubar:setMenu(menuData)
endfunction getWeather()hs.http.doAsyncRequest(urlApi, "GET", nil,nil, function(code, body, htable)if code ~= 200 thenprint('get weather error:'..code)returnendrawjson = hs.json.decode(body)city = rawjson.citymenuData = {}for k, v in pairs(rawjson.data) doif k == 1 thenmenubar:setTitle(weaEmoji[v.wea_img])if v.win_speed == "<3级" thentitlestr = string.format("%s %s %s 🌡️%s-%s°C", city,weaEmoji[v.wea_img], v.wea, v.tem_night, v.tem_day)elsetitlestr = string.format("%s %s 🌡️%s-%s°C 💨%s %s %s", city,weaEmoji[v.wea_img],v.tem_night, v.tem_day, v.win_speed, v.win, v.wea)enditem = { title = titlestr }table.insert(menuData, item)table.insert(menuData, {title = '-'})elseif v.win_speed == "<3级" thentitlestr = string.format("%s %s 🌡️%s-%s°C %s", v.date, weaEmoji[v.wea_img], v.tem_night, v.tem_day, v.wea)elsetitlestr = string.format("%s %s 🌡️%s-%s°C 💨%s %s %s", v.date, weaEmoji[v.wea_img],v.tem_night, v.tem_day, v.win_speed, v.win, v.wea)enditem = { title = titlestr }table.insert(menuData, item)endendupdateMenubar()end)
endmenubar:setTitle('👀')
getWeather()
updateMenubar()
hs.timer.doEvery(3000, getWeather)