2017-12-30 00:15:22 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-07-20 20:30:43 +00:00
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
import logging
|
2017-06-17 16:15:13 +00:00
|
|
|
import pytoml as toml
|
|
|
|
import time
|
2018-01-07 19:22:32 +00:00
|
|
|
import sendmail
|
2017-06-17 16:15:13 +00:00
|
|
|
|
2017-06-17 18:49:30 +00:00
|
|
|
from retootbot import RetootBot
|
|
|
|
from retweetbot import RetweetBot
|
2018-01-18 12:06:53 +00:00
|
|
|
from mailbot import Mailbot
|
2017-06-17 20:11:44 +00:00
|
|
|
from trigger import Trigger
|
2017-06-17 18:27:53 +00:00
|
|
|
|
2018-03-23 10:18:00 +00:00
|
|
|
def get_logger(config):
|
|
|
|
logpath = config['logging']['logpath']
|
2018-03-23 10:05:24 +00:00
|
|
|
logger = logging.getLogger()
|
2018-03-23 10:18:00 +00:00
|
|
|
fh = logging.FileHandler(logpath)
|
2018-03-23 10:05:24 +00:00
|
|
|
fh.setLevel(logging.DEBUG)
|
|
|
|
logger.addHandler(fh)
|
|
|
|
return logger
|
|
|
|
|
|
|
|
def get_config():
|
2017-06-17 17:37:33 +00:00
|
|
|
# read config in TOML format (https://github.com/toml-lang/toml#toml)
|
2017-12-30 09:32:20 +00:00
|
|
|
with open('config.toml') as configfile:
|
2017-06-17 18:49:30 +00:00
|
|
|
config = toml.load(configfile)
|
2018-03-23 10:05:24 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
def run():
|
|
|
|
config = get_config()
|
2018-03-23 10:18:00 +00:00
|
|
|
logger = get_logger(config)
|
2017-07-20 20:30:43 +00:00
|
|
|
|
2018-03-23 10:05:24 +00:00
|
|
|
# set trigger
|
2018-01-07 19:22:32 +00:00
|
|
|
trigger = Trigger(config)
|
2018-01-18 12:06:53 +00:00
|
|
|
|
2018-03-23 10:05:24 +00:00
|
|
|
# initialize bots
|
2018-01-19 15:00:36 +00:00
|
|
|
bots = []
|
|
|
|
if config["muser"]["enabled"] != "false":
|
|
|
|
bots.append(RetootBot(config))
|
|
|
|
if config["tuser"]["enabled"] != "false":
|
|
|
|
bots.append(RetweetBot(config))
|
|
|
|
if config["mail"]["enabled"] != "false":
|
|
|
|
bots.append(Mailbot(config))
|
2017-06-17 18:49:30 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
statuses = []
|
|
|
|
while True:
|
2018-01-18 12:06:53 +00:00
|
|
|
for bot in bots:
|
|
|
|
reports = bot.crawl()
|
|
|
|
for status in reports:
|
|
|
|
if not trigger.is_ok(status.text):
|
|
|
|
continue
|
|
|
|
for bot2 in bots:
|
|
|
|
if bot == bot2:
|
|
|
|
bot2.repost(status)
|
|
|
|
else:
|
|
|
|
bot2.post(status)
|
|
|
|
time.sleep(60) # twitter rate limit >.<
|
2017-11-24 17:11:35 +00:00
|
|
|
except KeyboardInterrupt:
|
2017-12-30 00:15:22 +00:00
|
|
|
print("Good bye. Remember to restart the bot!")
|
2017-07-11 20:05:46 +00:00
|
|
|
except:
|
2018-01-07 19:22:32 +00:00
|
|
|
logger.error('Shutdown', exc_info=True)
|
2018-01-18 12:06:53 +00:00
|
|
|
for bot in bots:
|
|
|
|
bot.save_last()
|
2018-03-23 10:18:00 +00:00
|
|
|
mailer = sendmail.Mailer(config)
|
2018-01-07 19:22:32 +00:00
|
|
|
try:
|
|
|
|
mailer.send('', config['mail']['contact'],
|
|
|
|
'Ticketfrei Crash Report',
|
|
|
|
attachment=config['logging']['logpath'])
|
|
|
|
except:
|
|
|
|
logger.error('Mail sending failed', exc_info=True)
|
2018-03-23 10:05:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
run()
|