Advertisement
nonogamer9

weather app for ComputerCraft

May 13th, 2024 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | Gaming | 0 0
  1. -- Function to parse JSON response
  2. local function parseJson(jsonString)
  3.     local obj = {}
  4.     local stack = {}
  5.     local curr = obj
  6.     local key
  7.     local inString = false
  8.     local stringValue = ""
  9.  
  10.     for i = 1, #jsonString do
  11.         local c = jsonString:sub(i, i)
  12.  
  13.         if c == "{" and not inString then
  14.             local newTable = {}
  15.             if key then
  16.                 curr[key] = newTable
  17.                 table.insert(stack, curr)
  18.                 curr = newTable
  19.             else
  20.                 table.insert(stack, curr)
  21.                 curr = newTable
  22.             end
  23.             key = nil
  24.         elseif c == "}" and not inString then
  25.             curr = table.remove(stack)
  26.         elseif c == "\"" then
  27.             inString = not inString
  28.         elseif c == ":" and not inString then
  29.             key = stringValue
  30.             stringValue = ""
  31.         elseif c == "," and not inString then
  32.             if key then
  33.                 curr[key] = stringValue
  34.                 key = nil
  35.             else
  36.                 table.insert(curr, stringValue)
  37.             end
  38.             stringValue = ""
  39.         else
  40.             stringValue = stringValue .. c
  41.         end
  42.     end
  43.  
  44.     return obj
  45. end
  46. -- Function to URL encode a string
  47. local function urlEncode(str)
  48.     if str then
  49.         str = string.gsub(str, "([^%w ])", function(c)
  50.             return string.format("%%%02X", string.byte(c))
  51.         end)
  52.         str = string.gsub(str, " ", "+")
  53.     end
  54.     return str
  55. end
  56.  
  57. -- Function to fetch weather data from the API
  58. local function fetchWeatherData(city)
  59.     local apiKey = "771196291bcb63791f108494bc9ce166"  -- Your API key here
  60.     local encodedCity = urlEncode(city)
  61.     local apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" .. encodedCity .. "&appid=" .. apiKey
  62.  
  63.     local response = ""
  64.     local request = http.get(apiUrl)
  65.     if request then
  66.         response = request.readAll()
  67.         request.close()
  68.     else
  69.         print("Failed to fetch weather data from the API")
  70.     end
  71.  
  72.     return response
  73. end
  74.  
  75.  
  76. -- Function to display weather data
  77. local function displayWeather(weatherData)
  78.     if weatherData and weatherData.main then
  79.         print("Weather Data:")
  80.         print("City: " .. (weatherData.name or "Unknown"))
  81.         print("Temperature: " .. (weatherData.main.temp and (weatherData.main.temp .. "°C") or "N/A"))
  82.         print("Humidity: " .. (weatherData.main.humidity and (weatherData.main.humidity .. "%") or "N/A"))
  83.         print("Wind Speed: " .. (weatherData.wind and weatherData.wind.speed and (weatherData.wind.speed .. " m/s") or "N/A"))
  84.     else
  85.         print("Failed to fetch weather data.")
  86.     end
  87. end
  88.  
  89. -- Main function
  90. local function main()
  91.     print("Enter a city name:")
  92.     local city = read()
  93.     local weatherDataResponse = fetchWeatherData(city)
  94.  
  95.     if not weatherDataResponse then
  96.         return
  97.     end
  98.  
  99.     local parsedData = parseJson(weatherDataResponse)
  100.     displayWeather(parsedData)
  101. end
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement