xrforge-v2/src/.lua/apiSync.lua

74 lines
2.1 KiB
Lua
Raw Normal View History

-- this is a helper/orchestrator for apicalls/syncs
--
-- implemented in openbrush.lua
local apiSync = {
init = function(pluginname)
local plugin = app.plugin[pluginname]
local api = {}
api.pluginname = pluginname
if not plugin.apiCall or not plugin.fetch or not plugin.download then
print("apiSync.lua: plugin " .. pluginname .. " does not implement the apiSync interface")
os.exit()
end
api.apiCall = plugin.apiCall
api.fetch = plugin.fetch
api.download = plugin.download
api.syncHashtags = function(opts)
local synced = 0
local tags = opts.tags
if #tags == 0 then table.insert(tags,'') end
foreach( tags, function(k,tag)
local url = opts.apiCall( #tag > 0 and tag or nil)
opts.body = nil -- erase previous url result
print('🤞 fetch ' .. url)
local t = opts.fetch(url,opts)
if t then
synced = synced + opts.download(t,tag)
end
end, true)
if synced > 0 then
app.cache.url = {} -- delete page cache
app.plugin.diskscan.scan(app.opts.plugin_diskscan_dir)
end
return synced
end
api.sync = function(api)
if #app.opts['plugin_' .. pluginname .. '_sync_URL'] > 0 and util.cmdExist('wget') then
local synced = api.syncHashtags({
tags = util.split( app.opts['plugin_' .. api.pluginname .. '_sync_hashtag'] or '', ' '),
apiCall = api.apiCall,
fetch = api.fetch,
download = api.download
})
end
end
-- runs sync in foreground
app.on('reset', function()
api.sync(api)
end)
-- runs sync in foreground
app.on('init', function()
if util.contains(argv, 'generate') then api.sync(api) end
end)
-- runs sync background
app.on('scheduler', function(opts)
if util.contains(argv, 'server') then
if opts.interval == app.opts['plugin_' .. pluginname .. '_sync_when'] then
api.sync(api)
end
end
end)
return api
end
}
return apiSync