try
This commit is contained in:
+7
-14
@@ -44,53 +44,46 @@ def should_send_notification(username, reset_time):
|
|||||||
"""Check if we should send a notification for this author."""
|
"""Check if we should send a notification for this author."""
|
||||||
tracking = load_notification_tracking()
|
tracking = load_notification_tracking()
|
||||||
author_data = tracking.get(username, {})
|
author_data = tracking.get(username, {})
|
||||||
|
reset_time_str = str(reset_time)
|
||||||
# If we've never notified for this author or the reset time has changed
|
if not author_data or str(author_data.get('reset_time')) != reset_time_str:
|
||||||
if not author_data or author_data.get('reset_time') != reset_time:
|
|
||||||
# Update tracking
|
|
||||||
tracking[username] = {
|
tracking[username] = {
|
||||||
'last_notification': datetime.now(timezone.utc).isoformat(),
|
'last_notification': datetime.now(timezone.utc).isoformat(),
|
||||||
'reset_time': reset_time
|
'reset_time': reset_time_str
|
||||||
}
|
}
|
||||||
save_notification_tracking(tracking)
|
save_notification_tracking(tracking)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def send_capacity_alert(username, remaining, reset_time):
|
def send_capacity_alert(username, remaining, reset_time):
|
||||||
"""Send email alert when an author's tweet capacity is full."""
|
"""Send email alert when an author's tweet capacity is full."""
|
||||||
# Check if we should send notification
|
# Always use string for reset_time
|
||||||
if not should_send_notification(username, reset_time):
|
reset_time_str = str(reset_time)
|
||||||
|
if not should_send_notification(username, reset_time_str):
|
||||||
logger.info(f"Skipping duplicate notification for {username}")
|
logger.info(f"Skipping duplicate notification for {username}")
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
msg = MIMEMultipart()
|
msg = MIMEMultipart()
|
||||||
msg['From'] = EMAIL_CONFIG['from_email']
|
msg['From'] = EMAIL_CONFIG['from_email']
|
||||||
msg['To'] = EMAIL_CONFIG['to_email']
|
msg['To'] = EMAIL_CONFIG['to_email']
|
||||||
msg['Subject'] = f"⚠️ X Capacity Alert: {username}"
|
msg['Subject'] = f"⚠️ X Capacity Alert: {username}"
|
||||||
|
|
||||||
body = f"""
|
body = f"""
|
||||||
X Tweet Capacity Alert!
|
X Tweet Capacity Alert!
|
||||||
|
|
||||||
Username: {username}
|
Username: {username}
|
||||||
Time: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}
|
Time: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}
|
||||||
Remaining Tweets: {remaining}/17
|
Remaining Tweets: {remaining}/17
|
||||||
Reset Time: {reset_time}
|
Reset Time: {reset_time_str}
|
||||||
|
|
||||||
This author has reached their daily tweet limit.
|
This author has reached their daily tweet limit.
|
||||||
The quota will reset at the time shown above.
|
The quota will reset at the time shown above.
|
||||||
|
|
||||||
This is an automated alert from your foodie_automator system.
|
This is an automated alert from your foodie_automator system.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
msg.attach(MIMEText(body, 'plain'))
|
msg.attach(MIMEText(body, 'plain'))
|
||||||
|
|
||||||
with smtplib.SMTP(EMAIL_CONFIG['smtp_server'], EMAIL_CONFIG['smtp_port']) as server:
|
with smtplib.SMTP(EMAIL_CONFIG['smtp_server'], EMAIL_CONFIG['smtp_port']) as server:
|
||||||
server.starttls()
|
server.starttls()
|
||||||
server.login(EMAIL_CONFIG['smtp_username'], EMAIL_CONFIG['smtp_password'])
|
server.login(EMAIL_CONFIG['smtp_username'], EMAIL_CONFIG['smtp_password'])
|
||||||
server.send_message(msg)
|
server.send_message(msg)
|
||||||
|
|
||||||
logger.info(f"Sent capacity alert email for {username}")
|
logger.info(f"Sent capacity alert email for {username}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to send capacity alert email: {e}")
|
logger.error(f"Failed to send capacity alert email: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user