Compare commits
2 commits
06c4d75f27
...
af75b864df
Author | SHA1 | Date | |
---|---|---|---|
af75b864df | |||
4b19063b66 |
1 changed files with 68 additions and 19 deletions
|
@ -19,7 +19,7 @@ try:
|
|||
import threading
|
||||
from time import time, localtime, strftime, sleep
|
||||
import configparser
|
||||
except ImportException as e:
|
||||
except Exception as e:
|
||||
print('Import error: {}'.format(e))
|
||||
|
||||
|
||||
|
@ -117,6 +117,17 @@ def display_peercert(cert):
|
|||
elif len(cert) == 0:
|
||||
logging.debug('Peer certificate was not valid')
|
||||
else:
|
||||
logging.debug('--- Peer cert ---')
|
||||
for key in cert.keys():
|
||||
if key in ('subject', 'serialNumber', 'notBefore', 'notAfter'):
|
||||
if key == 'subject':
|
||||
logging.debug(f'Subject: {cert[key]}')
|
||||
for i in cert[key]:
|
||||
logging.debug(f'{i[0][0]}: {i[0][1]}')
|
||||
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(
|
||||
|
@ -125,6 +136,9 @@ def display_peercert(cert):
|
|||
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):
|
||||
'''
|
||||
|
@ -172,7 +186,7 @@ def read_api(api):
|
|||
return False
|
||||
return api_json_data
|
||||
|
||||
def change_status(status, timestamp, filename):
|
||||
def change_status(status, timestamp, api_template, filename):
|
||||
'''
|
||||
Write the new status together with a timestamp into the Space API JSON.
|
||||
param 1: byte object
|
||||
|
@ -180,19 +194,15 @@ def change_status(status, timestamp, filename):
|
|||
return: boolean
|
||||
'''
|
||||
logging.debug('Change status API')
|
||||
# todo: use walrus operator := when migrating to python >= 3.8
|
||||
data = read_api(filename)
|
||||
if data is False:
|
||||
return False
|
||||
|
||||
if os.access(filename, os.W_OK):
|
||||
logging.debug('API file is writable')
|
||||
with open(filename, 'w') as api_file:
|
||||
logging.debug('API file successfull writable opened')
|
||||
data["state"]["open"] = status
|
||||
data["state"]["lastchange"] = timestamp
|
||||
api_template["state"]["open"] = status
|
||||
api_template["state"]["lastchange"] = timestamp
|
||||
try:
|
||||
json.dump(data, api_file, indent=4)
|
||||
# json.dump(data, api_file, indent=4)
|
||||
json.dump(api_template, api_file, indent=4)
|
||||
except Exception as e:
|
||||
logging.error('Failed to change API file')
|
||||
logging.error('{}'.format(e))
|
||||
|
@ -335,6 +345,45 @@ def main():
|
|||
'token': 'aaaaa-bbbbb-ccccc-ddddd-eeeee'
|
||||
}
|
||||
}
|
||||
|
||||
api_template = {
|
||||
"api": "0.13",
|
||||
"space": "Krautspace",
|
||||
"url": "https://kraut.space",
|
||||
"logo": "https://status.krautspace.de/images/krautspace_pixelbanner.png",
|
||||
"location": {
|
||||
"address": "Hackspace Jena e. V., Krautgasse 26, 07743 Jena, Germany",
|
||||
"lat": 50.9292,
|
||||
"lon": 11.5826
|
||||
},
|
||||
"state": {
|
||||
"open": False,
|
||||
"lastchange": 1563499131,
|
||||
"icon": {
|
||||
"open":"https://status.krautspace.de/images/status-open.png",
|
||||
"closed":"https://status.krautspace.de/images/status-closed.png"
|
||||
}
|
||||
},
|
||||
"feeds": {
|
||||
"calendar": {
|
||||
"type": "ical",
|
||||
"url": "https://cloud.kraut.space/remote.php/dav/public-calendars/2EkPGt3PF6WwYsA3?export"
|
||||
}
|
||||
},
|
||||
"contact": {
|
||||
"mastodon": "@krautspace@chaos.social",
|
||||
"matrix": "#krautchan:matrix.kraut.space",
|
||||
"email": "office@krautspace.de"
|
||||
},
|
||||
"issue_report_channels": [
|
||||
"matrix",
|
||||
"email"
|
||||
],
|
||||
"projects": [
|
||||
"https://git.kraut.space/Krautspace"
|
||||
]
|
||||
}
|
||||
|
||||
logging.info('Try to read config file')
|
||||
configfile = './apistatusd.conf'
|
||||
config = configparser.ConfigParser()
|
||||
|
@ -402,7 +451,7 @@ def main():
|
|||
raw_data = Connection.recv(1)
|
||||
if receive_buffer_is_valid(raw_data) is True:
|
||||
status, timestamp = get_status_and_time(raw_data)
|
||||
if change_status(status, timestamp, config['api']['api']) is True:
|
||||
if change_status(status, timestamp, api_template, config['api']['api']) is True:
|
||||
answer = raw_data
|
||||
if config['mastodon']['send'].lower() == 'true':
|
||||
logging.debug('Toot is set to true')
|
||||
|
|
Loading…
Reference in a new issue