-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
435 lines (360 loc) · 14.1 KB
/
agent.py
File metadata and controls
435 lines (360 loc) · 14.1 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import webbrowser
import os
import time
import qrcode
import requests
from datetime import datetime
import sys
from art import text2art
from PIL import Image, ImageDraw, ImageFont
import random
import subprocess
from rich.markdown import Markdown
import rich
from rich.console import Console
from rich.theme import Theme
from rich.prompt import Prompt
theme = Theme(
{"prompt": "bold #FF6E4A", "agent": "italic #00FFFF", "arrow": "blink yellow"}
)
console = Console(theme=theme)
def clear_screen():
os.system("cls" if os.name == "nt" else "clear")
def get_terminal_width():
try:
return os.get_terminal_size().columns
except OSError:
return 80
lines = [
"\033[34m /$$ /$$\033[0m",
"\033[34m| $$ |__/\033[0m",
"\033[34m| $$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ \033[0m",
"\033[34m| $$ | $$| $$__ $$ /$$__ $$ |____ $$ /$$__ $$\033[0m",
"\033[34m| $$ | $$| $$ | $$| $$_____/ /$$__ $$| $$\033[0m",
"\033[34m| $$$$$$$$| $$| $$ | $$| $$$$$$$| $$$$$$$| $$ \033[0m",
"\033[34m|________/|__/|__/ |__/ \\_______/ \\_______/|__/ \033[0m",
"\033[32m Developed by Linus Shyu \033[0m",
]
def visible_length(s):
import re
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
return len(ansi_escape.sub("", s))
max_width = max(visible_length(line) for line in lines)
terminal_width = get_terminal_width()
def display_at_position(padding):
clear_screen()
for line in lines:
print(" " * padding + line)
sys.stdout.flush()
try:
for padding in range(0, terminal_width - max_width + 1):
display_at_position(padding)
time.sleep(0.05)
center_position = (terminal_width - max_width) // 2
for padding in range(terminal_width - max_width, center_position - 1, -1):
display_at_position(padding)
time.sleep(0.05)
display_at_position(center_position)
except KeyboardInterrupt:
clear_screen()
for line in lines:
print(line)
def get_user_rating(username):
try:
url = f"https://codeforces.com/api/user.rating?handle={username}"
response = requests.get(url)
data = response.json()
if data["status"] == "OK":
contests = data["result"]
return [
(
c["contestId"],
c["contestName"],
c["ratingUpdateTimeSeconds"],
c["newRating"],
)
for c in contests
]
else:
console.print(
"[bright_red]Error fetching rating data:[/]",
data.get("comment", "Unknown error"),
)
return None
except Exception as e:
console.print("[bright_red]Error:[/]", e)
return None
def draw_ascii_chart(data, width=60, height=20):
if not data:
return
ratings = [x[3] for x in data]
dates = [datetime.fromtimestamp(x[2]).strftime("%Y-%m") for x in data]
min_rating = min(ratings)
max_rating = max(ratings)
range_rating = max_rating - min_rating
if range_rating == 0:
range_rating = 1
normalized = [int((r - min_rating) / range_rating * (height - 1))
for r in ratings]
chart = []
for y in range(height - 1, -1, -1):
line = []
for i, val in enumerate(normalized):
if i >= width:
break
if val == y:
line.append("[brigth_red]●[/]")
elif val > y:
line.append("[bright_green]│[/]")
else:
line.append("[bright_white] [/]")
current_rating = int(min_rating + (y / (height - 1)) * range_rating)
line.append(f" {current_rating}")
chart.append("".join(line))
x_labels = []
step = max(1, len(dates) // width)
for i in range(0, min(len(dates), width), step):
x_labels.append(dates[i][-2:])
chart.append(" ".join(x_labels))
return "\n".join(chart)
def show_rating_history(username):
rating_data = get_user_rating(username)
if not rating_data:
console.print(
f"[bold red]Could not fetch rating data for {username}[/]")
return
console.print(
f"[bright_green]\nCodeforces Rating History ({username}):[/]")
console.print("[bright_yellow]=[/]" * 70)
current_rating = rating_data[-1][3]
max_rating = max(r[3] for r in rating_data)
min_rating = min(r[3] for r in rating_data)
contests_count = len(rating_data)
console.print(f"[bright_red]Current Rating: {current_rating}[/]")
console.print(f"[bright_green]Highest Rating: {max_rating}[/]")
console.print(f"[britht_yellow]Lowest Rating: {min_rating}[/]")
console.print(f"[bright_blue]Contests Participated: {contests_count}[/]")
console.print("[bright_magenta]\nRating Chart:[/]")
chart = draw_ascii_chart(rating_data)
if chart:
console.print(chart)
console.print("[bright_red] \nLast 5 Contests: [/]")
for contest in rating_data[-5:]:
date = datetime.fromtimestamp(contest[2]).strftime("%Y-%m-%d")
console.print(
f"[bright_green]{date}: {contest[1]} (Rating: {contest[3]})[/]")
def main():
while True:
style = "[bright_green]What[/bright_green] [bright_yellow]you[/bright_yellow] [bright_blue]want to[/bright_blue] [agent]do[/agent] [bright_magenta]➜[bright_magenta] "
command = Prompt.ask(style, console=console)
if command == "home":
webbrowser.open("https://atcoder.jp/")
elif command == "contest":
webbrowser.open("https://atcoder.jp/contests/")
elif command == "rank":
webbrowser.open("https://atcoder.jp/ranking")
elif command == "userdata":
username = input("Enter your username: ")
webbrowser.open(f"https://atcoder.jp/users/{username}")
elif command == "play":
contest_name = input("Enter contest name: ")
webbrowser.open(f"https://atcoder.jp/contests/{contest_name}")
elif command == "task":
contest_name = input("Enter contest name: ")
task_name = input("Enter task name (a-g): ").lower()
if task_name in "abcdefg":
webbrowser.open(
f"https://atcoder.jp/contests/{contest_name}/tasks/{contest_name}_{task_name}"
)
elif command == "submit":
contest_name = input("Enter contest name: ")
webbrowser.open(
f"https://atcoder.jp/contests/{contest_name}/submit")
elif command == "stand":
contest_name = input("Enter contest name: ")
webbrowser.open(
f"https://atcoder.jp/contests/{contest_name}/standings")
elif command == "code":
user = input("Enter your Mac username: ")
try:
os.chdir(f"/Users/{user}/Desktop")
folder_name = input("Enter contest name for folder: ")
os.mkdir(folder_name)
os.chdir(f"/Users/{user}/Desktop/{folder_name}")
os.system("touch A.cpp B.cpp C.cpp D.cpp E.cpp F.cpp G.cpp")
cpp_code = """#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double lb;
typedef long long ll;
typedef long l;
#define endl '\\n';
void solve()
{
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while(T--)
{
solve();
}
return 0;
}
"""
for filename in [
"A.cpp",
"B.cpp",
"C.cpp",
"D.cpp",
"E.cpp",
"F.cpp",
"G.cpp",
]:
with open(filename, "w") as f:
f.write(cpp_code)
os.system(f"nvim /Users/{user}/Desktop/{folder_name}")
except Exception as e:
print(
f"[bright_red]Error: {e}\nPlease check your username and try again![/]"
)
elif command == "clear":
os.system("clear")
elif command == "time":
console.print(
time.strftime(
"[bright_red]%Y-%m-%d %H:%M:%S[/]", time.localtime(
time.time())
)
)
elif command == "about":
print("\033[34m /$$ /$$\033[0m")
print("\033[34m| $$ |__/\033[0m")
print(
"\033[34m| $$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ \033[0m"
)
print(
"\033[34m| $$ | $$| $$__ $$ /$$__ $$ |____ $$ /$$__ $$\033[0m"
)
print(
"\033[34m| $$ | $$| $$ | $$| $$_____/ /$$__ $$| $$\033[0m")
print(
"\033[34m| $$$$$$$$| $$| $$ | $$| $$$$$$$| $$$$$$$| $$ \033[0m")
print(
"\033[34m|________/|__/|__/ |__/ \\_______/ \\_______/|__/ \033[0m")
print(
"\033[34mWeChat QR code payment to support this project:\033[0m")
code = "wxp://f2f08Xmtax1P6TX2gayuRlMjXvgWRIJSXz5TmEnDiWWHgLLc3W7dIqFeUqjb4g8DAPp4"
qr = qrcode.QRCode(version=1, box_size=1, border=1)
qr.add_data(code)
qr.print_ascii()
print("\033[34mDeveloped by Linus Shyu\033[0m")
print("\033[34mSupport this project to keep it running!\033[0m")
print(
"GitHub Repository: \033[4mhttps://github.com/Linus-Shyu/AT-Tool\033[0m"
)
print(
"Developer Bilibili: \033[4mhttps://space.bilibili.com/411591950\033[0m"
)
print(
"Developer YouTube: \033[4mhttps://www.youtube.com/@LinusShyu\033[0m")
print(" ")
print(" ")
print("----------------------------------------------------")
print(
"\033[38;2;255;215;0mBNB Chain support:https://xterminalapp.github.io/BNB/\033[0m"
)
print("----------------------------------------------------")
print(" ")
print(" ")
art_text = text2art("> ./hack.sh", font="random")
img = Image.new("RGB", (300, 300), "black")
draw = ImageDraw.Draw(img)
font = ImageFont.load_default()
draw.text((10, 50), art_text, font=font, fill="cyan")
img.save("hacker_art.png")
open("./hacker_art.png")
url = "https://api.coingecko.com/api/v3/simple/price?ids=binancecoin&vs_currencies=usd"
response = requests.get(url)
data = response.json()
if "binancecoin" in data and "usd" in data["binancecoin"]:
price = data["binancecoin"]["usd"]
print(
f"\033[32mBNB Price Now: \033[38;2;255;215;0m{price:.4f} USD\033[0m"
)
else:
print("\033[31mFailed to get the price of BNB\033[0m")
elif command == "exit":
confirm = console.input(
"[bright_red]Are you sure to close all web pages? (y/n): [/]"
)
if confirm.lower() == "y":
os.system("pkill Google Chrome")
break
if confirm.lower() == "n":
break
elif command == "help":
console.print("[bright_yellow]\nAvailable commands:[/]")
console.print(
"[bright_yellow]home - Open AtCoder home page[/]")
console.print("[bright_yellow]contest - Open contests page[/]")
console.print(
"[bright_yellow]rank - Open global rankings[/]")
console.print("[bright_yellow]userdata - View user profile[/]")
console.print(
"[bright_yellow]play - Open specific contest[/]")
console.print(
"[bright_yellow]task - Open specific problem (a-g)[/]")
console.print(
"[bright_yellow]submit - Open submission page[/]")
console.print(
"[bright_yellow]stand - Open contest standings[/]")
console.print(
"[bright_yellow]code - Create C++ template files for contest[/]"
)
console.print(
"[bright_yellow]clear - Clear terminal screen[/]")
console.print("[bright_yellow]time - Show current time[/]")
console.print(
"[bright_yellow]rating - Show Codeforces rating graph[/]")
console.print(
"[bright_yellow]about - Show about information[/]")
console.print("[bright_yellow]exit - Exit the program[/]")
console.print(
"[bright_yellow]help - Show this help message\n[/]")
elif command == "rating":
username = console.input(
"[bright_white]Enter Codeforces username: [/]")
show_rating_history(username)
elif command.strip() == "":
continue
elif command == "local":
while True:
prompt = "[prompt]Ollama[/prompt] [agent]XTerminal Agent[/agent] [arrow]➜[/arrow] "
problem = Prompt.ask(prompt, console=console)
process = subprocess.Popen(
["ollama", "run", "llama3", problem],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1, # 行缓冲
universal_newlines=True,
)
for line in process.stdout:
rich.print(Markdown(line), end="")
if problem == "quit":
break
elif command == "remote":
os.system("./axec")
time.sleep(1)
else:
console.print(
"[cyan]Command not found. Type 'help' for available commands.[/]"
)
if __name__ == "__main__":
main()