itchy/check.moon
2018-05-25 14:14:21 -07:00

80 lines
2.3 KiB
Plaintext

require "love.timer"
import thread, timer from love
http = require "socket.http"
check = (data) ->
send = thread.getChannel data.thread_channel or "itchy"
exponential_backoff = 1
while true
result = {}
if data.url
result.body, result.status = http.request data.url
elseif data.proxy
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://api.itch.io/wharf/latest?target=#{data.target}&channel_name=#{data.channel}"
unless result.body
result.message = "socket.http.request error: #{result.status}"
send\push result
return false
result.version = result.body\match '%s*{%s*"latest"%s*:%s*"(.+)"%s*}%s*'
result.version = tonumber(result.version) or result.version
result.latest = if data.version
result.version == data.version
if result.status != 200 and (not result.version)
result.message = "unknown, error getting latest version: HTTP #{result.status}, trying again in #{exponential_backoff} seconds"
send\push result
timer.sleep exponential_backoff
exponential_backoff *= 2
exponential_backoff = 10 * 60 if exponential_backoff > 10 * 60 -- maximum backoff is 10 minutes
continue
elseif result.latest != nil
if result.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
-- data should be a table of information
start = (data) ->
data.proxy = "http://45.55.113.149:16343" unless data.proxy or data.url
-- channel can be autodetected if not specified
unless data.channel
require "love.system"
os = love.system.getOS!
switch os
when "OS X"
data.channel = "osx"
when "Windows"
data.channel = "win32"
when "Linux"
data.channel = "linux"
when "Android"
data.channel = "android"
when "iOS"
data.channel = "ios"
else
data.channel = os
check data
-- if we should check again every x seconds, wait, and do so
if data.interval
while true
timer.sleep data.interval
check data
start(...)