ticketfrei/backend.py

76 lines
2.2 KiB
Python
Raw Normal View History

2017-12-30 00:15:22 +00:00
#!/usr/bin/env python3
2017-07-20 20:30:43 +00:00
2018-03-24 15:26:35 +00:00
import logging
2017-06-17 16:15:13 +00:00
import time
import sendmail
from db import DB
2018-03-24 15:26:35 +00:00
from config import config
2017-06-17 16:15:13 +00:00
from mastodonbot import MastodonBot
from twitterbot import TwitterBot
from mailbot import Mailbot
2017-06-17 20:11:44 +00:00
from trigger import Trigger
2017-06-17 18:27:53 +00:00
def get_users(db):
user_rows = db.get_users()
users = {}
for row in user_rows:
users[row[0]] = []
return users
2018-03-24 15:26:35 +00:00
def init_bots(config, db, users):
for uid in users:
2018-03-23 17:00:05 +00:00
users[uid].append(Trigger(config, uid, db))
2018-03-24 15:26:35 +00:00
users[uid].append(MastodonBot(config, uid, db))
users[uid].append(TwitterBot(config, uid, db))
users[uid].append(Mailbot(config, uid, db))
return users
2018-03-24 15:26:35 +00:00
if __name__ == '__main__':
logpath = config['logging']['logpath']
logger = logging.getLogger()
fh = logging.FileHandler(logpath)
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)
db = DB()
2017-07-20 20:30:43 +00:00
while True:
# get a dictionary { uid : [ Bot objects ] }
users = get_users(db)
# initialize bots
users = init_bots(config, logger, db, users)
2017-06-17 18:49:30 +00:00
try:
for uid in users:
for bot in users[uid]:
reports = bot.crawl()
for status in reports:
if not users[uid][0].is_ok(status.text):
continue
for bot2 in users[uid]:
if bot == bot2:
bot2.repost(status)
else:
bot2.post(status)
time.sleep(60) # twitter rate limit >.<
except KeyboardInterrupt:
print("Good bye. Remember to restart the bot!")
except:
logger.error('Shutdown', exc_info=True)
for uid in users:
for bot in users[uid]:
bot.save_last()
mailer = sendmail.Mailer(config)
try:
mailer.send('', config['web']['contact'],
'Ticketfrei Crash Report',
attachment=config['logging']['logpath'])
except:
logger.error('Mail sending failed', exc_info=True)