|
| 1 | +import requests |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +API_KEY = '7136d46d4edf4c9bbcf124053242205' |
| 6 | +BASE_URL = 'http://api.weatherapi.com/v1/' |
| 7 | + |
| 8 | +def get_weather_data(city): |
| 9 | + current_weather_url = f"{BASE_URL}current.json?key={API_KEY}&q={city}" |
| 10 | + forecast_url = f"{BASE_URL}forecast.json?key={API_KEY}&q={city}&days=5" |
| 11 | + |
| 12 | + current_weather_response = requests.get(current_weather_url) |
| 13 | + forecast_response = requests.get(forecast_url) |
| 14 | + |
| 15 | + if current_weather_response.status_code == 200 and forecast_response.status_code == 200: |
| 16 | + current_weather = current_weather_response.json() |
| 17 | + forecast = forecast_response.json() |
| 18 | + return current_weather, forecast |
| 19 | + else: |
| 20 | + print(f"Error fetching weather data. Status codes: {current_weather_response.status_code}, {forecast_response.status_code}") |
| 21 | + if current_weather_response.status_code != 200: |
| 22 | + print(f"Current weather response: {current_weather_response.text}") |
| 23 | + if forecast_response.status_code != 200: |
| 24 | + print(f"Forecast response: {forecast_response.text}") |
| 25 | + return None, None |
| 26 | + |
| 27 | +def display_current_weather(current_weather): |
| 28 | + print("Current Weather:") |
| 29 | + location = current_weather['location'] |
| 30 | + current = current_weather['current'] |
| 31 | + print(f"Location: {location['name']}, {location['country']}") |
| 32 | + print(f"Temperature: {current['temp_c']}°C") |
| 33 | + print(f"Weather: {current['condition']['text']}") |
| 34 | + print(f"Humidity: {current['humidity']}%") |
| 35 | + print(f"Wind Speed: {current['wind_kph']} kph") |
| 36 | + print("-" * 40) |
| 37 | + |
| 38 | +def display_forecast(forecast): |
| 39 | + print("5-Day Forecast:") |
| 40 | + forecast_days = forecast['forecast']['forecastday'] |
| 41 | + for day in forecast_days: |
| 42 | + date = day['date'] |
| 43 | + day_info = day['day'] |
| 44 | + temp = day_info['avgtemp_c'] |
| 45 | + weather = day_info['condition']['text'] |
| 46 | + print(f"{date}: {temp}°C, {weather}") |
| 47 | + print("-" * 40) |
| 48 | + |
| 49 | +def plot_temperature_trends(forecast): |
| 50 | + dates = [] |
| 51 | + temps = [] |
| 52 | + |
| 53 | + forecast_days = forecast['forecast']['forecastday'] |
| 54 | + for day in forecast_days: |
| 55 | + date = datetime.strptime(day['date'], '%Y-%m-%d') |
| 56 | + temp = day['day']['avgtemp_c'] |
| 57 | + dates.append(date) |
| 58 | + temps.append(temp) |
| 59 | + |
| 60 | + plt.figure(figsize=(10, 5)) |
| 61 | + plt.plot(dates, temps, marker='o', linestyle='-', color='b') |
| 62 | + plt.title('Temperature Trends Over Next 5 Days') |
| 63 | + plt.xlabel('Date') |
| 64 | + plt.ylabel('Temperature (°C)') |
| 65 | + plt.grid(True) |
| 66 | + plt.show() |
| 67 | + |
| 68 | +def main(): |
| 69 | + city = input("Enter the city name: ") |
| 70 | + current_weather, forecast = get_weather_data(city) |
| 71 | + |
| 72 | + if current_weather and forecast: |
| 73 | + display_current_weather(current_weather) |
| 74 | + display_forecast(forecast) |
| 75 | + plot_temperature_trends(forecast) |
| 76 | + else: |
| 77 | + print("Could not retrieve weather data.") |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments