From c48c37e594e115bc34d56743d07c90710cf08ed3 Mon Sep 17 00:00:00 2001 From: bernd2k Date: Tue, 13 Feb 2018 21:19:34 +0100 Subject: [PATCH] tweeting now, new texts --- config.json | 1 + main.py | 46 +++++++++++++++++++++++++++++++++++++++++----- status.json | 9 ++++++--- 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/config.json b/config.json index 4dc4f7d..4aafddb 100644 --- a/config.json +++ b/config.json @@ -1,5 +1,6 @@ { "name": "Krautspace space status", + "path": "", "status_api": { "url": "https://status.kraut.space/api", "standard": "space.api", diff --git a/main.py b/main.py index ed01805..2a24afe 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,8 @@ import os import time import urllib.request, json import datetime +import tweepy +import random def internet_on(url): try: @@ -15,6 +17,16 @@ def internet_on(url): return False +def textselect(min, max): + number = random.randint(0, 1) + + if number == 0: + return 0 + else: + return random.randint(min, max) + + + while True: # starting by configuring the bot @@ -22,6 +34,19 @@ while True: with open(file_configure_path, 'r') as f: data_config = json.load(f) + 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) + + if internet_on("http://twitter.com") == True: # finding out the status of the space... @@ -31,19 +56,19 @@ while True: #print(data_space["state"]["open"]) - - file_path = "space.json" + file_path = 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" + file_status_path = path + "status.json" with open(file_status_path, 'r') as f: data_status = json.load(f) @@ -53,13 +78,24 @@ while True: 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"]) + number = len(data_status["closing_text"]) + number = textselect(0, number - 1) + + text = data_status["closing_text"][number]["text"] + print("Text:", text) + api.update_status(text) + # same text as before cannot be posted! 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"]) + number = len(data_status["opening_text"]) + number = textselect(0, number - 1) + + text = data_status["opening_text"][number]["text"] + print("Text:", text) + api.update_status(text) else: diff --git a/status.json b/status.json index f5bead1..a991fdb 100644 --- a/status.json +++ b/status.json @@ -63,6 +63,9 @@ }, { "text": "BEE DOO BEE DOO BEE DOO - space is closed. https://www.youtube.com/watch?v=ZvvF_N2XD8w " + }, + { + "text": "Krautspace is closed and before you intend to defy the weather outside while reaching us, check our @SenseBox_De https://opensensemap.org/explore/59d7de4c66f66a0010797868 " } ], "opening_text": [ @@ -104,8 +107,8 @@ }, { "text": "Come on over and have some fun! The space is open" - }, - { + }, + { "text": "The more you know! Krautspace is open. http://i3.kym-cdn.com/photos/images/newsfeed/000/620/434/f9e.gif" }, { @@ -117,4 +120,4 @@ "text": "people in #krautspace want their privacy" } ] -} +} \ No newline at end of file