erste version eines toots

This commit is contained in:
+++ 2022-07-13 19:10:17 +02:00
parent c7fc0b9eff
commit 666a997a90

View file

@ -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'],
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')
return False
try:
timestring = strftime(timeformat, localtime(self.timestamp))
logging.debug('Try to toot status to {}'.format(host))
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)
try:
toot_thread = Toot(status, timestamp, config)
toot_thread.run()
logging.debug('Toot thread started')
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))