-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather.py
More file actions
59 lines (45 loc) · 1.69 KB
/
Copy pathweather.py
File metadata and controls
59 lines (45 loc) · 1.69 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
import requests
from datetime import datetime
from pprint import pprint
api_key = '6727ead5dfa64ae630e4a50c6eda4dcb'
lat = 37.7749
lon = -122.4194
units = 'imperial'
api_url = (
f'https://api.openweathermap.org/data/2.5/forecast'
f'?lat={lat}&lon={lon}&units={units}&appid={api_key}'
)
def fetch_weather_forecast(url):
try:
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'An error occurred: {err}')
return None
def process_forecast_data(forecast_data):
print(f'5-day weather forecast for San Francisco (in 3-hour intervals):\n')
daily_data = {}
for forecast in forecast_data.get('list', []):
date = datetime.fromtimestamp(forecast['dt']).strftime('%m/%d/%Y')
temp = forecast['main']['temp']
humidity = forecast['main']['humidity']
if date not in daily_data:
daily_data[date] = {'total_temp': 0, 'total_humidity': 0, 'count': 0}
daily_data[date]['total_temp'] += temp
daily_data[date]['total_humidity'] += humidity
daily_data[date]['count'] += 1
for date, data in daily_data.items():
avg_temp = data['total_temp'] / data['count']
avg_humidity = data['total_humidity'] / data['count']
print(f'Date: {date}')
print(f' Avg Temperature: {avg_temp:.2f}°F')
print(f' Avg Humidity: {avg_humidity:.2f}%\n')
def main():
forecast_data = fetch_weather_forecast(api_url)
if forecast_data:
process_forecast_data(forecast_data)
if __name__ == '__main__':
main()