-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratingTracker.py
77 lines (61 loc) · 2.07 KB
/
ratingTracker.py
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
import Tkinter
import ttk
import requests
import webbrowser
users = [
'vici',
'noxe'
]
rk_colors = ['blue', 'purple', 'orange', 'red']
rk_thresholds = [1900, 2100, 2400, 3000]
def OnDoubleClick(event):
item = tree.selection()[0]
idx = int(tree.item(item, "text")) - 1
url = "http://codeforces.com/profile/" + users[idx]
webbrowser.open_new(url)
def treeview_sort_column(tv, col, reverse):
l = [(tv.set(k, col), k) for k in tv.get_children('')]
l.sort(reverse=reverse)
# rearrange items in sorted positions
for index, (val, k) in enumerate(l):
tv.move(k, '', index)
# reverse sort next time
tv.heading(col, command=lambda: treeview_sort_column(tv, col, not reverse))
def rankCol(rating):
for idx in range(len(rk_colors)):
if rating < rk_thresholds[idx]:
return rk_colors[idx]
return 'black'
def update():
children = tree.get_children()
str = ''
for idx in range(len(users)):
if idx == 0:
str += users[idx]
else:
str += ';' + users[idx]
re = requests.get('http://codeforces.com/api/user.info?handles=' + str)
for idx in range(len(users)):
rating = re.json()['result'][idx]['rating']
maxRating = re.json()['result'][idx]['maxRating']
tree.item(children[idx], values = (users[idx], rating, maxRating), tags = (rankCol(rating)))
for color in rk_colors:
tree.tag_configure(color, foreground=color)
win = Tkinter.Tk()
tree = ttk.Treeview(win)
columns = ("name", "rating", "maxRating")
tree["height"] = 20
tree["columns"] = columns
tree.column("name", width = 100)
tree.column("rating", width = 100)
tree.column("maxRating", width = 100)
tree.bind("<Double-1>", OnDoubleClick)
for idx in range(len(users)):
tree.insert("", idx, text = str(idx + 1) ,values=(users[idx], "", ""))
for col in columns:
tree.heading(col, text=col, command=lambda _col=col: treeview_sort_column(tree, _col, True))
btn_upd = ttk.Button(win, text = "update", command = update)
tree.pack()
btn_upd.pack()
update()
win.mainloop()