-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremind.py
More file actions
171 lines (141 loc) · 5.81 KB
/
Copy pathremind.py
File metadata and controls
171 lines (141 loc) · 5.81 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# To run every morning at 9am:
# Linux/macOS: add to crontab with `crontab -e`
# 0 9 * * * cd /path/to/tracker && python remind.py
# Windows: use Task Scheduler to run `python remind.py` daily at 9:00 AM
import os
import psycopg2
import psycopg2.extras
from datetime import date
from rich.console import Console
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Optionally load .env file if python-dotenv is installed
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
def send_email(subject: str, body: str):
smtp_host = "smtp.gmail.com"
smtp_port = 587
sender = os.environ.get("REMINDER_EMAIL_FROM") # your Gmail address
password = os.environ.get("REMINDER_EMAIL_PASSWORD") # Gmail app password
recipient = os.environ.get("REMINDER_EMAIL_TO") # where to send (can be same as sender)
if not all([sender, password, recipient]):
print("Email env vars not set — skipping email.")
return
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = sender
msg["To"] = recipient
msg.attach(MIMEText(body, "plain"))
with smtplib.SMTP(smtp_host, smtp_port) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, recipient, msg.as_string())
def main():
console = Console()
database_url = os.environ.get("DATABASE_URL")
if not database_url:
console.print("[red]DATABASE_URL environment variable is not set. Exiting.[/red]")
return
try:
conn = psycopg2.connect(database_url)
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor:
cursor.execute("""
SELECT title, type, platform, url, deadline
FROM applications
WHERE deadline IS NOT NULL
AND deadline >= %s
AND type NOT IN ('job', 'internship')
ORDER BY deadline ASC
""", (date.today(),))
apps = cursor.fetchall()
except Exception as e:
console.print(f"[red]Database error: {e}[/red]")
return
finally:
if 'conn' in locals():
conn.close()
today_str = date.today().strftime("%Y-%m-%d")
header_str = f"=== DEADLINE REMINDER — {today_str} ==="
console.print(f"\n[bold white]{header_str}[/bold white]\n")
email_lines = [header_str, ""]
if not apps:
msg = "No upcoming deadlines. Either you're very organized or very behind."
console.print(msg + "\n")
email_lines.append(msg)
send_email(f"[TRACKER] No upcoming deadlines — {today_str}", "\n".join(email_lines))
return
urgent = []
coming_up = []
this_month = []
# Track plain text lists for the email separate from the rich terminal output
urgent_email = []
coming_up_email = []
this_month_email = []
for app in apps:
deadline_date = app['deadline']
days_left = (deadline_date - date.today()).days
# Handle singular vs plural for "day"
days_str = "1 day" if days_left == 1 else f"{days_left} days"
platform_parts = []
email_platform_parts = []
if app['platform']:
platform_parts.append(app['platform'])
email_platform_parts.append(app['platform'])
if app['url']:
# Using rich's hyperlink markup for clickable terminal links
platform_parts.append(f"[link={app['url']}]link[/link]")
platform_str = f" ({' — '.join(platform_parts)})" if platform_parts else ""
item_text = f" • {app['title']} [{app['type']}] — {days_str} left{platform_str}"
email_platform_str = f" ({email_platform_parts[0]})" if email_platform_parts else ""
email_item_text = f" • {app['title']} [{app['type']}] — {days_str} left{email_platform_str}"
if days_left <= 3:
urgent.append(item_text)
urgent_email.append(email_item_text)
elif days_left <= 7:
coming_up.append(item_text)
coming_up_email.append(email_item_text)
elif days_left <= 30:
this_month.append(item_text)
this_month_email.append(email_item_text)
# Print URGENT section
console.print("🔴 [bold red]URGENT (≤ 3 days)[/bold red]")
email_lines.append("🔴 URGENT (≤ 3 days)")
if urgent:
for item in urgent:
console.print(item)
for item in urgent_email:
email_lines.append(item)
else:
console.print("Nothing urgent. Stay on top of it.")
email_lines.append("Nothing urgent. Stay on top of it.")
console.print()
email_lines.append("")
# Print COMING UP section
if coming_up:
console.print("🟡 [bold yellow]COMING UP (4–7 days)[/bold yellow]")
email_lines.append("🟡 COMING UP (4–7 days)")
for item in coming_up:
console.print(item)
for item in coming_up_email:
email_lines.append(item)
console.print()
email_lines.append("")
# Print THIS MONTH section
if this_month:
console.print("📋 [bold white]THIS MONTH (8–30 days)[/bold white]")
email_lines.append("📋 THIS MONTH (8–30 days)")
for item in this_month:
console.print(item)
for item in this_month_email:
email_lines.append(item)
console.print()
email_lines.append("")
total_count = len(urgent) + len(coming_up) + len(this_month)
subject = f"[TRACKER] {total_count} deadlines coming up — {today_str}"
send_email(subject, "\n".join(email_lines))
if __name__ == "__main__":
main()