-
Notifications
You must be signed in to change notification settings - Fork 1
/
getWeather.py
executable file
·39 lines (30 loc) · 1.12 KB
/
getWeather.py
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
#!/usr/bin/python
import requests
import os
import time
import json
CACHE_FOLDER = "cache"
CACHE_FILE = os.path.join(CACHE_FOLDER, "netatmo.json")
CACHE_EXPIRY = 300 # 5 minutes in seconds
def get_weather_data():
# Check if cache file exists and is newer than 5 minutes
if os.path.exists(CACHE_FILE) and time.time() - os.path.getmtime(CACHE_FILE) < CACHE_EXPIRY:
with open(CACHE_FILE, "r") as file:
return json.load(file)
url = "https://ekstremedia.no/api/weather/getWeatherForPi"
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for non-2xx status codes
weather_data = response.json()
# Save the data to the cache file
os.makedirs(CACHE_FOLDER, exist_ok=True)
with open(CACHE_FILE, "w") as file:
json.dump(weather_data, file)
return weather_data
except requests.exceptions.RequestException as e:
print("Failed to fetch weather data:", e)
return None
weather_data = get_weather_data()
if weather_data is not None:
# Process the weather data as needed
print(weather_data)