mangoboy121212

Untitled

Feb 13th, 2025
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. local function get_timedate(input)
  2. cities = {okla=0, anna=1, seat=-2}
  3. timestamp = os.epoch('local')/1000 + cities[input]*3600
  4. date = os.date('%B %d, %Y - %I:%M', timestamp)
  5. return date
  6. end
  7.  
  8. local function temp_unit(temp, unit)
  9. return temp .. ' ' .. unit
  10. end
  11.  
  12. local function get_temperature(hi, lo, current, apparent, unit)
  13. daily = 'High: ' .. temp_unit(hi, unit) .. ', Low: ' .. temp_unit(lo, unit)
  14. current = 'Current: ' .. temp_unit(current, unit)
  15. apparent = 'Feels like ' .. temp_unit(apparent, unit)
  16. return daily, current, apparent
  17. end
  18.  
  19. local function parse_response(input, content)
  20. weather_data = textutils.unserializeJSON(content)
  21. daily_data = weather_data['daily']
  22. current_data = weather_data['current']
  23. units = weather_data['current_units']
  24.  
  25. cities = {okla='Oklahoma City, OK', anna='Annapolis, MD', seat='Seattle, WA'}
  26. city = 'City: ' .. cities[input]
  27.  
  28. time = get_timedate(input)
  29.  
  30. hi, lo = daily_data['temperature_2m_max'][1], daily_data['temperature_2m_min'][1]
  31. cur, app = current_data['temperature_2m'], current_data['apparent_temperature']
  32. unit = string.char(176) .. 'F'
  33. daily_temp, current_temp, apparent_temp = get_temperature(hi, lo, cur, app, unit)
  34.  
  35. windspeed = 'Windspeed: ' .. current_data['wind_speed_10m'] .. ' ' .. units['wind_speed_10m']
  36. precip = 'Precipitation: ' .. daily_data['precipitation_sum'][1] .. ' ' .. units['precipitation']
  37.  
  38. return {city, time, daily_temp, current_temp, apparent_temp, windspeed, precip}
  39. end
  40.  
  41. local function display_weather(screen, weather_strings)
  42. screen.clear()
  43. for i=1, #weather_strings do
  44. screen.setCursorPos(1, i)
  45. screen.write(weather_strings[i])
  46. end
  47. end
  48.  
  49. local function make_request_link(input)
  50. cities = {okla={35.4676, -97.5164, 'Chicago'}, anna={38.9784, -76.4921, 'New_York'}, seat={47.6062, -122.3321, 'Los_Angeles'}}
  51. lat = cities[input][1]
  52. long = cities[input][2]
  53. timezone = cities[input][3]
  54. request_link = 'https://api.open-meteo.com/v1/forecast?latitude=' .. lat .. '&longitude=' .. long .. '&current=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'
  55. return request_link
  56. end
  57.  
  58. screen = peripheral.wrap('left')
  59. screen.setTextScale(3)
  60.  
  61. args = { ... }
  62. request_link = make_request_link(args[1])
  63.  
  64. while true do
  65. resp = http.get(request_link)
  66. weather_strings = parse_response(args[1], resp.readAll())
  67. display_weather(screen, weather_strings)
  68. sleep(60)
  69. end
Add Comment
Please, Sign In to add comment