funktion send_toot() hinzu

This commit is contained in:
+++ 2022-07-10 19:38:29 +02:00
parent edece83dd1
commit 930ab7eef3

View file

@ -15,7 +15,7 @@ import socket
import ssl
import sys
from mastodon import Mastodon
from time import time, sleep
from time import time, localtime, strftime, sleep
import configparser
@ -103,6 +103,37 @@ def receive_buffer_is_valid(raw_data):
return False
def send_toot(status, timestamp, host, token):
'''
param1: boolean
param2: integer
param3: string
param4: string
return: boolean
'''
msg = None
timeformat = '%d.%m.%Y %H:%M Uhr'
if status not in (True, False):
logging.error('Invalid status to toot')
return False
timestring = strftime(timeformat, localtime(timestamp))
logging.debug('Try to toot status to {}'.format(host))
if status == True:
msg = 'The krautspace is open since: {}'.format(timestring)
elif 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)
except Exception as e:
logging.error('Failed to toot status')
return False
return False
def change_status(status, timestamp, filename):
'''
Write the new status together with a timestamp into the Space API JSON.
@ -169,7 +200,7 @@ def get_status_and_time(raw_data):
Create a timestamp, changes the value of the given byte into a string
and returns both.
param 1: byte object
return: tuple
return: tuple (boolean, integer)
'''
status = True if raw_data.decode('utf-8', 'strict') == '1' else False
timestamp = int(str(time()).split('.')[0])
@ -302,7 +333,9 @@ def main():
if change_status(status, timestamp, config['api']['api']) is True:
answer = raw_data
if config['mastodon']['send'].lower() == 'true':
logging.debug('Try to toot status')
send_toot(status, timestamp,
config['mastodon']['host'],
config['mastodon']['token'])
else: logging.debug('Toot is set to false')
if conn:
logging.debug('Send {} back'.format(raw_data))