ticketfrei/backend.py

77 lines
2.1 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
import prepare
2017-06-17 16:15:13 +00:00
import time
import sendmail
from db import DB
2017-06-17 16:15:13 +00:00
2017-06-17 18:49:30 +00:00
from retootbot import RetootBot
# from retweetbot import RetweetBot
# 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
def init_bots(config, logger, db, users):
for uid in users:
users[uid].append(RetootBot(config, logger, uid, db))
# users[uid].append(RetweetBot(config, uid, db))
# users[uid].append(Mailbot(config, uid, db))
return users
def run():
config = prepare.get_config()
logger = prepare.get_logger(config)
db = DB()
2017-07-20 20:30:43 +00:00
# set trigger
trigger = Trigger(config)
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 trigger.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['mail']['contact'],
'Ticketfrei Crash Report',
attachment=config['logging']['logpath'])
except:
logger.error('Mail sending failed', exc_info=True)
if __name__ == '__main__':
run()