2017-12-30 00:15:22 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-07-20 20:30:43 +00:00
|
|
|
|
2018-03-23 14:51:52 +00:00
|
|
|
import prepare
|
2017-06-17 16:15:13 +00:00
|
|
|
import time
|
2018-03-23 14:51:52 +00:00
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
import sendmail
|
2018-03-23 14:51:52 +00:00
|
|
|
from db import DB
|
2017-06-17 16:15:13 +00:00
|
|
|
|
2017-06-17 18:49:30 +00:00
|
|
|
from retootbot import RetootBot
|
2018-03-23 14:51:52 +00:00
|
|
|
# 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
|
|
|
|
2018-03-23 10:05:24 +00:00
|
|
|
|
2018-03-23 14:51:52 +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
|
|
|
|
|
2018-03-23 10:05:24 +00:00
|
|
|
|
|
|
|
def run():
|
2018-03-23 14:51:52 +00:00
|
|
|
config = prepare.get_config()
|
|
|
|
logger = prepare.get_logger(config)
|
|
|
|
db = DB()
|
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 14:51:52 +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
|
|
|
|
2018-01-07 19:22:32 +00:00
|
|
|
try:
|
2018-03-23 14:51:52 +00:00
|
|
|
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!")
|
2018-01-07 19:22:32 +00:00
|
|
|
except:
|
2018-03-23 14:51:52 +00:00
|
|
|
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)
|
2018-03-23 10:05:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-03-23 14:51:52 +00:00
|
|
|
run()
|