-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
239 lines (199 loc) · 8.05 KB
/
Copy pathmain.py
File metadata and controls
239 lines (199 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
"""
Weather Forecast Application
A functional weather app that fetches current weather and forecasts using OpenWeatherMap API
"""
import os
import requests
import json
from datetime import datetime, timedelta
from typing import Dict, List, Optional
class WeatherApp:
"""Main weather application class"""
BASE_URL = "https://api.openweathermap.org/data/2.5"
def __init__(self, api_key: str):
"""Initialize the weather app with an API key"""
self.api_key = api_key
self.current_weather = None
self.forecast = None
def get_current_weather(self, city: str) -> Optional[Dict]:
"""
Fetch current weather for a given city
Args:
city: City name
Returns:
Dictionary with weather data or None if request fails
"""
try:
params = {
'q': city,
'appid': self.api_key,
'units': 'metric'
}
response = requests.get(f"{self.BASE_URL}/weather", params=params, timeout=5)
response.raise_for_status()
self.current_weather = response.json()
return self.current_weather
except requests.exceptions.RequestException as e:
print(f"⚠️ API Error: {e}")
print(f"📌 Using demo data (get API key from openweathermap.org)")
# Return mock data for demo purposes
self.current_weather = self.get_mock_current_weather(city)
return self.current_weather
def get_forecast(self, city: str, days: int = 5) -> Optional[Dict]:
"""
Fetch weather forecast for a given city
Args:
city: City name
days: Number of days to forecast (default 5)
Returns:
Dictionary with forecast data or None if request fails
"""
try:
params = {
'q': city,
'appid': self.api_key,
'units': 'metric',
'cnt': days * 8 # API returns data in 3-hour intervals
}
response = requests.get(f"{self.BASE_URL}/forecast", params=params, timeout=5)
response.raise_for_status()
self.forecast = response.json()
return self.forecast
except requests.exceptions.RequestException as e:
print(f"⚠️ API Error: {e}")
print(f"📌 Using demo data (get API key from openweathermap.org)")
# Return mock data for demo purposes
self.forecast = self.get_mock_forecast(city)
return self.forecast
def display_current_weather(self) -> None:
"""Display current weather in a formatted way"""
if not self.current_weather:
print("No current weather data. Please fetch it first.")
return
data = self.current_weather
print("\n" + "="*50)
print(f"🌍 CURRENT WEATHER - {data['name']}, {data['sys']['country']}")
print("="*50)
main = data['main']
weather = data['weather'][0]
wind = data['wind']
print(f"Temperature: {main['temp']}°C (feels like {main['feels_like']}°C)")
print(f"Condition: {weather['main']} - {weather['description']}")
print(f"Humidity: {main['humidity']}%")
print(f"Pressure: {main['pressure']} hPa")
print(f"Wind Speed: {wind['speed']} m/s")
print(f"Cloudiness: {data['clouds']['all']}%")
print(f"Sunrise: {datetime.fromtimestamp(data['sys']['sunrise']).strftime('%H:%M:%S')}")
print(f"Sunset: {datetime.fromtimestamp(data['sys']['sunset']).strftime('%H:%M:%S')}")
print("="*50 + "\n")
def display_forecast(self) -> None:
"""Display weather forecast in a formatted way"""
if not self.forecast:
print("No forecast data. Please fetch it first.")
return
data = self.forecast
print("\n" + "="*50)
print(f"📅 WEATHER FORECAST - {data['city']['name']}")
print("="*50)
current_date = None
for item in data['list'][:24]: # Show first 24 hours
dt = datetime.fromtimestamp(item['dt'])
date = dt.strftime('%Y-%m-%d')
time = dt.strftime('%H:%M')
if current_date != date:
current_date = date
print(f"\n📅 {date}")
print("-" * 50)
temp = item['main']['temp']
feels = item['main']['feels_like']
weather = item['weather'][0]['main']
humidity = item['main']['humidity']
print(f" {time} - {temp}°C (feels {feels}°C) | {weather} | 💧 {humidity}%")
print("="*50 + "\n")
def get_mock_current_weather(self, city: str) -> Optional[Dict]:
"""Generate mock current weather data for demo"""
now = datetime.now()
return {
"name": city,
"sys": {
"country": "GB",
"sunrise": int((now - timedelta(hours=6)).timestamp()),
"sunset": int((now + timedelta(hours=6)).timestamp())
},
"main": {
"temp": 12.5,
"feels_like": 10.2,
"humidity": 75,
"pressure": 1013
},
"weather": [{
"main": "Cloudy",
"description": "overcast clouds"
}],
"wind": {"speed": 5.2},
"clouds": {"all": 85}
}
def get_mock_forecast(self, city: str) -> Optional[Dict]:
"""Generate mock forecast data for demo"""
now = datetime.now()
forecast_list = []
for i in range(8):
dt = now + timedelta(hours=i*3)
forecast_list.append({
"dt": int(dt.timestamp()),
"main": {
"temp": 12 + i - 2,
"feels_like": 10 + i - 2,
"humidity": 70 + (i % 3) * 5
},
"weather": [{
"main": ["Sunny", "Cloudy", "Rainy"][i % 3]
}]
})
return {
"city": {"name": city},
"list": forecast_list
}
def main():
"""Main function to run the weather app"""
# Get your free API key from: https://openweathermap.org/api
# Sign up at: https://openweathermap.org/api
API_KEY = os.getenv("OPENWEATHER_API_KEY")
if not API_KEY:
print("❌ API key not found.")
print("Set OPENWEATHER_API_KEY as an environment variable.")
# Replace with your actual API key
app = WeatherApp(API_KEY)
print("\n🌤️ WEATHER FORECAST APPLICATION 🌤️")
print("=" * 50)
print("Note: Using demo data (get' real API key from openweathermap.org)")
while True:
print("\nOptions:")
print("1. Get current weather")
print("2. Get weather forecast")
print("3. Exit")
choice = input("\nEnter your choice (1-3): ").strip()
if choice == '1':
city = input("Enter city name: ").strip()
if city:
print(f"\n⏳ Fetching current weather for {city}...")
if app.get_current_weather(city):
app.display_current_weather()
else:
print(f"❌ Could not fetch weather for {city}")
elif choice == '2':
city = input("Enter city name: ").strip()
if city:
print(f"\n⏳ Fetching forecast for {city}...")
if app.get_forecast(city):
app.display_forecast()
else:
print(f"❌ Could not fetch forecast for {city}")
elif choice == '3':
print("\n👋 Goodbye!")
break
else:
print("❌ Invalid choice. Please try again.")
if __name__ == "__main__":
main()