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", "name": "Krautspace space status",
"path": "", "path": "",
"status_url" : "https://raw.githubusercontent.com/HackspaceJena/space_status/master/raspberry_pi/status.json", "status": {
"status_api": { "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", "url": "https://status.kraut.space/api",
"standard": "space.api", "standard": "space.api",
"checking_period": 300 "checking_period": 300

View file

@ -62,7 +62,9 @@ with open(file_configure_path, 'r') as f:
data_config = json.load(f) data_config = json.load(f)
path = data_config["path"] 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"] TWITTER_CONSUMER_KEY = data_config["twitter_api"]["consumer_key"]
@ -110,18 +112,46 @@ while True:
if state != state_b4: if state != state_b4:
try: file_status_internet_path = dir_path + os.sep + "status_internet.json"
data_status = download_json(STATUS_URL)
print("status fetched online")
except: # 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)
# 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" file_status_path = dir_path + os.sep + "status.json"
if os.path.exists(file_status_path) == True: if os.path.exists(file_status_path) == True:
with open(file_status_path, 'r') as f: with open(file_status_path, 'r') as f:
data_status = json.load(f) data_status = json.load(f)
print("status fetched locally") print("status fetched locally")
if state == 1 and state_b4 == 0: if state == 1 and state_b4 == 0:
time_opening = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S') time_opening = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S')