Skip to content

Commit bb008d3

Browse files
committed
chore: added daily question ttl caching and added problem label in mail
1 parent 5803a38 commit bb008d3

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ python-dotenv
66

77
rich
88
halo
9-
tqdm
9+
tqdm
10+
cachetools

src/core_logic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def run_check():
7272

7373
if solved_today:
7474
print(f"[ {username} ] has already solved the daily problem.")
75-
subject = f"Awesome! You solved today’s LeetCode challenge!"
75+
subject = "Awesome! You solved today’s LeetCode challenge!"
7676
else:
7777
print(f" [ {username} ] has not solved the daily problem yet sending reminder...")
78-
subject = f"⏳ Reminder: Solve Today’s LeetCode Problem!"
78+
subject = "⏳ Reminder: Solve Today’s LeetCode Problem!"
7979

8080

8181
print(f"Difficulty: {q_details['difficulty']}, AC Rate: {format(float(q_details['acRate']), '.2f')}% ")
@@ -94,10 +94,11 @@ def run_check():
9494
html = email_service.build_html_email(
9595
username=username,
9696
title=q_details['title'],
97+
difficulty=q_details['difficulty'],
9798
link=q_link,
9899
solved=solved_today,
99100
quote=ai_quote,
100-
hints=ai_hints
101+
hints=ai_hints,
101102
)
102103

103104
with Halo(text='Sending mail..', spinner='bouncingBar', color='yellow') as spinner:

src/email_service.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def send_email(to_email, subject, html_content):
3333
return False
3434

3535

36-
def build_html_email(username, title, link, solved, quote=None, hints=None):
36+
def build_html_email(username, title, difficulty, link, solved, quote=None, hints=None):
3737
"""Builds a responsive email with optional AI hints, quotes, and a live countdown GIF."""
3838

3939
if hints is None:
@@ -53,9 +53,20 @@ def build_html_email(username, title, link, solved, quote=None, hints=None):
5353
else:
5454
preheader_text = f"Don't forget to solve {title} today!"
5555
heading = f"Hey <a href='https://leetcode.com/u/{username}' target='_blank' style='text-decoration: none;'>{username}</a>, time to code!"
56-
subtext = f"Today's problem, <strong>{title}</strong>, is waiting for you. Don't miss out on your streak!"
5756
button_text = f"Solve '{title}' Now"
58-
button_color = "#000000"
57+
pill_color = {
58+
"Easy": "#43AA03",
59+
"Medium": "#FFBB00",
60+
"Hard": "#f8615c"
61+
}[difficulty]
62+
difficulty_badge = (
63+
f"<span style='display:inline-block; padding:4px 10px; "
64+
f"font-size:12px; font-weight:700; color:{pill_color}; border-radius:999px; "
65+
f"background:#00000030; line-height:1; vertical-align:middle;'>"
66+
f"{difficulty}</span>"
67+
)
68+
subtext = (f"Today's problem, <strong>{title}</strong> {difficulty_badge}, is waiting for you. "
69+
f"Don't miss out on your streak!")
5970
footer = f"<em>“{quote}”</em>" if quote else "<em>“Small daily improvements lead to big results.” 🌱</em>"
6071

6172
deadline_iso = get_deadline_for_potd()
@@ -160,11 +171,10 @@ def build_html_email(username, title, link, solved, quote=None, hints=None):
160171
<p style="margin: 0 0 30px 0; font-size: 16px; line-height: 1.6;">
161172
{subtext}
162173
</p>
163-
164174
<table border="0" cellpadding="0" cellspacing="0" width="100%">
165175
<tr>
166176
<td align="center">
167-
<a href="{link}" target="_blank" class="button-link" style="background-color: {button_color}; color: #ffffff; font-size: 16px; font-weight: bold; text-decoration: none; padding: 14px 22px; border-radius: 5px; display: inline-block;">
177+
<a href="{link}" target="_blank" class="button-link" style="background-color: #000000; color: #ffffff; font-size: 16px; font-weight: bold; text-decoration: none; padding: 14px 22px; border-radius: 5px; display: inline-block;">
168178
{button_text} &rarr;
169179
</a>
170180
</td>

src/leetcode_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import requests
22
from . import config
3+
from cachetools import TTLCache, cached
34

5+
cache = TTLCache(maxsize=1, ttl=540000)
6+
@cached(cache)
47
def get_daily_question():
58
"""Fetches the title and link of the daily LeetCode question"""
69
try:
10+
cached_result = cache.get('daily_question')
11+
if cached_result:
12+
print("Fetched daily question from cache.")
13+
return cached_result
14+
715
response = requests.post(
816
config.LEETCODE_API_URL,
917
json={'query': config.QUERY_DAILY_QUESTION}
1018
)
1119
response.raise_for_status()
1220
q_data = response.json()["data"]["activeDailyCodingChallengeQuestion"]
1321
q_data['fullLink'] = "https://leetcode.com" + q_data['link']
22+
23+
cache['daily_question'] = q_data
1424
return q_data
1525

1626
except Exception as e:

0 commit comments

Comments
 (0)