Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local json = require("json")
- local xml = require("xml").newParser()
- local http = require("socket.http")
- -- alt version2, handles cycles, functions, booleans, etc
- -- - abuse to http://richard.warburton.it
- -- output almost identical to print(table.show(t)) below.
- function print_r (t, name, indent)
- local tableList = {}
- function table_r (t, name, indent, full)
- local serial=string.len(full) == 0 and name
- or type(name)~="number" and '["'..tostring(name)..'"]' or '['..name..']'
- io.write(indent,serial,' = ')
- if type(t) == "table" then
- if tableList[t] ~= nil then io.write('{}; -- ',tableList[t],' (self reference)\n')
- else
- tableList[t]=full..serial
- if next(t) then -- Table not empty
- io.write('{\n')
- for key,value in pairs(t) do table_r(value,key,indent..'\t',full..serial) end
- io.write(indent,'};\n')
- else io.write('{};\n') end
- end
- else io.write(type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"'
- or tostring(t),';\n') end
- end
- table_r(t,name or '__unnamed__',indent or '','')
- end
- local cities = {
- { name="Moscow", country="Russia"},
- { name="Kiev", country="Ukraine"},
- { name="Los%20Angeles", country="US"}
- }
- local data = {}
- for i,location in pairs(cities) do
- local response = nil
- response = http.request("http://www.google.com/ig/api?weather="..location.name.."&hl=en")
- local days = {}
- local t = xml:ParseXmlText(response)
- for i=2,#t.child[1].child do
- local day = nil
- day = t.child[1].child[i].child
- --print_r(day)
- newDay = {}
- for name,property in pairs(day) do
- newDay[property.name] = property.properties.data
- if newDay.day then
- days[newDay.day] = newDay
- else
- days["current"] = newDay
- end
- end
- --print_r(newDay)
- end
- data[location.name] = days
- --print_r(days)
- --os.exit()
- end
- print_r(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement