Browse Source

Update bing daily to save Pictures

ubuntu_ci
Ein Verne 4 years ago
parent
commit
6ee4fcb124
No known key found for this signature in database
GPG Key ID: 926634D64ACAD792
  1. 32
      hammerspoon/Spoons/BingDaily.spoon/init.lua

32
hammerspoon/Spoons/BingDaily.spoon/init.lua

@ -3,6 +3,7 @@
--- Use Bing daily picture as your wallpaper, automatically. --- 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) --- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/BingDaily.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/BingDaily.spoon.zip)
log = hs.logger.new('BingDaily', 'debug')
local obj={} local obj={}
obj.__index = obj obj.__index = obj
@ -13,13 +14,13 @@ obj.version = "1.0"
obj.author = "ashfinal <[email protected]>" obj.author = "ashfinal <[email protected]>"
obj.homepage = "https://github.com/Hammerspoon/Spoons" obj.homepage = "https://github.com/Hammerspoon/Spoons"
obj.license = "MIT - https://opensource.org/licenses/MIT" obj.license = "MIT - https://opensource.org/licenses/MIT"
obj.bing_path = os.getenv("HOME") .. "/Pictures/Bing/"
local function curl_callback(exitCode, stdOut, stdErr) local function curl_callback(exitCode, stdOut, stdErr)
if exitCode == 0 then if exitCode == 0 then
obj.task = nil obj.task = nil
obj.last_pic = hs.http.urlParts(obj.full_url).lastPathComponent log.i("callback " .. obj.localpath)
local localpath = os.getenv("HOME") .. "/.Trash/" .. hs.http.urlParts(obj.full_url).lastPathComponent hs.screen.mainScreen():desktopImageURL("file://" .. obj.localpath)
hs.screen.mainScreen():desktopImageURL("file://" .. localpath)
else else
print(stdOut, stdErr) print(stdOut, stdErr)
end end
@ -27,33 +28,46 @@ end
local function bingRequest() 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" 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" local json_req_url = "https://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) hs.http.asyncGet(json_req_url, {["User-Agent"]=user_agent_str}, function(stat,body,header)
if stat == 200 then if stat == 200 then
if pcall(function() hs.json.decode(body) end) then if pcall(function() hs.json.decode(body) end) then
local decode_data = hs.json.decode(body) local decode_data = hs.json.decode(body)
local pic_url = decode_data.images[1].url local pic_url = decode_data.images[1].url
local pic_name = hs.http.urlParts(pic_url).lastPathComponent -- local pic_name = hs.http.urlParts(pic_url).lastPathComponent
if obj.last_pic ~= pic_name then local pic_content_name = decode_data.images[1].copyright
local pic_name = decode_data.images[1].fullstartdate .. "_" .. pic_content_name .. ".jpg"
local pic_name = pic_name:gsub("/", "-")
log.i(pic_name)
local localpath = obj.bing_path .. pic_name
if obj.localpath ~= localpath then
obj.full_url = "https://www.bing.com" .. pic_url obj.full_url = "https://www.bing.com" .. pic_url
if obj.task then if obj.task then
obj.task:terminate() obj.task:terminate()
obj.task = nil obj.task = nil
end end
local localpath = os.getenv("HOME") .. "/.Trash/" .. hs.http.urlParts(obj.full_url).lastPathComponent obj.localpath = localpath
obj.task = hs.task.new("/usr/bin/curl", curl_callback, {"-A", user_agent_str, obj.full_url, "-o", localpath}) obj.task = hs.task.new("/usr/bin/curl", curl_callback, {"-A", user_agent_str, obj.full_url, "-o", localpath})
obj.task:start() obj.task:start()
end end
end end
else else
print("Bing URL request failed!") log.i("Bing URL request failed!")
end end
end) end)
end end
function create_dir(path)
if hs.fs.dir(path) == nil then
hs.fs.mkdir(path)
log.i("path: " .. path .. " create successfully!")
end
end
function obj:init() function obj:init()
create_dir(obj.bing_path)
if obj.timer == nil then if obj.timer == nil then
obj.timer = hs.timer.doEvery(3*60*60, function() bingRequest() end) obj.timer = hs.timer.doEvery(60, function() bingRequest() end)
obj.timer:setNextTrigger(5) obj.timer:setNextTrigger(5)
else else
obj.timer:start() obj.timer:start()

Loading…
Cancel
Save