adds download of status within period

This commit is contained in:
bernd2k 2020-01-08 22:45:43 +01:00
parent 06865bf6e4
commit 30567b1fdc
2 changed files with 50 additions and 17 deletions

View file

@ -1,8 +1,11 @@
{
"name": "Krautspace space status",
"path": "",
"status_url" : "https://raw.githubusercontent.com/HackspaceJena/space_status/master/raspberry_pi/status.json",
"status_api": {
"status": {
"url": "https://raw.githubusercontent.com/HackspaceJena/space_status/master/raspberry_pi/status.json",
"checking_period": 86400.0,
"last_time_checked": 0.0
},"status_api": {
"url": "https://status.kraut.space/api",
"standard": "space.api",
"checking_period": 300

View file

@ -62,7 +62,9 @@ with open(file_configure_path, 'r') as f:
data_config = json.load(f)
path = data_config["path"]
STATUS_URL = data_config["status_url"]
STATUS_URL = data_config["status"]["url"]
STATUS_CHECKING_PERIOD = data_config["status"]["checking_period"]
STATUS_LAST_TIME_CHECKED = data_config["status"]["last_time_checked"]
TWITTER_CONSUMER_KEY = data_config["twitter_api"]["consumer_key"]
@ -110,11 +112,38 @@ while True:
if state != state_b4:
file_status_internet_path = dir_path + os.sep + "status_internet.json"
# it is just nice to not always download the newest status.json.
if time_now - data_config["status"]["last_time_checked"] >= STATUS_CHECKING_PERIOD and \
STATUS_URL != "":
print("status fetched online", time_now - STATUS_LAST_TIME_CHECKED)
try:
data_status = download_json(STATUS_URL)
print("status fetched online")
# writing JSON object
with open(file_status_internet_path, 'w') as f:
json.dump(data_status, f, indent=4)
data_config["status"]["last_time_checked"] = time_now
# writing JSON object
with open(file_configure_path, 'w') as f:
json.dump(data_config, f, indent=4)
except:
print("download failed", time_now)
# loading JSON object
if os.path.exists(file_status_internet_path) == True:
print("status internet file exists")
with open(file_status_internet_path, 'r') as f:
data_status = json.load(f)
else:
file_status_path = dir_path + os.sep + "status.json"
if os.path.exists(file_status_path) == True:
@ -123,6 +152,7 @@ while True:
print("status fetched locally")
if state == 1 and state_b4 == 0:
time_opening = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S')
print("open", time_opening)