-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapms.lua
40 lines (34 loc) · 1.09 KB
/
apms.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
local requests = require 'requests'
local cjson = require 'cjson'
local apms = {}
apms.url_template = "http://www.apms.si/response.ajax.php?com=voznired&task=get&datum=%s&postaja_od=%s&postaja_do=%s"
-- helper functions
local function repair_year(val)
if val < 2000 then return val + 2000 end
return val -- else
end
function apms:get(date, from, to)
local d -- first handle the date
if type(date) == "table" then d = string.format("%d.%d.%d", date[1],date[2],repair_year(date[3])) end
if type(date) == "string" then d = date end
local st = { -- stations
string.gsub(from, " ", "+"),
string.gsub(to, " ", "+")
}
local res = requests.get(string.format(self.url_template, d, st[1], st[2]))
if res.status_code ~= 200 then return res.status_code, "STATUS" end
local decoded = cjson.decode(res.text)
local out = {}
for _, v in ipairs(decoded) do
for _, p in ipairs(v.potek_voznje) do
if from == p.postajalisce then
table.insert(out, {
['station'] = p.postajalisce,
['at'] = p.odhod
})
end
end
end
return out
end
return apms