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 socket
|
||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
|
import threading
|
||||||
from mastodon import Mastodon
|
from mastodon import Mastodon
|
||||||
from time import time, localtime, strftime, sleep
|
from time import time, localtime, strftime, sleep
|
||||||
import configparser
|
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):
|
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
|
||||||
|
@ -103,37 +161,6 @@ def receive_buffer_is_valid(raw_data):
|
||||||
return False
|
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):
|
def change_status(status, timestamp, filename):
|
||||||
'''
|
'''
|
||||||
Write the new status together with a timestamp into the Space API JSON.
|
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:
|
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':
|
||||||
send_toot(status, timestamp,
|
toot_threat = Toot(status, timestamp, config)
|
||||||
config['mastodon']['host'],
|
toot_thread.run()
|
||||||
config['mastodon']['token'])
|
logging.debug('Toot thread started')
|
||||||
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