|
4 | 4 | from . import leetcode_api |
5 | 5 | from halo import Halo |
6 | 6 | 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 | + |
7 | 25 |
|
8 | 26 | def run_check(): |
9 | 27 | """Main logic to check submissions for each user and send emails.""" |
10 | 28 | ist_now = datetime.utcnow() + timedelta(hours=5, minutes=30) |
11 | 29 | print(f"\n[{ist_now.strftime('%Y-%m-%d %H:%M:%S')} IST] Starting submission check...") |
12 | 30 |
|
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: |
15 | 34 | print("Aborting check since daily question could not be fetched.") |
16 | 35 | return |
17 | 36 |
|
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']})") |
19 | 41 | today = ist_now.date() |
20 | 42 |
|
21 | 43 | users = config.load_users() |
22 | 44 | if not users: |
23 | 45 | print("No users loaded from users.json. Exiting check.") |
24 | 46 | return |
25 | 47 |
|
| 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 | + |
26 | 57 | for user in users: |
27 | 58 | username, email = user["username"], user["email"] |
28 | 59 | print(f"\n🔍 Checking user: {username}") |
29 | 60 |
|
30 | | - #submissions = leetcode_api.get_recent_submissions(username) |
31 | 61 | with Halo(text='Fetching submissions...', spinner='star2', color='cyan') as spinner: |
32 | 62 | submissions = leetcode_api.get_recent_submissions(username) |
33 | 63 | spinner.succeed('Submissions fetched successfully!') |
34 | 64 |
|
35 | 65 | solved_today = any( |
36 | | - sub["titleSlug"] == daily_slug and |
| 66 | + sub["titleSlug"] == q_details["titleSlug"] and |
37 | 67 | (datetime.fromtimestamp(int(sub["timestamp"])) + timedelta(hours=5, minutes=30)).date() == today |
38 | 68 | for sub in submissions |
39 | 69 | ) |
40 | 70 |
|
| 71 | + ai_hints = [] |
| 72 | + |
41 | 73 | if solved_today: |
42 | 74 | 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) |
45 | 75 | subject = f"Awesome! You solved today’s LeetCode challenge!" |
46 | 76 | 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...") |
49 | 78 | subject = f"⏳ Reminder: Solve Today’s LeetCode Problem!" |
50 | 79 |
|
| 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 | + ) |
51 | 97 |
|
52 | 98 | with Halo(text='Sending mail..', spinner='bouncingBar', color='yellow') as spinner: |
53 | 99 | try: |
|
0 commit comments