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"]
@ -90,10 +92,10 @@ while True:
if testmode != True: if testmode != True:
if GPIO.input(12) == GPIO.HIGH: if GPIO.input(12) == GPIO.HIGH:
#print("button was pushed:") #print("button was pushed:")
state = 1 state = 1
if GPIO.input(12) == GPIO.LOW: if GPIO.input(12) == GPIO.LOW:
#print("button was not pushed:") #print("button was not pushed:")
state = 0 state = 0
@ -110,24 +112,52 @@ 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") # 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 \
except: 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')
print("open", time_opening) print("open", time_opening)
number = len(data_status["opening_text"]) number = len(data_status["opening_text"])
number = textselect(0, number - 1) number = textselect(0, number - 1)
@ -168,7 +198,7 @@ while True:
if state == 0 and state_b4 == 1: if state == 0 and state_b4 == 1:
time_closing = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S') time_closing = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S')
print("closed", time_closing) print("closed", time_closing)
number = len(data_status["closing_text"]) number = len(data_status["closing_text"])
number = textselect(0, number - 1) number = textselect(0, number - 1)
@ -182,7 +212,7 @@ while True:
else: else:
text = texts["universal"] + " [CLOSED:" + time_closing + "]" text = texts["universal"] + " [CLOSED:" + time_closing + "]"
print("twitter text:", text) print("twitter text:", text)
if testmode != True: if testmode != True:
@ -205,5 +235,5 @@ while True:
state_b4 = state state_b4 = state
time.sleep(10) time.sleep(10)