File size: 2,653 Bytes
86ffe15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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