mailbot: keep mbox open

This commit is contained in:
Egg 2024-10-09 16:29:10 +02:00
parent f4edfba532
commit a0dc703708
2 changed files with 17 additions and 8 deletions

View file

@ -15,18 +15,22 @@ logger = logging.getLogger("main")
class Mailbot(Bot): class Mailbot(Bot):
mails = None
# returns a list of Report objects # returns a list of Report objects
def crawl(self, user): def crawl(self, user):
reports = [] reports = []
# todo: adjust to actual mailbox # todo: adjust to actual mailbox
if self.mails is None:
try: try:
mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user']) self.mails = mailbox.mbox("/var/mail/" + config['mail']['mbox_user'])
except FileNotFoundError: except FileNotFoundError:
logger.error("No mbox file found.") logger.error("No mbox file found.")
self.mails = None
return reports return reports
for msg in mails: for msg in self.mails:
if get_date_from_header(msg['Date']) > user.get_seen_mail():
if user.get_city().lower() in msg['To'].lower(): 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)) reports.append(make_report(msg, user))
return reports return reports

View file

@ -33,8 +33,12 @@ if __name__ == '__main__':
try: try:
while True: while True:
sleep(15) # rest 15 seconds between each crawl run sleep(15) # rest 15 seconds between each crawl run
timer_all = time()
user_count = 0
for user in db.active_users: for user in db.active_users:
sleep(1) sleep(1)
user_count += 1
for bot in bots: for bot in bots:
timer_crawl = time() timer_crawl = time()
reports = bot.crawl(user) reports = bot.crawl(user)
@ -46,6 +50,7 @@ if __name__ == '__main__':
for bot2 in bots: for bot2 in bots:
bot2.post(user, status) bot2.post(user, status)
logger.info("Resent: %d %s %s" % (user.uid, status.author, status.text)) 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: except Exception:
logger.error("Shutdown.", exc_info=True) logger.error("Shutdown.", exc_info=True)
shutdown() shutdown()