Advertisement
nonogamer9

weather app for opencomputers

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