weather.lua (1327B)
1 local http = require "socket.http" 2 local json = require "json" 3 4 if not args:find("weather") then 5 return nil 6 end 7 8 function urlencode(str) 9 if (str) then 10 str = string.gsub(str, "\n", "\r\n") 11 str = string.gsub(str, "([^%w ])", function (c) return string.format ("%%%02X", string.byte(c)) end) 12 str = string.gsub(str, " ", "+") 13 end 14 return str 15 end 16 17 local f = io.open(get_path("data/memory.json"), "rb") 18 local js = f:read("*all") 19 f:close() 20 local t = json.decode(js) 21 22 units = t["units"] 23 location = t["location"] 24 25 local url = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" .. urlencode(location) .. "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" 26 27 local js = http.request(url) 28 local t = json.decode(js) 29 local w = t["query"]["results"]["channel"] 30 31 local temp = w["item"]["condition"]["temp"] 32 local tempunits = w["units"]["temperature"] 33 local city = w["location"]["city"] 34 local region = w["location"]["region"] 35 local country = w["location"]["country"] 36 37 local place 38 if country == "United States" or country == "United Kingdom" then 39 place = city .. region 40 else 41 place = city .. ", " .. country 42 end 43 44 return "It is " .. temp .. " " .. tempunits .. " out today in " .. place