forked from Krautspace/doorstatus
angefangen toot in thread auszulagern
This commit is contained in:
parent
930ab7eef3
commit
c7fc0b9eff
1 changed files with 61 additions and 34 deletions
|
@ -14,11 +14,69 @@ import os
|
|||
import socket
|
||||
import ssl
|
||||
import sys
|
||||
import threading
|
||||
from mastodon import Mastodon
|
||||
from time import time, localtime, strftime, sleep
|
||||
import configparser
|
||||
|
||||
|
||||
class Toot(threading.Thread):
|
||||
'''
|
||||
The thread to toot the status to mastodon.
|
||||
'''
|
||||
def __init__(self, status, timestamp, config):
|
||||
'''
|
||||
param1: boolean
|
||||
param2: integer
|
||||
param3: dictionary
|
||||
'''
|
||||
threading.Thread.__init__(self)
|
||||
self.status = status
|
||||
self.config = config
|
||||
self.timestamp = timestamp
|
||||
self.mastodon = Mastodon(api_base_url = self.config['mastodon']['host'],
|
||||
access_token = self.config['mastodon']['token'])
|
||||
|
||||
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')
|
||||
timestring = strftime(timeformat, localtime(self.timestamp))
|
||||
|
||||
logging.debug('Try to toot status to {}'.format(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)
|
||||
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
|
||||
|
@ -103,37 +161,6 @@ 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.
|
||||
|
@ -333,9 +360,9 @@ def main():
|
|||
if change_status(status, timestamp, config['api']['api']) is True:
|
||||
answer = raw_data
|
||||
if config['mastodon']['send'].lower() == 'true':
|
||||
send_toot(status, timestamp,
|
||||
config['mastodon']['host'],
|
||||
config['mastodon']['token'])
|
||||
toot_threat = Toot(status, timestamp, config)
|
||||
toot_thread.run()
|
||||
logging.debug('Toot thread started')
|
||||
else: logging.debug('Toot is set to false')
|
||||
if conn:
|
||||
logging.debug('Send {} back'.format(raw_data))
|
||||
|
|
Loading…
Reference in a new issue