Unfuck paczkomate a bit
parent
fe7a360d2e
commit
d4ac18254e
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue