erste version eines toots
This commit is contained in:
parent
c7fc0b9eff
commit
666a997a90
1 changed files with 31 additions and 25 deletions
|
@ -20,6 +20,14 @@ from time import time, localtime, strftime, sleep
|
||||||
import configparser
|
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):
|
class Toot(threading.Thread):
|
||||||
'''
|
'''
|
||||||
The thread to toot the status to mastodon.
|
The thread to toot the status to mastodon.
|
||||||
|
@ -34,49 +42,43 @@ class Toot(threading.Thread):
|
||||||
self.status = status
|
self.status = status
|
||||||
self.config = config
|
self.config = config
|
||||||
self.timestamp = timestamp
|
self.timestamp = timestamp
|
||||||
self.mastodon = Mastodon(api_base_url = self.config['mastodon']['host'],
|
try:
|
||||||
access_token = self.config['mastodon']['token'])
|
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):
|
def run(self):
|
||||||
'''
|
'''
|
||||||
return: boolean
|
return: boolean
|
||||||
send_toot(status, timestamp,
|
|
||||||
config['mastodon']['host'],
|
|
||||||
config['mastodon']['token'])
|
|
||||||
'''
|
'''
|
||||||
msg = None
|
msg = None
|
||||||
timeformat = '%d.%m.%Y %H:%M Uhr'
|
timeformat = '%d.%m.%Y %H:%M Uhr'
|
||||||
timestring = strftime(timeformat, localtime(self.timestamp))
|
|
||||||
|
|
||||||
if self.status not in (True, False):
|
if self.status not in (True, False):
|
||||||
logging.error('Invalid status to toot')
|
logging.error('Invalid status to toot')
|
||||||
timestring = strftime(timeformat, localtime(self.timestamp))
|
return False
|
||||||
|
try:
|
||||||
logging.debug('Try to toot status to {}'.format(host))
|
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:
|
if self.status == True:
|
||||||
msg = 'The krautspace is open since: {}'.format(timestring)
|
msg = 'The krautspace is open since: {}'.format(timestring)
|
||||||
elif self.status == False:
|
elif self.status == False:
|
||||||
msg = 'The krautspace is closed since: {}'.format(timestring)
|
msg = 'The krautspace is closed since: {}'.format(timestring)
|
||||||
logging.debug('Send message: {}'.format(msg))
|
logging.debug('Send message: {}'.format(msg))
|
||||||
try:
|
try:
|
||||||
mastodon = Mastodon(api_base_url = host,
|
|
||||||
access_token = token)
|
|
||||||
mastodon.toot(mag)
|
mastodon.toot(mag)
|
||||||
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error('Failed to toot status')
|
logging.error('Failed to toot status')
|
||||||
return False
|
return False
|
||||||
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):
|
def certs_readable(config):
|
||||||
'''
|
'''
|
||||||
checks at start, if the needed certificates defined (no nullstring) and
|
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:
|
if change_status(status, timestamp, config['api']['api']) is True:
|
||||||
answer = raw_data
|
answer = raw_data
|
||||||
if config['mastodon']['send'].lower() == 'true':
|
if config['mastodon']['send'].lower() == 'true':
|
||||||
toot_threat = Toot(status, timestamp, config)
|
try:
|
||||||
toot_thread.run()
|
toot_thread = Toot(status, timestamp, config)
|
||||||
logging.debug('Toot thread started')
|
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')
|
else: logging.debug('Toot is set to false')
|
||||||
if conn:
|
if conn:
|
||||||
logging.debug('Send {} back'.format(raw_data))
|
logging.debug('Send {} back'.format(raw_data))
|
||||||
|
|
Loading…
Reference in a new issue