From 930ab7eef3f834a46c523f11ce4e1289a4e4d9b9 Mon Sep 17 00:00:00 2001 From: +++ Date: Sun, 10 Jul 2022 19:38:29 +0200 Subject: [PATCH] funktion send_toot() hinzu --- source/server/apistatusd.py | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/source/server/apistatusd.py b/source/server/apistatusd.py index fca5c21..0cae07f 100755 --- a/source/server/apistatusd.py +++ b/source/server/apistatusd.py @@ -15,7 +15,7 @@ import socket import ssl import sys from mastodon import Mastodon -from time import time, sleep +from time import time, localtime, strftime, sleep import configparser @@ -103,6 +103,37 @@ def receive_buffer_is_valid(raw_data): return False +def send_toot(status, timestamp, host, token): + ''' + param1: boolean + param2: integer + param3: string + param4: string + return: boolean + ''' + msg = None + timeformat = '%d.%m.%Y %H:%M Uhr' + if status not in (True, False): + logging.error('Invalid status to toot') + return False + timestring = strftime(timeformat, localtime(timestamp)) + + logging.debug('Try to toot status to {}'.format(host)) + if status == True: + msg = 'The krautspace is open since: {}'.format(timestring) + elif status == False: + msg = 'The krautspace is closed since: {}'.format(timestring) + logging.debug('Send message: {}'.format(msg)) + try: + mastodon = Mastodon(api_base_url = host, + access_token = token) + mastodon.toot(mag) + except Exception as e: + logging.error('Failed to toot status') + return False + return False + + def change_status(status, timestamp, filename): ''' Write the new status together with a timestamp into the Space API JSON. @@ -169,7 +200,7 @@ def get_status_and_time(raw_data): Create a timestamp, changes the value of the given byte into a string and returns both. param 1: byte object - return: tuple + return: tuple (boolean, integer) ''' status = True if raw_data.decode('utf-8', 'strict') == '1' else False timestamp = int(str(time()).split('.')[0]) @@ -302,7 +333,9 @@ def main(): if change_status(status, timestamp, config['api']['api']) is True: answer = raw_data if config['mastodon']['send'].lower() == 'true': - logging.debug('Try to toot status') + send_toot(status, timestamp, + config['mastodon']['host'], + config['mastodon']['token']) else: logging.debug('Toot is set to false') if conn: logging.debug('Send {} back'.format(raw_data))