Fix apistatusd.py: missing ) and code cleanup

This commit is contained in:
Ludwig Behm 2023-10-25 01:08:16 +02:00
parent c4c78aa5ba
commit fd8c5da35a
Signed by: l.behm
GPG key ID: D344835D63B89384

View file

@ -130,18 +130,6 @@ def display_peercert(cert):
else:
logging.debug(f'{key}: {cert[key]}')
logging.debug('-----------------')
try:
logging.debug('Peer certificate commonName: {}'.format(
cert['subject'][5][0][1]))
logging.debug('Peer certificate serialNumber: {}'.format(
cert['serialNumber']))
logging.debug('Peer certificate notBefore: {}'.format(
cert['notBefore']))
logging.debug('Peer certificate notAfter: {}'.format(
cert['notAfter']))
except Exception as error:
logging.debug('Cert display failed')
logging.debug(error)
def receive_buffer_is_valid(raw_data):
'''
@ -224,7 +212,7 @@ def get_status_and_time(raw_data):
param 1: byte object
return: tuple (boolean, integer)
'''
status = True if raw_data.decode('utf-8', 'strict') == '1' else False
status = raw_data.decode('utf-8', 'strict') == '1'
timestamp = int(str(time()).split('.')[0])
logging.debug('Set values for timestamp: {} and status: {}'.format(
@ -274,10 +262,6 @@ class Toot(threading.Thread):
return: boolean
'''
timeformat = '%d.%m.%Y %H:%M Uhr'
# check if status is valid
if self.status not in (True, False):
logging.error('Invalid status to toot')
return False
# convert seconds into timestring
try:
timestring = strftime(timeformat, localtime(self.timestamp))
@ -285,9 +269,9 @@ class Toot(threading.Thread):
logging.error('Can not convert timestamp into timestring')
return False
# set status message
if self.status == True:
if self.status:
self.data['status'] = 'Krautspace is open since: {}'.format(timestring)
elif self.status == False:
else:
self.data['status'] = 'Krautspace is closed since: {}'.format(timestring)
logging.debug('Message: {}'.format(self.data['status']))
# build mastodon api url
@ -448,19 +432,17 @@ def main():
ClientSocket.settimeout(float(config['general']['timeout']))
logging.debug('set ssl handshake timeout to {}s'.format(ClientSocket.gettimeout()))
Connection = context.wrap_socket(ClientSocket, server_side=True)
logging.info('SSL Connection established')
Connection.settimeout(float(config['general']['timeout']))
logging.debug('Connection timeout set to {}'.format(Connection.gettimeout())
cert = Connection.getpeercert(binary_form=False)
display_peercert(cert)
logging.debug('SSL Connection established')
display_peercert(Connection.getpeercert(binary_form=False))
except Exception as e:
logging.error('Unexpected error: {}'.format(e))
continue
# empfangen und antworten
raw_data = Connection.recv(1)
if receive_buffer_is_valid(raw_data) is True:
if receive_buffer_is_valid(raw_data):
status, timestamp = get_status_and_time(raw_data)
if change_status(status, timestamp, api_template, config['api']['api']) is True:
if change_status(status, timestamp, api_template, config['api']['api']):
answer = raw_data
if config['mastodon']['send'].lower() == 'true':
logging.debug('Toot is set to true')