Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local internet = require("internet")
- -- Function to URL encode a string
- local function urlEncode(str)
- if str then
- str = string.gsub(str, "([^%w ])", function(c)
- return string.format("%%%02X", string.byte(c))
- end)
- str = string.gsub(str, " ", "+")
- end
- return str
- end
- -- Function to fetch weather data from the API
- local function fetchWeatherData(city)
- local apiKey = "771196291bcb63791f108494bc9ce166" -- Your API key here
- local encodedCity = urlEncode(city)
- local apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" .. encodedCity .. "&appid=" .. apiKey
- local response = ""
- for chunk in internet.request(apiUrl) do
- response = response .. chunk
- end
- return response
- end
- -- Function to parse JSON response
- local function parseJson(jsonString)
- local obj = {}
- local stack = {}
- local curr = obj
- local key
- local inString = false
- local stringValue = ""
- for i = 1, #jsonString do
- local c = jsonString:sub(i, i)
- if c == "{" and not inString then
- local newTable = {}
- if key then
- curr[key] = newTable
- key = nil
- else
- table.insert(stack, curr)
- table.insert(stack, key)
- table.insert(stack, i)
- key = nil
- end
- curr = newTable
- elseif c == "}" and not inString then
- local startIndex = table.remove(stack)
- local key = table.remove(stack)
- local parent = table.remove(stack)
- local endIndex = i
- local value = parseJson(jsonString:sub(startIndex, endIndex))
- if key then
- parent[key] = value
- else
- table.insert(parent, value)
- end
- curr = parent
- elseif c == "\"" then
- if not inString then
- inString = true
- stringValue = ""
- else
- inString = false
- if not key then
- key = stringValue
- else
- curr[key] = stringValue
- key = nil
- end
- end
- elseif c == ":" and not inString then
- key = stringValue
- stringValue = ""
- elseif c == "," and not inString then
- if key then
- curr[key] = stringValue
- key = nil
- else
- table.insert(curr, stringValue)
- end
- stringValue = ""
- else
- stringValue = stringValue .. c
- end
- end
- return obj
- end
- -- Function to display weather data
- local function displayWeather(weatherData)
- if weatherData and weatherData.main then
- print("Weather Data:")
- print("City: " .. (weatherData.name or "Unknown"))
- print("Temperature: " .. (weatherData.main.temp and (weatherData.main.temp .. "°C") or "N/A"))
- print("Humidity: " .. (weatherData.main.humidity and (weatherData.main.humidity .. "%") or "N/A"))
- print("Wind Speed: " .. (weatherData.wind and weatherData.wind.speed and (weatherData.wind.speed .. " m/s") or "N/A"))
- else
- print("Failed to fetch weather data.")
- end
- end
- -- Main function
- local function main()
- print("Enter a city name:")
- local city = io.read()
- local weatherDataResponse = fetchWeatherData(city)
- print("Response:")
- print(weatherDataResponse)
- local parsedData = parseJson(weatherDataResponse)
- displayWeather(parsedData)
- end
- -- Call the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement