Unfuck paczkomate a bit

master
q3k 2015-09-27 18:36:41 +02:00
parent fe7a360d2e
commit d4ac18254e
1 changed files with 25 additions and 14 deletions

View File

@ -24,6 +24,15 @@ local function ParsePackXML(XML)
return XML:sub(Start+1, End-1)
end
local function GetPackStatus(ID)
local URL = "http://api.paczkomaty.pl/?do=getpackstatus&packcode=" .. ID
local Body, Code, Headers = http.request(URL)
if Code ~= 200 then
return nil
end
return ParsePackXML(Body)
end
local function CheckMate(IRC)
local Now = os.time()
local Redis = GetRedisClient()
@ -34,19 +43,13 @@ local function CheckMate(IRC)
for _, ID in pairs(Redis:smembers("paczkomate:ids")) do
local LastChecked = tonumber(Redis:hget("paczkomate:objects:" .. ID, "lastchecked"))
local LastStatus = Redis:hget("paczkomate:objects:" .. ID, "status")
if os.time() > LastChecked + 600 then
Redis:hset("paczkomate:objects:" .. ID, "lastchecked", os.time())
local URL = "http://api.paczkomaty.pl/?do=getpackstatus&packcode=" .. ID
local Body, Code, Headers = http.request(URL)
if Code == 200 then
local NewStatus = ParsePackXML(Body)
if Now > LastChecked + 600 then
Redis:hset("paczkomate:objects:" .. ID, "lastchecked", Now)
local NewStatus = GetPackStatus(ID)
if NewStatus ~= LastStatus then
Redis:hset("paczkomate:objects:" .. ID, "status", NewStatus)
if NewStatus ~= nil then
if NewStatus ~= LastStatus then
local Message = string.format("Paczkomate %s status changed (%s -> %s)", ID, LastStatus, NewStatus)
IRC:Say("#hackerspace-pl-members", Message)
end
end
local Message = string.format("Paczkomate %s status changed (%s -> %s)", ID, LastStatus, NewStatus)
IRC:Say("#hackerspace-pl-members", Message)
end
end
end
@ -72,15 +75,23 @@ plugin.AddHook('irc.Message', 'testmate', function(Username, Channel, Message)
end
end)
plugin.AddCommand('list-paczkomate', 0, function(Username, Channel, Message)
plugin.AddCommand('list-paczkomate', 0, function(Username, Channel)
if Channel.Name ~= "#hackerspace-pl-members" then
Channel:Say("#hackerspace-pl-members only, dear.")
return
end
local Redis = GetRedisClient()
for _, ID in pairs(Redis:smembers("paczkomate:ids")) do
local LastChecked = tonumber(Redis:hget("paczkomate:objects:" .. ID, "lastchecked"))
local LastStatus = Redis:hget("paczkomate:objects:" .. ID, "status")
Channel:Say(string.format(" - %s, status: %s", ID, LastStatus))
end
end)
plugin.AddCommand('check-paczkomate', 0, function(Username, Channel)
if Channel.Name ~= "#hackerspace-pl-members" then
Channel:Say("#hackerspace-pl-members only, dear.")
return
end
CheckMate(Channel._irc)
Channel:Say("Done.")
end)