Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function get_timedate(input)
- cities = {okla=0, anna=1, seat=-2}
- timestamp = os.epoch('local')/1000 + cities[input]*3600
- date = os.date('%B %d, %Y - %I:%M', timestamp)
- return date
- end
- local function temp_unit(temp, unit)
- return temp .. ' ' .. unit
- end
- local function get_temperature(hi, lo, current, apparent, unit)
- daily = 'High: ' .. temp_unit(hi, unit) .. ', Low: ' .. temp_unit(lo, unit)
- current = 'Current: ' .. temp_unit(current, unit)
- apparent = 'Feels like ' .. temp_unit(apparent, unit)
- return daily, current, apparent
- end
- local function parse_response(input, content)
- weather_data = textutils.unserializeJSON(content)
- daily_data = weather_data['daily']
- current_data = weather_data['current']
- units = weather_data['current_units']
- cities = {okla='Oklahoma City, OK', anna='Annapolis, MD', seat='Seattle, WA'}
- city = 'City: ' .. cities[input]
- time = get_timedate(input)
- hi, lo = daily_data['temperature_2m_max'][1], daily_data['temperature_2m_min'][1]
- cur, app = current_data['temperature_2m'], current_data['apparent_temperature']
- unit = string.char(176) .. 'F'
- daily_temp, current_temp, apparent_temp = get_temperature(hi, lo, cur, app, unit)
- windspeed = 'Windspeed: ' .. current_data['wind_speed_10m'] .. ' ' .. units['wind_speed_10m']
- precip = 'Precipitation: ' .. daily_data['precipitation_sum'][1] .. ' ' .. units['precipitation']
- return {city, time, daily_temp, current_temp, apparent_temp, windspeed, precip}
- end
- local function display_weather(screen, weather_strings)
- screen.clear()
- for i=1, #weather_strings do
- screen.setCursorPos(1, i)
- screen.write(weather_strings[i])
- end
- end
- local function make_request_link(input)
- cities = {okla={35.4676, -97.5164, 'Chicago'}, anna={38.9784, -76.4921, 'New_York'}, seat={47.6062, -122.3321, 'Los_Angeles'}}
- lat = cities[input][1]
- long = cities[input][2]
- timezone = cities[input][3]
- request_link = 'https://api.open-meteo.com/v1/forecast?latitude=' .. lat .. '&longitude=' .. long .. '¤t=temperature_2m,apparent_temperature,precipitation,wind_speed_10m&daily=temperature_2m_max,temperature_2m_min,precipitation_sum&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&timeformat=unixtime&timezone=America%2F' .. timezone .. '&forecast_days=1'
- return request_link
- end
- screen = peripheral.wrap('left')
- screen.setTextScale(3)
- args = { ... }
- request_link = make_request_link(args[1])
- while true do
- resp = http.get(request_link)
- weather_strings = parse_response(args[1], resp.readAll())
- display_weather(screen, weather_strings)
- sleep(60)
- end
Add Comment
Please, Sign In to add comment