Team Ai
Apppublic

typesdigital/TD-OpenWeatherMap-API

sourceHugging Faceunlicenseupdated 3y agoView on Hugging Face
1likes
app.py37 linesDownload Raw Back to root
1import requests2 3def get_weather_data(api_key, city):4    base_url = "http://api.openweathermap.org/data/2.5/weather"5    params = {"q": city, "appid": api_key, "units": "metric"}6 7    response = requests.get(base_url, params=params)8 9    if response.status_code == 200:10        return response.json()11    else:12        print("Failed to fetch weather data.")13        return None14 15def display_weather_info(weather_data):16    if weather_data:17        city = weather_data["name"]18        weather = weather_data["weather"][0]["description"]19        temperature = weather_data["main"]["temp"]20        wind_speed = weather_data["wind"]["speed"]21 22        print(f"Weather in {city}: {weather}")23        print(f"Temperature: {temperature}°C")24        print(f"Wind Speed: {wind_speed} m/s")25    else:26        print("Weather data is unavailable.")27 28def main():29    api_key = "1aafc3163909c1493596da9340e00aee"  # Replace with your OpenWeatherMap API key30    city = input("Enter a city name: ")31 32    weather_data = get_weather_data(api_key, city)33    display_weather_info(weather_data)34 35if __name__ == "__main__":36    main()37