-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_fetch.py
More file actions
32 lines (26 loc) · 1.07 KB
/
Copy pathweather_fetch.py
File metadata and controls
32 lines (26 loc) · 1.07 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
import datetime as dt
import requests
from config import WEATHER_API_KEY
BASE_URL = f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline'
def get_weather_forecast(location):
url = f'{BASE_URL}/{location}?unitGroup=metric&key={WEATHER_API_KEY}'
response = requests.get(url)
data = response.json()
forecast = ""
for day in data['days']:
# Convert date into european format
date = dt.datetime.strptime(day['datetime'],"%Y-%m-%d").strftime("%d.%m.%Y")
# Extract data
min_temp = day['tempmin']
max_temp = day['tempmax']
precip = day['precip']
wind_speed = day['windspeed']
forecast += (f"Datum: {date} \n"
f"Min-Temp: {min_temp}°C, Max-Temp: {max_temp}°C \n"
f"Regenwahrscheinlichkeit: {precip}%, Windgeschwindigkeit: {wind_speed} km/h\n\n")
return forecast
#just nessessary if test only the fetch function
if __name__ == "__main__":
location = 'Berlin'
forecast = get_weather_forecast(location)
print(forecast)