updated to use new returned data format

This commit is contained in:
Paul Liverman III 2018-03-25 13:43:34 -07:00
parent d7ced8d1a4
commit ca8bb9b6a9
2 changed files with 84 additions and 44 deletions

View File

@ -8,40 +8,62 @@ local http = require("socket.http")
local receive = thread.getChannel("send-itchy") local receive = thread.getChannel("send-itchy")
local send = thread.getChannel("receive-itchy") local send = thread.getChannel("receive-itchy")
local check local check
check = function(data, send_errors) check = function(data)
if send_errors == nil then
send_errors = true
end
local exponential_backoff = 1 local exponential_backoff = 1
while true do while true do
local body, status local _continue_0 = false
if data.url then repeat
body, status = http.request(data.url) do
elseif data.proxy then local result = { }
body, status = http.request(tostring(data.proxy) .. "/get/https://itch.io/api/1/x/wharf/latest?target=" .. tostring(data.target) .. "&channel_name=" .. tostring(data.channel)) if data.url then
end result.body, result.status = http.request(data.url)
if status == 200 then elseif data.proxy then
local latest = body:match('%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*') if not (data.target) then
send:push(latest) result.message = "'target' or 'url' must be defined!"
return true send:push(result)
else return false
if send_errors then end
send:push("unknown, error getting latest version: HTTP " .. tostring(status) .. ", trying again in " .. tostring(exponential_backoff) .. " seconds") result.body, result.status = http.request(tostring(data.proxy) .. "/get/https://itch.io/api/1/x/wharf/latest?target=" .. tostring(data.target) .. "&channel_name=" .. tostring(data.channel))
end
if not (result.body) then
result.message = "socket.http.request error: " .. tostring(status)
send:push(result)
return false
end
result.version = result.body:match('%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*')
result.version = tonumber(result.version) or result.version
if data.version then
result.latest = result.version == data.version
end
if status ~= 200 and (not version) then
result.message = "unknown, error getting latest version: HTTP " .. tostring(status) .. ", trying again in " .. tostring(exponential_backoff) .. " seconds"
send:push(result)
timer.sleep(exponential_backoff)
exponential_backoff = exponential_backoff * 2
_continue_0 = true
break
elseif latest ~= nil then
if latest then
result.message = tostring(result.version) .. ", you have the latest version"
else
result.message = tostring(result.version) .. ", there is a newer version available!"
end
else
result.message = result.version
end
send:push(result)
return true
end end
timer.sleep(exponential_backoff) _continue_0 = true
exponential_backoff = exponential_backoff * 2 until true
if not _continue_0 then
break
end end
end end
end end
local start local start
start = function() start = function()
local data = receive:demand() local data = receive:demand()
if not (data.target) then
error("Target undefined. Cannot search for latest version of unknown target!")
end
if not (data.version) then
data.version = "0"
end
if not (data.proxy or data.url) then if not (data.proxy or data.url) then
data.proxy = "http://104.236.139.220:16343" data.proxy = "http://104.236.139.220:16343"
end end
@ -69,8 +91,9 @@ start = function()
timer.sleep(data.interval) timer.sleep(data.interval)
if receive:getCount() > 0 then if receive:getCount() > 0 then
return start() return start()
else
check(data)
end end
check(data, data.send_interval_errors)
end end
end end
end end

View File

@ -5,32 +5,49 @@ http = require "socket.http"
receive = thread.getChannel "send-itchy" receive = thread.getChannel "send-itchy"
send = thread.getChannel "receive-itchy" send = thread.getChannel "receive-itchy"
check = (data, send_errors=true) -> check = (data) ->
exponential_backoff = 1 exponential_backoff = 1
while true while true
local body, status result = {}
if data.url if data.url
body, status = http.request data.url result.body, result.status = http.request data.url
elseif data.proxy elseif data.proxy
body, status = http.request "#{data.proxy}/get/https://itch.io/api/1/x/wharf/latest?target=#{data.target}&channel_name=#{data.channel}" unless data.target
result.message = "'target' or 'url' must be defined!"
send\push result
return false
result.body, result.status = http.request "#{data.proxy}/get/https://itch.io/api/1/x/wharf/latest?target=#{data.target}&channel_name=#{data.channel}"
if status == 200 unless result.body
latest = body\match '%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*' result.message = "socket.http.request error: #{status}"
send\push latest send\push result
return true return false
else
if send_errors result.version = result.body\match '%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*'
send\push "unknown, error getting latest version: HTTP #{status}, trying again in #{exponential_backoff} seconds" result.version = tonumber(result.version) or result.version
result.latest = if data.version
result.version == data.version
if status != 200 and (not version)
result.message = "unknown, error getting latest version: HTTP #{status}, trying again in #{exponential_backoff} seconds"
send\push result
timer.sleep exponential_backoff timer.sleep exponential_backoff
exponential_backoff = exponential_backoff * 2 exponential_backoff *= 2
continue
elseif latest != nil
if latest
result.message = "#{result.version}, you have the latest version"
else
result.message = "#{result.version}, there is a newer version available!"
else
result.message = result.version
send\push result
return true
start = -> start = ->
-- data should be a table of information -- data should be a table of information
data = receive\demand! data = receive\demand!
unless data.target
error "Target undefined. Cannot search for latest version of unknown target!"
data.version = "0" unless data.version
data.proxy = "http://104.236.139.220:16343" unless data.proxy or data.url data.proxy = "http://104.236.139.220:16343" unless data.proxy or data.url
-- channel can be autodetected if not specified -- channel can be autodetected if not specified
@ -61,7 +78,7 @@ start = ->
-- if we are sent new data, start over entirely -- if we are sent new data, start over entirely
if receive\getCount! > 0 if receive\getCount! > 0
return start! return start!
else
check data, data.send_interval_errors check data
start! start!