-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.py
More file actions
165 lines (133 loc) · 5.73 KB
/
Copy pathBot.py
File metadata and controls
165 lines (133 loc) · 5.73 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
import requests
import time
import os
from datetime import datetime
class CodeforcesFriendsMonitor:
def __init__(self, your_handle):
self.your_handle = your_handle
self.base_url = "https://codeforces.com/api/"
self.last_submission_ids = set()
self.friends = []
def get_user_submissions(self, handle, count=10):
"""Retrieve the latest submissions of a user."""
try:
url = f"{self.base_url}user.status?handle={handle}&from=1&count={count}"
response = requests.get(url)
data = response.json()
if data['status'] == 'OK':
return data['result']
return []
except Exception as e:
print(f"Error fetching submissions for {handle}: {e}")
return []
def play_sound(self, sound_type="default"):
"""Play notification sound on macOS."""
try:
# macOS system sounds:
sounds = {
"default": "Ping",
"glass": "Glass",
"hero": "Hero",
"sosumi": "Sosumi",
"submarine": "Submarine",
"blow": "Blow",
"bottle": "Bottle",
"frog": "Frog",
"funk": "Funk",
"pop": "Pop",
"purr": "Purr",
"tink": "Tink"
}
sound = sounds.get(sound_type, "Ping")
os.system(f'afplay /System/Library/Sounds/{sound}.aiff')
except Exception as e:
print(f"Error playing sound: {e}")
def send_notification(self, title, message):
"""Send macOS notification."""
try:
# Using osascript for native macOS notification
script = f'''
display notification "{message}" with title "{title}" sound name "Ping"
'''
os.system(f"osascript -e '{script}'")
except Exception as e:
print(f"Error sending notification: {e}")
def monitor_submissions(self, check_interval=60, sound_type="default", show_notifications=True):
"""Monitor friends' submissions."""
print(f"\n[INFO] Starting submission monitoring (every {check_interval} seconds)\n")
print("Press Ctrl+C to stop.\n")
# Fetch initial submissions to avoid alerting on old ones
for friend in self.friends:
submissions = self.get_user_submissions(friend, count=5)
for sub in submissions:
self.last_submission_ids.add(sub['id'])
print(f"[OK] {len(self.last_submission_ids)} initial submissions recorded.\n")
try:
while True:
for friend in self.friends:
submissions = self.get_user_submissions(friend, count=5)
for sub in submissions:
if sub['id'] not in self.last_submission_ids:
# New submission found!
self.last_submission_ids.add(sub['id'])
problem_name = sub['problem'].get('name', 'Unknown')
problem_index = sub['problem'].get('index', '?')
contest_id = sub.get('contestId', sub.get('problem', {}).get('contestId', '?'))
verdict = sub.get('verdict', 'TESTING')
timestamp = datetime.fromtimestamp(sub['creationTimeSeconds'])
# Display in terminal
print("-" * 60)
print("[ALERT] New Submission!")
print(f"User: {friend}")
print(f"Problem: {contest_id}{problem_index} - {problem_name}")
print(f"Verdict: {verdict}")
print(f"Time: {timestamp.strftime('%Y-%m-%d %H:%M:%S')}")
print("-" * 60)
print()
# Play sound
self.play_sound(sound_type)
# Send macOS notification
if show_notifications:
notification_msg = f"{friend} -> {contest_id}{problem_index} ({verdict})"
self.send_notification("Codeforces Submission", notification_msg)
time.sleep(check_interval)
except KeyboardInterrupt:
print("\n\nMonitoring stopped.")
def main():
print("=" * 60)
print("CF Monitor Bot - macOS Edition")
print("=" * 60)
# Enter your handle
your_handle = input("\nEnter your Codeforces handle: ").strip()
if not your_handle:
print("Error: Handle cannot be empty!")
return
monitor = CodeforcesFriendsMonitor(your_handle)
print("\nEnter your friends' handles:")
friends_input = input("Separate handles with comma: ").strip()
if friends_input:
monitor.friends = [f.strip() for f in friends_input.split(',')]
print(f"[OK] {len(monitor.friends)} friend(s) added.")
else:
print("Error: No friends entered!")
return
check_interval = input("\nEnter check interval in seconds [default: 60]: ").strip()
check_interval = int(check_interval) if check_interval.isdigit() else 60
print("\nSelect notification sound:")
print("1. Ping (Default)")
print("2. Glass")
print("3. Hero")
print("4. Submarine")
print("5. Funk")
sound_choice = input("Choice (1-5): ").strip()
sound_map = {
"1": "default",
"2": "glass",
"3": "hero",
"4": "submarine",
"5": "funk"
}
sound_type = sound_map.get(sound_choice, "default")
monitor.monitor_submissions(check_interval, sound_type)
if __name__ == "__main__":
main()