This repository has been archived on 2024-01-26. You can view files and clone it, but cannot push or open issues or pull requests.
space_status/raspberry_pi/main.py

239 lines
6.5 KiB
Python
Raw Normal View History

import json
import os
import time
import datetime
2018-02-13 21:19:34 +01:00
import tweepy
2019-12-20 00:44:07 +01:00
from mastodon import Mastodon
2018-02-13 21:19:34 +01:00
import random
2020-01-06 20:27:14 +01:00
import urllib.request
testmode = False
if testmode != True:
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("Mode: operational")
else:
print("Mode: test")
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)
def send_tweet(twitter_api, text, time_now, status):
try:
twitter_api.update_status(text)
# same text as before cannot be posted!
except:
print(time_now, "did not tweet", status, "status")
def send_toot(MASTODON_ACCESS_TOKEN, MASTODON_API_BASE_URL, text, time_now, status):
try:
mastodon = Mastodon(
access_token=MASTODON_ACCESS_TOKEN,
api_base_url=MASTODON_API_BASE_URL
)
mastodon.toot(text)
except:
print(time_now, "did not toot", status," status")
2020-01-06 20:27:14 +01:00
def download_json(json_url):
with urllib.request.urlopen(json_url) as url:
data = json.loads(url.read().decode())
return data
path = os.path.abspath(__file__)
dir_path = os.path.dirname(path)
print("dir path is", dir_path)
2018-02-13 21:19:34 +01:00
# starting by configuring the bot
file_configure_path = dir_path + os.sep + "config.json"
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"]
2020-01-08 22:45:43 +01:00
STATUS_URL = data_config["status"]["url"]
STATUS_CHECKING_PERIOD = data_config["status"]["checking_period"]
STATUS_LAST_TIME_CHECKED = data_config["status"]["last_time_checked"]
2019-12-20 00:44:07 +01:00
TWITTER_CONSUMER_KEY = data_config["twitter_api"]["consumer_key"]
TWITTER_CONSUMER_SECRET = data_config["twitter_api"]["consumer_secret"]
TWITTER_ACCESS_KEY = data_config["twitter_api"]["token"]
TWITTER_ACCESS_SECRET = data_config["twitter_api"]["token_secret"]
twitter_auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
twitter_auth.set_access_token(TWITTER_ACCESS_KEY, TWITTER_ACCESS_SECRET)
twitter_api = tweepy.API(twitter_auth)
MASTODON_ACCESS_TOKEN = data_config["mastodon_api"]["access_token"]
MASTODON_API_BASE_URL = data_config["mastodon_api"]["api_base_url"]
# storing the current and the previous states of the door
state = 1
state_b4 = 1
2018-02-13 21:19:34 +01:00
while True:
2019-12-20 00:44:07 +01:00
if testmode != True:
if GPIO.input(12) == GPIO.HIGH:
#print("button was pushed:")
2020-01-08 22:45:43 +01:00
state = 1
2020-01-08 22:45:43 +01:00
if GPIO.input(12) == GPIO.LOW:
#print("button was not pushed:")
state = 0
else:
# just toggles the states for test mode!
if state == 0:
state = 1
else:
state = 0
2019-12-20 00:44:07 +01:00
time_now = time.time()
if state != state_b4:
2020-01-08 22:45:43 +01:00
file_status_internet_path = dir_path + os.sep + "status_internet.json"
# 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 \
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:
2020-01-06 20:27:14 +01:00
file_status_path = dir_path + os.sep + "status.json"
if os.path.exists(file_status_path) == True:
with open(file_status_path, 'r') as f:
data_status = json.load(f)
2020-01-08 22:45:43 +01:00
print("status fetched locally")
if state == 1 and state_b4 == 0:
2020-01-06 14:40:02 +01:00
time_opening = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S')
print("open", time_opening)
2020-01-08 22:45:43 +01:00
number = len(data_status["opening_text"])
number = textselect(0, number - 1)
2019-12-20 00:44:07 +01:00
texts = data_status["opening_text"][number]["text"]
if TWITTER_ACCESS_KEY != "fill in your data!":
if texts.get("twitter") != None:
text = texts["twitter"] + " [OPEN:" + time_opening + "]"
2019-12-20 00:44:07 +01:00
else:
text = texts["universal"] + " [OPEN:" + time_opening + "]"
2019-12-20 00:44:07 +01:00
print("twitter text:", text)
2019-12-20 00:44:07 +01:00
if testmode != True:
send_tweet(twitter_api, text, time_now, "opening")
2019-12-20 00:44:07 +01:00
if MASTODON_ACCESS_TOKEN != "fill in your data!":
if texts.get("mastodon") != None:
text = texts["mastodon"] + " [OPEN:" + time_opening + "]"
else:
text = texts["universal"] + " [OPEN:" + time_opening + "]"
print("mastodon text:", text)
if testmode != True:
send_toot(MASTODON_ACCESS_TOKEN, MASTODON_API_BASE_URL, text, time_now, "opening")
2019-12-20 00:44:07 +01:00
if state == 0 and state_b4 == 1:
2020-01-06 14:40:02 +01:00
time_closing = datetime.datetime.fromtimestamp(int(time_now)).strftime('%Y-%m-%d %H:%M:%S')
print("closed", time_closing)
2020-01-08 22:45:43 +01:00
number = len(data_status["closing_text"])
number = textselect(0, number - 1)
2019-12-20 00:44:07 +01:00
texts = data_status["closing_text"][number]["text"]
2020-01-05 13:23:21 +01:00
if TWITTER_ACCESS_KEY != "fill in your data!":
if texts.get("twitter") != None:
text = texts["twitter"] + " [CLOSED:" + time_closing + "]"
2019-12-20 00:44:07 +01:00
else:
text = texts["universal"] + " [CLOSED:" + time_closing + "]"
2019-12-20 00:44:07 +01:00
2020-01-08 22:45:43 +01:00
print("twitter text:", text)
2019-12-20 00:44:07 +01:00
if testmode != True:
send_tweet(twitter_api, text, time_now, "closing")
2019-12-20 00:44:07 +01:00
if MASTODON_ACCESS_TOKEN != "fill in your data!":
if texts.get("mastodon") != None:
text = texts["mastodon"] + " [CLOSED:" + time_closing + "]"
else:
text = texts["universal"] + " [CLOSED:" + time_closing + "]"
print("mastodon text:", text)
if testmode != True:
send_toot(MASTODON_ACCESS_TOKEN, MASTODON_API_BASE_URL, text, time_now, "closing")
2019-12-20 00:44:07 +01:00
state_b4 = state
2020-01-08 22:45:43 +01:00
time.sleep(10)