ticketfrei/backend.py

50 lines
1.6 KiB
Python
Raw Normal View History

2017-12-30 00:15:22 +00:00
#!/usr/bin/env python3
2018-03-28 15:36:35 +00:00
from bot import Bot
import active_bots
from config import config
from db import db
2018-03-24 15:26:35 +00:00
import logging
2018-03-28 23:25:17 +00:00
from sendmail import sendmail
2023-01-14 22:26:46 +00:00
from time import sleep
def shutdown():
try:
sendmail(config['web']['contact'], 'Ticketfrei Shutdown')
except Exception:
logger.error('Could not inform admin.', exc_info=True)
exit(1)
2018-03-24 15:26:35 +00:00
if __name__ == '__main__':
2023-06-30 11:22:34 +00:00
logger = logging.getLogger("main")
2023-06-30 11:32:40 +00:00
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('/var/log/ticketfrei/backend.log')
formatter = logging.Formatter('%(asctime)s %(levelname)8s: %(message)s')
fh.setFormatter(formatter)
2018-03-24 15:26:35 +00:00
logger.addHandler(fh)
logger.info("Backend Daemon was started...")
2018-03-28 15:36:35 +00:00
bots = []
for ActiveBot in active_bots.__dict__.values():
if isinstance(ActiveBot, type) and issubclass(ActiveBot, Bot):
bots.append(ActiveBot())
2018-03-28 15:36:35 +00:00
try:
while True:
for user in db.active_users:
for bot in bots:
2023-06-30 11:22:34 +00:00
sleep(1)
2018-03-28 15:36:35 +00:00
reports = bot.crawl(user)
for status in reports:
2018-03-28 15:36:35 +00:00
if not user.is_appropriate(status):
2018-09-24 17:46:21 +00:00
logger.info("Inaproppriate message: %d %s %s" % (user.uid, status.author, status.text))
2018-09-24 17:58:17 +00:00
continue
2018-03-28 21:50:55 +00:00
for bot2 in bots:
2023-06-22 06:14:30 +00:00
sleep(1)
2018-03-28 21:50:55 +00:00
bot2.post(user, status)
2018-09-24 17:46:21 +00:00
logger.info("Resent: %d %s %s" % (user.uid, status.author, status.text))
2018-03-28 23:25:17 +00:00
except Exception:
logger.error("Shutdown.", exc_info=True)
shutdown()