From 5523cf9ab5bdff3c7b8f2e1405586bc4d1472b37 Mon Sep 17 00:00:00 2001 From: bernd2k Date: Sat, 10 Feb 2018 13:30:32 +0100 Subject: [PATCH] checking the status on the space api checking the status on the space api detecing change, and preparing the corresponding text. currently, the standard text will be used only --- config.json | 14 +++++++++ main.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++ status.json | 45 ++++++++++++++++++----------- 3 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 config.json create mode 100644 main.py diff --git a/config.json b/config.json new file mode 100644 index 0000000..4dc4f7d --- /dev/null +++ b/config.json @@ -0,0 +1,14 @@ +{ + "name": "Krautspace space status", + "status_api": { + "url": "https://status.kraut.space/api", + "standard": "space.api", + "checking_period": 300 + }, + "twitter_api": { + "consumer_key": "fill in your data!", + "consumer_secret": "fill in your data!", + "token": "fill in your data!", + "token_secret": "fill in your data!" + } +} \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..ed01805 --- /dev/null +++ b/main.py @@ -0,0 +1,81 @@ +# status bot + +import json +import os +import time +import urllib.request, json +import datetime + +def internet_on(url): + try: + urllib.request.urlopen(url) + return True + except urllib.error.URLError as e: + print(e.reason) + return False + + +while True: + + # starting by configuring the bot + file_configure_path = "config.json" + with open(file_configure_path, 'r') as f: + data_config = json.load(f) + + if internet_on("http://twitter.com") == True: + + # finding out the status of the space... + api_url = data_config["status_api"]["url"] + with urllib.request.urlopen(api_url) as url: + data_space = json.loads(url.read().decode()) + #print(data_space["state"]["open"]) + + + + file_path = "space.json" + if os.path.exists(file_path) == True: + # Reading data back + with open(file_path, 'r') as f: + data_space_b4 = json.load(f) + #print(data_space_b4["state"]["open"]) + + if data_space["state"]["open"] != data_space_b4["state"]["open"]: + print("status changed.") + + # starting by configuring the bot + file_status_path = "status.json" + with open(file_status_path, 'r') as f: + data_status = json.load(f) + + + # finding out wich state it change and sending out the text + if data_space["state"]["open"] == False: + print("closing at", time.time(), ",last change happend at", datetime.datetime.fromtimestamp( + int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S')) + + print("Text:", data_status["closing_text"][0]["text"]) + + else: + print("opening at", time.time(), ",last change happend at", datetime.datetime.fromtimestamp( + int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S')) + + print("Text:", data_status["opening_text"][0]["text"]) + + + else: + print("status is still the same at", time.time(), datetime.datetime.fromtimestamp( + int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S')) + + else: + print("welcome to your initial round") + + + # saving the current space status just too compare them in the next round + with open(file_path, 'w') as outfile: + json.dump(data_space, outfile, indent=4) + + else: + print(time.time(), "internet is off") + + # how often shall we check for status? + time.sleep(data_config["status_api"]["checking_period"]) \ No newline at end of file diff --git a/status.json b/status.json index ecb2f02..64930a3 100644 --- a/status.json +++ b/status.json @@ -2,40 +2,53 @@ "name": "Krautspace space status", "closing_text": [ { - "text": "#krautspace is closed.", - "limitperweek": 9000 + "text": "Krautspace ist geschlossen." }, { - "text": "Last person left #krautspace", - "limitperweek": 9000 + "text": "#krautspace is closed." }, { - "text": "Closing #krautspace. We're sorry you need to wait outside, but there is always @freifunkjena to distract you.", - "limitperweek": 1 + "text": "Last person left #krautspace" }, { - "text": "Currently, there is no light in #krautspace, so why not walk the beautiful @jenalichtstadt?", - "limitperweek": 1 + "text": "Closing #krautspace. We're sorry you need to wait outside, but there is always @freifunkjena to distract you." }, { - "text": "#krautspace is zu, aber @offenesjena ist immer offen.", - "limitperweek": 1 + "text": "Currently, there is no light in #krautspace, so why not walk the beautiful @jenalichtstadt?" + }, + { + "text": "#krautspace is zu, aber @offenesjena ist immer offen." + }, + { + "text": "The space is closed, but our chat https://kraut.space/chat/ is always there for you!" + }, + { + "text": "#Krautspace ist zu, aber andere St\u00e4dte haben auch sch\u00f6ne #hackerspaces." + }, + { + "text": "Dave Bowman: \"Open the Krautspace doors, HAL.\" | HAL9000: \" I'm sorry, Dave. I'm afraid I can't do that.\"" } ], "opening_text": [ { - "text": "#krautspace is open.", - "limitperweek": 9000 + "text": "Krautspace ist besetzt." }, { - "text": "At least one person is in #krautspace and invites you to come and join hacking!", - "limitperweek": 9000 + "text": "#krautspace is open." + }, + { + "text": "At least one person is in #krautspace and invites you to come and join hacking!" + }, + { + "text": "One Ping Only! .... Ping, #krautspace is open." + }, + { + "text": "Booting up #krautspace.... space is open!" } ], "uncertain_text": [ { - "text": "people in #krautspace want their privacy", - "limitperweek": 9000 + "text": "people in #krautspace want their privacy" } ] } \ No newline at end of file