Skip to content

Instantly share code, notes, and snippets.

@alexito4
Created Jan 17, 2015
Embed
What would you like to do?
local wait = 60
os.loadAPI("weather")
local city = "London,uk"
while true do
local commandBlock = peripheral.wrap("back")
local w = weather.getWeather(city)
local command = "weather " .. w .. " " .. wait
print(command)
commandBlock.setCommand(command)
commandBlock.runCommand()
sleep(wait)
end
os.loadAPI("json")
local sun = "clear"
local rain = "rain"
local thunder = "thunder"
-- Relates id with weather
-- http://openweathermap.org/weather-conditions
local typeeq = {
{id = 960, w = "thunder"},
{id = 961, w = "thunder"},
{id = 962, w = "thunder"},
}
local typeless = {
{id = 300, w = "thunder"},
{id = 400, w = "rain"},
{id = 600, w = "rain"},
{id = 700, w = "rain"},
{id = 800, w = "clear"},
{id = 900, w = "clear"},
{id = 1000, w = "thunder"},
}
function getWeather(city)
local url = "http://api.openweathermap.org/data/2.5/weather?q=" .. city
local response = http.get(url)
local responseStr = response.readAll()
local obj = json.decode(responseStr)
local id = tonumber(obj["weather"][1]["id"])
local w = clear
-- print("Id: " .. id)
-- Search for an special id
for i=1,#typeeq do
if typeeq[i]["id"] == id then
w = typeeq[i]["w"]
break
end
end
-- Search for an id in ranges
for i=1,#typeless do
if id < typeless[i]["id"] then
w = typeless[i]["w"]
break
end
end
return w
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment