mirror of https://github.com/einverne/dotfiles.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
3.1 KiB
91 lines
3.1 KiB
4 years ago
|
--- === BingDaily ===
|
||
|
---
|
||
|
--- Use Bing daily picture as your wallpaper, automatically.
|
||
|
---
|
||
|
--- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/BingDaily.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/BingDaily.spoon.zip)
|
||
4 years ago
|
log = hs.logger.new('BingDaily', 'debug')
|
||
4 years ago
|
|
||
|
local obj={}
|
||
|
obj.__index = obj
|
||
|
|
||
|
-- Metadata
|
||
|
obj.name = "BingDaily"
|
||
|
obj.version = "1.0"
|
||
|
obj.author = "ashfinal <[email protected]>"
|
||
|
obj.homepage = "https://github.com/Hammerspoon/Spoons"
|
||
|
obj.license = "MIT - https://opensource.org/licenses/MIT"
|
||
4 years ago
|
obj.bing_path = os.getenv("HOME") .. "/Pictures/Bing/"
|
||
4 years ago
|
|
||
|
local function curl_callback(exitCode, stdOut, stdErr)
|
||
|
if exitCode == 0 then
|
||
|
obj.task = nil
|
||
4 years ago
|
local allScreens = hs.screen.allScreens()
|
||
|
for i = 1, #allScreens do
|
||
|
local newScreen = allScreens[i]:desktopImageURL("file://" .. obj.localpath)
|
||
|
log.i(newScreen:desktopImageURL())
|
||
|
end
|
||
|
--hs.screen.mainScreen():desktopImageURL("file://" .. obj.localpath)
|
||
4 years ago
|
else
|
||
|
print(stdOut, stdErr)
|
||
|
end
|
||
|
end
|
||
|
|
||
4 years ago
|
local function writeDescToFile(filename, content)
|
||
|
f = io.open(obj.bing_path .. "/" .. filename .. ".txt", "w")
|
||
|
io.output(f)
|
||
|
io.write(content)
|
||
|
io.close(f)
|
||
|
end
|
||
|
|
||
4 years ago
|
local function bingRequest()
|
||
|
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"
|
||
4 years ago
|
local json_req_url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1"
|
||
4 years ago
|
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
|
||
4 years ago
|
-- local pic_name = hs.http.urlParts(pic_url).lastPathComponent
|
||
|
local pic_content_name = decode_data.images[1].copyright
|
||
4 years ago
|
local fullstartdate = decode_data.images[1].fullstartdate
|
||
|
local pic_name = fullstartdate .. ".jpg"
|
||
|
writeDescToFile(fullstartdate, pic_content_name)
|
||
|
local pic_name = pic_name:gsub("/", "_")
|
||
|
local pic_name = pic_name:gsub(" ", "_")
|
||
4 years ago
|
local localpath = obj.bing_path .. pic_name
|
||
|
if obj.localpath ~= localpath then
|
||
4 years ago
|
obj.full_url = "https://www.bing.com" .. pic_url
|
||
|
if obj.task then
|
||
|
obj.task:terminate()
|
||
|
obj.task = nil
|
||
|
end
|
||
4 years ago
|
obj.localpath = localpath
|
||
4 years ago
|
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
|
||
4 years ago
|
log.i("Bing URL request failed!")
|
||
4 years ago
|
end
|
||
|
end)
|
||
|
end
|
||
|
|
||
4 years ago
|
function create_dir(path)
|
||
|
if hs.fs.dir(path) == nil then
|
||
|
hs.fs.mkdir(path)
|
||
|
log.i("path: " .. path .. " create successfully!")
|
||
|
end
|
||
|
end
|
||
|
|
||
4 years ago
|
function obj:init()
|
||
4 years ago
|
create_dir(obj.bing_path)
|
||
4 years ago
|
if obj.timer == nil then
|
||
4 years ago
|
obj.timer = hs.timer.doEvery(60, function() bingRequest() end)
|
||
4 years ago
|
obj.timer:setNextTrigger(5)
|
||
|
else
|
||
|
obj.timer:start()
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return obj
|