Advertisement
kopilo

Grab melbourne weather

May 14th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.16 KB | None | 0 0
  1. --requires packages
  2. -- lua-socket--
  3. -- loads the HTTP module and any libraries it requires
  4. local ftp = require("socket.ftp")
  5. local body, head = ftp.get("ftp://ftp2.bom.gov.au/anon/gen/fwo/IDA00003.dat")
  6. local format = ""
  7. --converts string to lines
  8. function magiclines(s)
  9.     if s:sub(-1)~="\n" then s=s.."\n" end
  10.         return s:gmatch("(.-)\n")
  11. end
  12.  
  13. function createWeatherTable(s)
  14.     local datatable = {}
  15.     local keys = {}
  16.     --generate temporary format table
  17.     local i = 0
  18.     for value in string.gmatch(format, '([^#]+)') do
  19.         keys[i] = value
  20.         i = i + 1
  21.     end
  22.     i = 0
  23.     for value in string.gmatch(s, '([^#]+)') do
  24.         if((string.match(keys[i], "max") or string.match(keys[i], "min")) and tonumber(value) == nil ) then
  25.             datatable[keys[i]] = " "
  26.             i = i + 1
  27.         else
  28.         --replace i with value from other table
  29.         datatable[keys[i]] = value
  30.         --basic output 
  31.         print(keys[i]..": "..datatable[keys[i]])   
  32.         i = i + 1
  33.         end
  34.     end
  35.    
  36. end
  37.  
  38. --main
  39. local i = true;
  40. for line in magiclines(body) do
  41.     --grab the first line
  42.     if(i) then
  43.         i = false
  44.         format = line
  45.     end
  46.     --print values
  47.     if string.match(line, "Melbourne") then
  48.         createWeatherTable(line)
  49.         break
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement