logging the changing states

logging the changing states into a csv
This commit is contained in:
bernd2k 2018-02-14 19:08:47 +01:00
parent c48c37e594
commit 9f4af89c12

19
main.py
View file

@ -7,6 +7,9 @@ import urllib.request, json
import datetime import datetime
import tweepy import tweepy
import random import random
path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
print("dir path is", dir_path)
def internet_on(url): def internet_on(url):
try: try:
@ -25,12 +28,15 @@ def textselect(min, max):
else: else:
return random.randint(min, max) return random.randint(min, max)
def log(out):
f = open("log.csv", "a")
f.write(out)
f.close()
while True: while True:
# starting by configuring the bot # starting by configuring the bot
file_configure_path = "config.json" file_configure_path = dir_path + os.sep + "config.json"
with open(file_configure_path, 'r') as f: with open(file_configure_path, 'r') as f:
data_config = json.load(f) data_config = json.load(f)
@ -72,6 +78,7 @@ while 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)
time_now = time.time()
# finding out wich state it change and sending out the text # finding out wich state it change and sending out the text
if data_space["state"]["open"] == False: if data_space["state"]["open"] == False:
@ -86,6 +93,9 @@ while True:
api.update_status(text) api.update_status(text)
# same text as before cannot be posted! # same text as before cannot be posted!
log(str(time_now) + ";" + data_space["state"]["lastchange"] + ";" + str(data_space["state"]["open"])
+ ";" + str(number) + "\n")
else: else:
print("opening at", time.time(), ",last change happend at", datetime.datetime.fromtimestamp( print("opening at", time.time(), ",last change happend at", datetime.datetime.fromtimestamp(
int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S')) int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S'))
@ -97,13 +107,16 @@ while True:
print("Text:", text) print("Text:", text)
api.update_status(text) api.update_status(text)
log(str(time_now) + ";" + data_space["state"]["lastchange"] + ";" + str(data_space["state"]["open"])
+ ";" + str(number) + "\n")
else: else:
print("status is still the same at", time.time(), datetime.datetime.fromtimestamp( print("status is still the same at", time.time(), datetime.datetime.fromtimestamp(
int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S')) int(data_space["state"]["lastchange"]) ).strftime('%Y-%m-%d %H:%M:%S'))
else: else:
print("welcome to your initial round") print("We're back, baby! #Krautspace space status is active again. @HackspaceJena")
# saving the current space status just too compare them in the next round # saving the current space status just too compare them in the next round