From a0dc703708a4c519bf59368d3471fd8169814c95 Mon Sep 17 00:00:00 2001 From: Egg Date: Wed, 9 Oct 2024 16:29:10 +0200 Subject: [PATCH] mailbot: keep mbox open --- active_bots/mailbot.py | 20 ++++++++++++-------- backend.py | 5 +++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/active_bots/mailbot.py b/active_bots/mailbot.py index 334d25e..84543fd 100644 --- a/active_bots/mailbot.py +++ b/active_bots/mailbot.py @@ -15,18 +15,22 @@ logger = logging.getLogger("main") class Mailbot(Bot): + mails = None + # returns a list of Report objects def crawl(self, user): reports = [] # todo: adjust to actual mailbox - try: - mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user']) - except FileNotFoundError: - logger.error("No mbox file found.") - return reports - for msg in mails: - if get_date_from_header(msg['Date']) > user.get_seen_mail(): - if user.get_city().lower() in msg['To'].lower(): + if self.mails is None: + try: + self.mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user']) + except FileNotFoundError: + logger.error("No mbox file found.") + self.mails = None + return reports + for msg in self.mails: + if user.get_city().lower() in msg['To'].lower(): + if get_date_from_header(msg['Date']) > user.get_seen_mail(): reports.append(make_report(msg, user)) return reports diff --git a/backend.py b/backend.py index 223fbcc..155da55 100755 --- a/backend.py +++ b/backend.py @@ -33,8 +33,12 @@ if __name__ == '__main__': try: while True: sleep(15) # rest 15 seconds between each crawl run + timer_all = time() + user_count = 0 for user in db.active_users: sleep(1) + user_count += 1 + for bot in bots: timer_crawl = time() reports = bot.crawl(user) @@ -46,6 +50,7 @@ if __name__ == '__main__': for bot2 in bots: bot2.post(user, status) logger.info("Resent: %d %s %s" % (user.uid, status.author, status.text)) + logger.debug("All %d users crawled and msgs resent in %f seconds" % (user_count, time()-timer_all)) except Exception: logger.error("Shutdown.", exc_info=True) shutdown()