2018-02-10 13:30:32 +01:00
|
|
|
# status bot
|
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import urllib.request, json
|
|
|
|
import datetime
|
2018-02-13 21:19:34 +01:00
|
|
|
import tweepy
|
|
|
|
import random
|
2018-02-14 19:08:47 +01:00
|
|
|
path = os.path.abspath(__file__)
|
|
|
|
dir_path = os.path.dirname(path)
|
|
|
|
print("dir path is", dir_path)
|
2018-02-10 13:30:32 +01:00
|
|
|
|
|
|
|
def internet_on(url):
|
|
|
|
try:
|
|
|
|
urllib.request.urlopen(url)
|
|
|
|
return True
|
|
|
|
except urllib.error.URLError as e:
|
|
|
|
print(e.reason)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
def textselect(min, max):
|
|
|
|
number = random.randint(0, 1)
|
|
|
|
|
|
|
|
if number == 0:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return random.randint(min, max)
|
|
|
|
|
2018-02-14 19:08:47 +01:00
|
|
|
def log(out):
|
2018-02-14 19:59:47 +01:00
|
|
|
f = open(dir_path + os.sep + "log.csv", "a")
|
2018-02-14 19:08:47 +01:00
|
|
|
f.write(out)
|
|
|
|
f.close()
|
2018-02-13 21:19:34 +01:00
|
|
|
|
2018-02-10 13:30:32 +01:00
|
|
|
while True:
|
|
|
|
|
|
|
|
# starting by configuring the bot
|
2018-02-14 19:08:47 +01:00
|
|
|
file_configure_path = dir_path + os.sep + "config.json"
|
2018-02-10 13:30:32 +01:00
|
|
|
with open(file_configure_path, 'r') as f:
|
|
|
|
data_config = json.load(f)
|
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
path = data_config["path"]
|
|
|
|
|
|
|
|
CONSUMER_KEY = data_config["twitter_api"]["consumer_key"]
|
|
|
|
CONSUMER_SECRET = data_config["twitter_api"]["consumer_secret"]
|
|
|
|
ACCESS_KEY = data_config["twitter_api"]["token"]
|
|
|
|
ACCESS_SECRET = data_config["twitter_api"]["token_secret"]
|
|
|
|
print(CONSUMER_KEY)
|
|
|
|
|
|
|
|
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
|
|
|
|
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
|
|
|
|
api = tweepy.API(auth)
|
|
|
|
|
|
|
|
|
2018-02-10 13:30:32 +01:00
|
|
|
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"])
|
|
|
|
|
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
file_path = path + "space.json"
|
2018-02-10 13:30:32 +01:00
|
|
|
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"])
|
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
|
2018-02-10 13:30:32 +01:00
|
|
|
if data_space["state"]["open"] != data_space_b4["state"]["open"]:
|
|
|
|
print("status changed.")
|
|
|
|
|
|
|
|
# starting by configuring the bot
|
2018-02-13 21:19:34 +01:00
|
|
|
file_status_path = path + "status.json"
|
2018-02-10 13:30:32 +01:00
|
|
|
with open(file_status_path, 'r') as f:
|
|
|
|
data_status = json.load(f)
|
|
|
|
|
2018-02-14 19:08:47 +01:00
|
|
|
time_now = time.time()
|
2018-02-10 13:30:32 +01:00
|
|
|
|
|
|
|
# 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'))
|
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
number = len(data_status["closing_text"])
|
|
|
|
number = textselect(0, number - 1)
|
|
|
|
|
2018-02-15 19:02:47 +01:00
|
|
|
text = data_status["closing_text"][number]["text"] + " [closed:" + datetime.datetime.fromtimestamp(
|
|
|
|
int( time_now + 3600 )).strftime('%Y-%m-%d %H:%M:%S') + "]"
|
2018-02-14 19:59:47 +01:00
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
print("Text:", text)
|
|
|
|
api.update_status(text)
|
|
|
|
# same text as before cannot be posted!
|
2018-02-10 13:30:32 +01:00
|
|
|
|
2018-02-14 19:59:47 +01:00
|
|
|
log(str(time_now) + ";" + str(data_space["state"]["lastchange"]) + ";" + str(data_space["state"]["open"])
|
2018-02-14 19:08:47 +01:00
|
|
|
+ ";" + str(number) + "\n")
|
|
|
|
|
2018-02-10 13:30:32 +01:00
|
|
|
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'))
|
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
number = len(data_status["opening_text"])
|
|
|
|
number = textselect(0, number - 1)
|
|
|
|
|
2018-02-15 19:02:47 +01:00
|
|
|
text = data_status["opening_text"][number]["text"] + " [open:" + datetime.datetime.fromtimestamp(
|
|
|
|
int( time_now + 3600 )).strftime('%Y-%m-%d %H:%M:%S') + "]"
|
2018-02-14 19:59:47 +01:00
|
|
|
|
2018-02-13 21:19:34 +01:00
|
|
|
print("Text:", text)
|
|
|
|
api.update_status(text)
|
2018-02-10 13:30:32 +01:00
|
|
|
|
2018-02-14 19:59:47 +01:00
|
|
|
log(str(time_now) + ";" + str(data_space["state"]["lastchange"]) + ";" + str(data_space["state"]["open"])
|
2018-02-14 19:08:47 +01:00
|
|
|
+ ";" + str(number) + "\n")
|
|
|
|
|
2018-02-10 13:30:32 +01:00
|
|
|
|
|
|
|
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:
|
2018-02-14 19:08:47 +01:00
|
|
|
print("We're back, baby! #Krautspace space status is active again. @HackspaceJena")
|
2018-02-10 13:30:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
# 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?
|
2018-02-14 19:59:47 +01:00
|
|
|
time.sleep(data_config["status_api"]["checking_period"])
|