1. local json = require("json")
  2. local xml = require("xml").newParser()
  3. local http = require("socket.http")
  4.  
  5.  
  6.  
  7. -- alt version2, handles cycles, functions, booleans, etc
  8. --  - abuse to http://richard.warburton.it
  9. -- output almost identical to print(table.show(t)) below.
  10. function print_r (t, name, indent)
  11.   local tableList = {}
  12.   function table_r (t, name, indent, full)
  13.     local serial=string.len(full) == 0 and name
  14.         or type(name)~="number" and '["'..tostring(name)..'"]' or '['..name..']'
  15.     io.write(indent,serial,' = ')
  16.     if type(t) == "table" then
  17.       if tableList[t] ~= nil then io.write('{}; -- ',tableList[t],' (self reference)\n')
  18.       else
  19.         tableList[t]=full..serial
  20.         if next(t) then -- Table not empty
  21.           io.write('{\n')
  22.           for key,value in pairs(t) do table_r(value,key,indent..'\t',full..serial) end
  23.           io.write(indent,'};\n')
  24.         else io.write('{};\n') end
  25.       end
  26.     else io.write(type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"'
  27.                   or tostring(t),';\n') end
  28.   end
  29.   table_r(t,name or '__unnamed__',indent or '','')
  30. end
  31.  
  32.  
  33.  
  34. local cities = {
  35.         { name="Moscow", country="Russia"},
  36.         { name="Kiev", country="Ukraine"},
  37.         { name="Los%20Angeles", country="US"}
  38.     }
  39.  
  40.  
  41. local data = {}
  42. for i,location in pairs(cities) do
  43.  
  44.     local response = nil
  45.     response = http.request("http://www.google.com/ig/api?weather="..location.name.."&hl=en")
  46.  
  47.     local days = {}
  48.     local t = xml:ParseXmlText(response)
  49.     for i=2,#t.child[1].child do
  50.         local day = nil
  51.         day = t.child[1].child[i].child
  52.        
  53.         --print_r(day)
  54.         newDay = {}
  55.         for name,property in pairs(day) do
  56.             newDay[property.name] = property.properties.data
  57.             if newDay.day then
  58.                 days[newDay.day] = newDay  
  59.             else
  60.                 days["current"] = newDay   
  61.             end
  62.         end
  63.         --print_r(newDay)
  64.     end
  65.     data[location.name] = days
  66.     --print_r(days)
  67.     --os.exit()
  68. end
  69. print_r(data)