Spaces:
Build error
Build error
| # scheduler.py | |
| from apscheduler.schedulers.background import BackgroundScheduler | |
| from apscheduler.triggers.interval import IntervalTrigger | |
| from datetime import datetime | |
| import logging | |
| from config import Config | |
| # Import our other modules | |
| from pdf_parser import extract_text_from_pdf, parse_email_thread, get_days_without_account | |
| from email_watcher import check_for_new_fos_email | |
| from notifier import send_telegram_message, send_email_notification | |
| logging.basicConfig(level=logging.INFO) | |
| last_checked_time = datetime.now() | |
| def scheduled_check(): | |
| """Task that runs periodically to check for updates and send reports.""" | |
| global last_checked_time | |
| logging.info("Running scheduled check...") | |
| # 1. Check for new FOS emails | |
| new_emails = check_for_new_fos_email(last_checked_time) | |
| if new_emails: | |
| for email in new_emails: | |
| msg = f"📩 New email from FOS:\nSubject: {email['subject']}\nPreview: {email['body'][:200]}" | |
| send_telegram_message(msg) | |
| send_email_notification("New FOS Reply", msg, Config.LANDLORD_EMAIL) | |
| logging.info("Notified about new FOS email.") | |
| last_checked_time = datetime.now() | |
| else: | |
| logging.info("No new FOS emails.") | |
| # 2. Generate daily report and send to landlord | |
| try: | |
| text = extract_text_from_pdf() | |
| emails = parse_email_thread(text) | |
| days = get_days_without_account(text) | |
| report_lines = [ | |
| f"===== DAILY COMPLAINT UPDATE =====", | |
| f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M')}", | |
| f"Days without account: {days}", | |
| f"Total emails logged: {len(emails)}", | |
| "Last email from FOS: " + (emails[-1]['date'] if emails else "N/A"), | |
| "==================================" | |
| ] | |
| report = "\n".join(report_lines) | |
| # Send to landlord via email | |
| send_email_notification( | |
| f"Daily Update – {days} days without account", | |
| report, | |
| Config.LANDLORD_EMAIL, | |
| cc=Config.CC_LANDLORD | |
| ) | |
| logging.info("Daily report sent to landlord.") | |
| except Exception as e: | |
| logging.error(f"Error generating report: {e}") | |
| send_telegram_message(f"⚠️ Scheduler error: {e}") | |
| def start_scheduler(): | |
| scheduler = BackgroundScheduler() | |
| scheduler.add_job( | |
| scheduled_check, | |
| trigger=IntervalTrigger(minutes=Config.CHECK_INTERVAL), | |
| id='complaint_check', | |
| replace_existing=True | |
| ) | |
| scheduler.start() | |
| logging.info(f"Scheduler started. Checking every {Config.CHECK_INTERVAL} minutes.") | |
| return scheduler |