Skip to content

Commit aeb4723

Browse files
committed
feat: Integrate Gemini API for hints and quotes
- Added `gemini_service` to handle gemini api calls. - Updated `core_logic` to calculate hint count and fetch AI hints. - Updated `email_service` template to display new AI hints section and AI-generated quotes. - added halo lib's animated spinners for enhanced CLI visuals.
1 parent f4deb73 commit aeb4723

File tree

6 files changed

+351
-148
lines changed

6 files changed

+351
-148
lines changed

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def main():
7777
except KeyboardInterrupt:
7878
print("\n")
7979
rprint(Panel.fit(
80-
"[bold yellow]Shutting down the bot[/bold yellow]\n",
80+
"[bold yellow]Shutting down the bot[/bold yellow]\nSending mail to admin",
8181
title=" Goodbye! :(",
8282
border_style="cyan",
8383
padding=(1, 4)

src/config.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@
1515
SMTP_SERVER = os.getenv("SMTP_SERVER", "smtp.gmail.com")
1616
SMTP_PORT = int(os.getenv("SMTP_PORT", 587))
1717
LEETCODE_API_URL = os.getenv("LEETCODE_API_URL", "https://leetcode.com/graphql")
18-
19-
# --- Static Config (App-level constants) ---
20-
QUOTES = [
21-
"Keep pushing! You’re closer than you think 💪",
22-
"Consistency beats motivation every time ⚡",
23-
"Another problem solved — another step ahead 🚀",
24-
"Your hard work today is your success tomorrow 💫",
25-
"One problem a day keeps the bugs away 🧠"
26-
]
18+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
2719

2820
# --- GraphQL Queries ---
2921
QUERY_DAILY_QUESTION = """
@@ -36,6 +28,7 @@
3628
titleSlug
3729
difficulty
3830
hints
31+
acRate
3932
topicTags{
4033
name
4134
}

src/core_logic.py

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,96 @@
44
from . import leetcode_api
55
from halo import Halo
66
from . import email_service
7+
from . import gemini_service
8+
9+
10+
def get_hint_count(difficulty, acRate):
11+
"""
12+
Calculates the number of hints to show based on your logic.
13+
"""
14+
acRate = float(acRate)
15+
16+
if difficulty == "Easy":
17+
return 1 if acRate >= 60 else 2
18+
elif difficulty == "Medium":
19+
return 2 if acRate >= 55 else 3
20+
elif difficulty == "Hard":
21+
return 3 if acRate >= 40 else 4
22+
23+
return 2 # Default fallback
24+
725

826
def run_check():
927
"""Main logic to check submissions for each user and send emails."""
1028
ist_now = datetime.utcnow() + timedelta(hours=5, minutes=30)
1129
print(f"\n[{ist_now.strftime('%Y-%m-%d %H:%M:%S')} IST] Starting submission check...")
1230

13-
daily_slug, question_link = leetcode_api.get_daily_question()
14-
if not daily_slug:
31+
# Get all question data
32+
question_data = leetcode_api.get_daily_question()
33+
if not question_data:
1534
print("Aborting check since daily question could not be fetched.")
1635
return
1736

18-
print(f"Today's POTD is '{daily_slug}'")
37+
q_details = question_data['question']
38+
q_link = question_data['fullLink']
39+
40+
print(f"Today's POTD is '{q_details['title']}' ({q_details['difficulty']})")
1941
today = ist_now.date()
2042

2143
users = config.load_users()
2244
if not users:
2345
print("No users loaded from users.json. Exiting check.")
2446
return
2547

48+
# Get one AI quote for everyone for this run
49+
with Halo(text='Fetching quote from gemini...', spinner='earth', color='cyan') as spinner:
50+
try:
51+
ai_quote = gemini_service.get_motivational_quote()
52+
spinner.succeed('Quote fetched successfully!')
53+
except Exception as e:
54+
spinner.fail(f'Failed to get motivational quote: {e}')
55+
56+
2657
for user in users:
2758
username, email = user["username"], user["email"]
2859
print(f"\n🔍 Checking user: {username}")
2960

30-
#submissions = leetcode_api.get_recent_submissions(username)
3161
with Halo(text='Fetching submissions...', spinner='star2', color='cyan') as spinner:
3262
submissions = leetcode_api.get_recent_submissions(username)
3363
spinner.succeed('Submissions fetched successfully!')
3464

3565
solved_today = any(
36-
sub["titleSlug"] == daily_slug and
66+
sub["titleSlug"] == q_details["titleSlug"] and
3767
(datetime.fromtimestamp(int(sub["timestamp"])) + timedelta(hours=5, minutes=30)).date() == today
3868
for sub in submissions
3969
)
4070

71+
ai_hints = []
72+
4173
if solved_today:
4274
print(f"[ {username} ] has already solved the daily problem.")
43-
quote = random.choice(config.QUOTES)
44-
html = email_service.build_html_email(username, daily_slug, question_link, True, quote)
4575
subject = f"Awesome! You solved today’s LeetCode challenge!"
4676
else:
47-
print(f" [ {username} ]has not solved the daily problem yet. Sending reminder.")
48-
html = email_service.build_html_email(username, daily_slug, question_link, False)
77+
print(f" [ {username} ] has not solved the daily problem yet sending reminder...")
4978
subject = f"⏳ Reminder: Solve Today’s LeetCode Problem!"
5079

80+
hint_count = get_hint_count(q_details['difficulty'], q_details['acRate'])
81+
print(f"Difficulty: {q_details['difficulty']}, AC Rate: {format(float(q_details['acRate']), '.2f')}% -> Generating {hint_count} hints.")
82+
83+
with Halo(text='Calling Gemini...', spinner='arrow3', color='blue') as spinner:
84+
ai_hints = gemini_service.generate_optimal_hints(q_details, hint_count)
85+
spinner.succeed('Hints generated successfully!')
86+
87+
88+
# Send email
89+
html = email_service.build_html_email(
90+
username=username,
91+
title=q_details['title'],
92+
link=q_link,
93+
solved=solved_today,
94+
quote=ai_quote,
95+
hints=ai_hints
96+
)
5197

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

0 commit comments

Comments
 (0)