Created
October 26, 2019 12:54
-
-
Save ryanqy/5288c69b084c3f344a1b6da05566b9c2 to your computer and use it in GitHub Desktop.
hammerspoon init.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- load spoons | |
logger = hs.logger.new("Roger", "debug") | |
logger.i("load hammerspoon config") | |
function currentSSID() | |
ssid = hs.wifi.currentNetwork() | |
if ssid == nil then | |
return "" | |
else | |
return ssid | |
end | |
end | |
wifiWatcher = nil | |
homeSSID = "Taurus" | |
companySSID = "Baturu" | |
lastSSID = currentSSID() | |
function string.starts(String,Start) | |
return string.sub(String,1,string.len(Start))==Start | |
end | |
function connectToHomeWifi(newSSID, lastSSID) | |
return newSSID == homeSSID and lastSSID ~= homeSSID | |
end | |
function leaveHomeWifi(newSSID, lastSSID) | |
return newSSID ~= homeSSID and lastSSID == homeSSID | |
end | |
function connectToCompanyWifi(newSSID, lastSSID) | |
return string.starts(newSSID, companySSID) and not string.starts(lastSSID, companySSID) | |
end | |
function leaveCompanyWifi(newSSID, lastSSID) | |
return not string.starts(newSSID, companySSID) and string.starts(lastSSID, companySSID) | |
end | |
function connectToWifiExceptCompany(newSSID, lastSSID) | |
return not string.starts(newSSID, companySSID) | |
end | |
function fileExists(name) | |
local file = io.open(name, "r") | |
if file~=nil then | |
io.close(file) | |
return true | |
else | |
return false | |
end | |
end | |
function appendMavenSettingsFileNameWithBackup() | |
if fileExists("/Users/laughing/.m2/settings.xml") then | |
logger.d("rename /Users/laughing/.m2/settings.xml to settings.xml.backup") | |
os.rename("/Users/laughing/.m2/settings.xml", "/Users/laughing/.m2/settings.xml.backup") | |
end | |
end | |
function recoverMavenSettingsFileName() | |
if fileExists("/Users/laughing/.m2/settings.xml.backup") then | |
logger.d("rename /Users/laughing/.m2/settings.xml.backup to settings.xml") | |
os.rename("/Users/laughing/.m2/settings.xml.backup", "/Users/laughing/.m2/settings.xml") | |
end | |
end | |
function ssidChangedCallback() | |
newSSID = currentSSID() | |
logger.d("ssid has changed, new ssid:" .. newSSID .. " last ssid:" .. lastSSID) | |
if connectToCompanyWifi(newSSID, lastSSID) then | |
logger.d("connect to company wifi") | |
hs.notify.show("HammerSpoon", "WIFI", "Connect to wifi " .. newSSID) | |
hs.audiodevice.defaultOutputDevice():setMuted(true) | |
logger.d("audio device set to muted") | |
recoverMavenSettingsFileName() | |
end | |
if leaveCompanyWifi(newSSID, lastSSID) then | |
logger.d("leave company wifi") | |
appendMavenSettingsFileNameWithBackup() | |
end | |
if connectToHomeWifi(newSSID, lastSSID) then | |
logger.d("connect to home wifi") | |
hs.audiodevice.defaultOutputDevice():setMuted(false) | |
logger.d("audio device set to unmuted") | |
end | |
if connectToWifiExceptCompany(newSSID, lastSSID) then | |
logger.d("connect to " .. newSSID .. " wifi") | |
hs.audiodevice.defaultOutputDevice():setMuted(false) | |
logger.d("audio device set to unmuted") | |
end | |
lastSSID = newSSID | |
end | |
-- trigger when wifi connect | |
wifiWatcher = hs.wifi.watcher.new(ssidChangedCallback) | |
wifiWatcher:start() | |
local obj={} | |
local function curl_callback(exitCode, stdOut, stdErr) | |
if exitCode == 0 then | |
obj.task = nil | |
-- obj.last_pic = hs.http.urlParts(obj.full_url).lastPathComponent | |
-- local localpath = os.getenv("HOME") .. "/.Trash/" .. hs.http.urlParts(obj.full_url).lastPathComponent | |
local localpath = os.getenv("HOME") .. "/.Trash/" .. obj.pic_name | |
logger.i('tell application "System Events" to tell every desktop to set picture to "' .. localpath .. '"') | |
-- hs.osascript.applescriptFromFile("/Users/laughing/Shells/changeWallpapers.scpt") | |
hs.osascript.applescript('tell application "System Events" to tell every desktop to set picture to "' .. localpath .. '"') | |
-- screens = hs.screen.allScreens() | |
-- for index, screen in ipairs(screens) do | |
-- screen:desktopImageURL("file://" .. localpath) | |
-- end | |
-- os.remove(localpath) | |
else | |
print(stdOut, stdErr) | |
end | |
end | |
local function bingRequest() | |
hs.notify.show("HammerSpoon", "change wallpapers", "Ok, let's change the wallpaper from bing") | |
local user_agent_str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" | |
local json_req_url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" | |
hs.http.asyncGet(json_req_url, {["User-Agent"]=user_agent_str}, function(stat, body, header) | |
if stat == 200 then | |
if pcall(function() hs.json.decode(body) end) then | |
local decode_data = hs.json.decode(body) | |
local pic_url = decode_data.images[1].url | |
-- local pic_name = hs.http.urlParts(pic_url).lastPathComponent | |
-- if obj.last_pic ~= pic_name then | |
obj.full_url = "https://www.bing.com" .. pic_url | |
if obj.task then | |
obj.task:terminate() | |
obj.task = nil | |
end | |
local time = os.time(os.date("!*t")) | |
-- local localpath = os.getenv("HOME") .. "/.Trash/" .. hs.http.urlParts(obj.full_url).lastPathComponent | |
obj.pic_name = time | |
local localpath = os.getenv("HOME") .. "/.Trash/" .. obj.pic_name | |
-- os.remove(localpath) | |
obj.task = hs.task.new("/usr/bin/curl", curl_callback, {"-A", user_agent_str, obj.full_url, "-o", localpath}) | |
obj.task:start() | |
-- end | |
end | |
else | |
logger.e("Bing URL request failed! state:" .. stat) | |
end | |
end) | |
end | |
timer = hs.timer.doEvery(5 * 60 * 60, function() bingRequest() end, true) | |
timer:setNextTrigger(5) | |
timer:start() | |
screenWatcher = hs.screen.watcher.new(function() bingRequest() end) | |
screenWatcher:start() | |
-- local htmlparser = require("htmlparser") | |
-- local function kafkaLagStatus() | |
-- local user_agent_str = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4" | |
-- local kafkaLagHtml = "http://kafka-manager.qipeipu.net/clusters/online/consumers/ofc-service-online/topic/OFC_PURCHASE_FINISH/type/ZK" | |
-- hs.http.asyncGet(kafkaLagHtml, {["User-Agent"]=user_agent_str}, function(stat,body,header) | |
-- if stat == 200 then | |
-- local root = htmlparser.parse(body) | |
-- local trs = root(".table tr") | |
-- local partition0Lag = trs[4]("td")[4]:getcontent() | |
-- local partition1Lag = trs[5]("td")[4]:getcontent() | |
-- partition0Lag = string.gsub(partition0Lag, ",", "") | |
-- partition1Lag = string.gsub(partition1Lag, ",", "") | |
-- partition0Lag = string.gsub(partition0Lag, " ", "") | |
-- partition1Lag = string.gsub(partition1Lag, " ", "") | |
-- if tonumber(partition0Lag) > 10 or tonumber(partition1Lag) > 10 then | |
-- hs.notify.show("HammerSpoon", "kafka moniter", "You got a kafka lag problem") | |
-- end | |
-- logger.i("kafka partition 0 lag: " .. partition0Lag) | |
-- logger.i("kafka partition 1 lag: " .. partition1Lag) | |
-- else | |
-- logger.e("kafka lag request failed!") | |
-- end | |
-- end) | |
-- end | |
-- kafkaMoniterTimer = hs.timer.doEvery(60, function() kafkaLagStatus() end, true) | |
-- kafkaMoniterTimer:setNextTrigger(5) | |
-- kafkaMoniterTimer:start() | |
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "H", function() bingRequest() end) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment