Initial commit
This commit is contained in:
68
scripts/send_past_reports.py
Normal file
68
scripts/send_past_reports.py
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
from datetime import date
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
# Add the project root to Python path
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if project_root not in sys.path:
|
||||
sys.path.insert(0, project_root)
|
||||
|
||||
from app_core.config.settings import AppSettings
|
||||
from app_core.services.mailer_service import MailerService
|
||||
|
||||
def send_report_for_date(service, settings, report_date):
|
||||
print(f"--- Processing date: {report_date} ---")
|
||||
|
||||
# Check if we should skip if already sent (the user said "we need to send", so I'll skip the check unless specified)
|
||||
# if service.has_sent_for_date(str(report_date)):
|
||||
# print(f"Already sent for {report_date}; skipping.")
|
||||
# return
|
||||
|
||||
df = service.fetch_daily_rows(report_date)
|
||||
if df.empty:
|
||||
print(f"No data for {report_date}. Skipping.")
|
||||
return
|
||||
|
||||
row = df.iloc[0].to_dict()
|
||||
html = service.build_email_html(row, df)
|
||||
|
||||
recipients_env = settings.report_recipients or os.getenv("REPORT_RECIPIENTS")
|
||||
if not recipients_env:
|
||||
print("Error: REPORT_RECIPIENTS env var is empty.")
|
||||
return
|
||||
|
||||
recipients = [r.strip() for r in recipients_env.split(',') if r.strip()]
|
||||
subject = f"Daily Digest - {report_date}"
|
||||
|
||||
print(f"Sending email to: {recipients}")
|
||||
ok, msg = service.send_email(recipients, subject=subject, html=html)
|
||||
|
||||
service.log_email(
|
||||
recipients=recipients,
|
||||
subject=subject,
|
||||
date_for=str(report_date),
|
||||
status="sent" if ok else "failed",
|
||||
error=None if ok else msg
|
||||
)
|
||||
|
||||
if ok:
|
||||
print(f"Successfully sent report for {report_date}")
|
||||
else:
|
||||
print(f"Failed to send report for {report_date}: {msg}")
|
||||
|
||||
def main():
|
||||
settings = AppSettings()
|
||||
service = MailerService(settings)
|
||||
|
||||
dates_to_send = [
|
||||
date(2026, 3, 21),
|
||||
date(2026, 3, 22),
|
||||
]
|
||||
|
||||
for d in dates_to_send:
|
||||
send_report_for_date(service, settings, d)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user