From 666a997a90af229c92200be861f0e8ec1a0fef65 Mon Sep 17 00:00:00 2001 From: +++ Date: Wed, 13 Jul 2022 19:10:17 +0200 Subject: [PATCH] erste version eines toots --- source/server/apistatusd.py | 56 ++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/source/server/apistatusd.py b/source/server/apistatusd.py index c4ece63..eb59dc3 100755 --- a/source/server/apistatusd.py +++ b/source/server/apistatusd.py @@ -20,6 +20,14 @@ from time import time, localtime, strftime, sleep import configparser +class InitException(Exception): + ''' + If the initialisation from the mastodon instance failes then we raise + this exception. + ''' + def __init__(self, error): + self.error = error + class Toot(threading.Thread): ''' The thread to toot the status to mastodon. @@ -34,49 +42,43 @@ class Toot(threading.Thread): self.status = status self.config = config self.timestamp = timestamp - self.mastodon = Mastodon(api_base_url = self.config['mastodon']['host'], - access_token = self.config['mastodon']['token']) - + try: + self.mastodon = Mastodon( + api_base_url = self.config['mastodon']['host'], + access_token = self.config['mastodon']['token']) + except Exception as e: + logging.error('Exception occurred: {}'.format(e)) + raise InitException('Mastodon instance initialisation failed') + def run(self): ''' return: boolean - send_toot(status, timestamp, - config['mastodon']['host'], - config['mastodon']['token']) ''' msg = None timeformat = '%d.%m.%Y %H:%M Uhr' - timestring = strftime(timeformat, localtime(self.timestamp)) - if self.status not in (True, False): logging.error('Invalid status to toot') - timestring = strftime(timeformat, localtime(self.timestamp)) - - logging.debug('Try to toot status to {}'.format(host)) + return False + try: + timestring = strftime(timeformat, localtime(self.timestamp)) + except Exception as e: + logging.error('Can not convert timestamp into timestring') + return False + logging.debug('Try to toot status to {}'.format(self.config['mastodon']['host'])) if self.status == True: msg = 'The krautspace is open since: {}'.format(timestring) elif self.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) + return True except Exception as e: logging.error('Failed to toot status') return False return False - def send_toot(self): - ''' - Starts the thread - ''' - send_toot(status, timestamp, - config['mastodon']['host'], - config['mastodon']['token']) - - def certs_readable(config): ''' checks at start, if the needed certificates defined (no nullstring) and @@ -360,9 +362,13 @@ def main(): if change_status(status, timestamp, config['api']['api']) is True: answer = raw_data if config['mastodon']['send'].lower() == 'true': - toot_threat = Toot(status, timestamp, config) - toot_thread.run() - logging.debug('Toot thread started') + try: + toot_thread = Toot(status, timestamp, config) + toot_thread.run() + except InitException as e: + logging.debug('InitException: {}'.format(e)) + except Exception as ex: + logging.debug('Exception: {}'.format(ex)) else: logging.debug('Toot is set to false') if conn: logging.debug('Send {} back'.format(raw_data))