-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
297 lines (270 loc) · 10.6 KB
/
main.py
File metadata and controls
297 lines (270 loc) · 10.6 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
import sys
import io
import traceback
import discord
from discord.ext import commands, tasks
from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib.dates import num2date
import db
import scraper
kattis_conn = db.KattisDbConn("db/kattis.db")
user_conn = db.UserDbConn("db/user.db")
scraper = scraper.Scraper()
client = commands.Bot("$", help_command=None)
def main():
with open('token.txt', 'r') as f:
token = f.read().strip()
client.run(token)
@client.event
async def on_ready():
global main_channel
main_channel = client.get_channel(804351000113971284)
scrape_timer.start()
scrape_fails = 0
@tasks.loop(minutes=10)
async def scrape_timer():
await client.wait_until_ready()
global scrape_fails
curr = datetime.now().date()
prev = kattis_conn.max_time()
if(prev == None or prev.date() != curr):
try:
print("scraping...")
scraper.scrape()
print("ok!")
if(scrape_fails):
print("Scraped successfully!")
await main_channel.send("Lyckades scrapea nu :)")
scrape_fails = 0
except Exception as crap:
print("Scraping failed!\n\n", crap, "\n")
traceback.print_exc()
await main_channel.send("Scraping failed!!")
scrape_fails += 1
if(scrape_fails >= 10):
print("10 consecutive fails; shutting down.")
await main_channel.send("RAGE QUITTING")
await client.close()
@client.command()
async def setname(ctx, *args):
name = " ".join(args)
user_conn.set_realname(str(ctx.message.author.id), name)
await ctx.send("Your name was set to `" + name + "`!")
@client.command()
async def whoami(ctx):
discord_id = str(ctx.message.author.id)
name = user_conn.get_realname(discord_id)
if(name == None): await ctx.send("I don't know! Set your name with `setname Name`.")
else: await ctx.send("You are `" + name + "`!")
@client.command()
async def forgetme(ctx):
discord_id = str(ctx.message.author.id)
name = user_conn.remove_realname(discord_id)
await ctx.send("You have been forgotten!")
@client.command()
async def help(ctx):
await ctx.send(
"`$kattis [user|uni|country] [\"Name1\" \"Name2\" ...] [top=[global|swe|chalmers][x]] [score|rank|nof_unis|nof_users] [global|swe|chalmers] [days=x] [log] [nozoom] [legend|nolegend] [ignore-not-found]` (slightly simplified...)\n"
"`$setname name`\n`$whoami`\n`$forgetme`")
# $kattis [[type=]user|uni|country] [[name=]name1,name2,...] [top[=[global|swe|chalmers][x]]] [[variable=]score|rank|nof_unis|nof_users]
# [[ranklist=]global|swe|chalmers] [days=x] [log] [nozoom] [ignore-not-found] [legend|nolegend]
@client.command()
async def kattis(ctx, *args):
named_args = dict()
bool_args = set()
for arg in args:
if('=' in arg):
x, y = arg.split('=')
if(x in named_args):
await ctx.send(f"`{x}=...` twice :(")
return
named_args[x] = y
else:
if(arg in bool_args):
await ctx.send(f"`{arg}` twice :(")
return
bool_args.add(arg)
if('top' in bool_args and 'top' not in named_args): named_args['top'] = ""
if('name' in named_args): named_args['name'] = [x.strip() for x in named_args['name'].split(',')]
else: named_args['name'] = []
newargs = dict()
d = {
'user':'type', 'uni':'type', 'country':'type',
'score':'variable', 'rank':'variable', 'nof_unis':'variable', 'nof_users':'variable',
'global':'ranklist', 'swe':'ranklist', 'chalmers':'ranklist',
}
for x in bool_args:
if(x in d):
if(d[x] in named_args):
await ctx.send(f"`{x}` and `{d[x]}={named_args[d[x]]}` :(")
return
if(d[x] in newargs):
await ctx.send(f"`{x}` and `{newargs[d[x]]}` :(")
return
newargs[d[x]] = x
elif(' ' in x or x[0].isupper()):
named_args['name'].append(x)
elif(x == 'me'):
discord_id = str(ctx.message.author.id)
name = user_conn.get_realname(discord_id)
if(name == None):
await ctx.send("Set your name with `setname Name`.")
return
named_args['name'].append(name)
elif(x[0:3] == "<@!"):
discord_id = ''.join(filter(str.isdigit, x))
name = user_conn.get_realname(discord_id)
if(name == None):
await ctx.send(x + " has no name :(")
return
named_args['name'].append(name)
else:
if(x not in ['log', 'nozoom', 'ignore-not-found', 'legend', 'nolegend']):
await ctx.send(f"`{x}` ???????")
return
named_args.update(newargs)
if('type' not in named_args): named_args['type'] = 'user'
if(named_args['type'] not in ['user', 'uni', 'country']):
await ctx.send("Unknown type, must be `user`, `uni` or `country`.")
return
if('variable' not in named_args): named_args['variable'] = 'score'
if(named_args['variable'] not in ['score','rank','nof_unis','nof_users']):
await ctx.send("Unknown variable, must be `score`, `rank`, `nof_unis` or `nof_users`.")
return
if(named_args['variable'] == 'nof_unis' and named_args['type'] != 'country'):
await ctx.send("`variable=nof_unis` can only be used when `type=country`.")
return
if(named_args['variable'] == 'nof_users' and named_args['type'] == 'user'):
await ctx.send("`variable=nof_users` can not be used when `type=user`.")
return
if(named_args['type'] == 'user' and named_args['variable'] == 'rank' and 'ranklist' not in named_args):
named_args['ranklist'] = 'chalmers'
if('ranklist' in named_args and named_args['ranklist'] not in ['global', 'swe', 'chalmers']):
await ctx.send("Unknown ranklist, must be `global`, `swe` or `chalmers`.")
return
if('top' in named_args or named_args['name']==[]):
top = named_args.get('top', '')
toplist = named_args.get('ranklist', {'user':'chalmers','uni':'swe','country':'global'}[named_args['type']])
if(top.startswith('chalmers')): toplist = 'chalmers'
if(top.startswith('swe')): toplist = 'swe'
if(top.startswith('global')): toplist = 'global'
if(top.startswith(toplist)): top = top[len(toplist):]
topcnt = 5 #default
if(len(top)):
topcnt = int(top) #error?
e = kattis_conn.get_top(named_args['type'], toplist, topcnt)
if(len(e) != topcnt and 'ignore-not-found' not in bool_args):
await ctx.send(f"Length is only {len(e)}.")
return
named_args['name'].extend(e)
named_args['name'] = list(set(named_args['name'])) #remove duplicates & sort
if('days' not in named_args): named_args['days'] = 10**5
else:
named_args['days'] = int(named_args['days']) #error?
mintimestamp = int(datetime.now().timestamp()) - int(named_args['days'])*24*3600
plt.clf()
plt.xticks(rotation = 20)
plt.ylabel({'score':'Score', 'rank':'Rank', 'nof_users':'#users', 'nof_unis':'#unis'}[named_args['variable']])
if(named_args['variable'] != 'score'):
plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True)) #only integer ticks
if(named_args['variable'] == 'rank'):
plt.gca().invert_yaxis()
if("log" in bool_args):
plt.yscale("log")
if('log' in bool_args and 'nozoom' in bool_args):
await ctx.send("`log` and `nozoom` ... :(")
return
nofLines = 0
if(named_args['type'] == 'user'):
history = kattis_conn.history(mintimestamp, 'user', named_args['name'], named_args.get('ranklist', 'all'))
for name,s_history in history:
timestamps, ranks, names, places, unis, scores = [[x[i] for x in s_history] for i in range(6)]
dates = list(map(datetime.fromtimestamp, timestamps))
assert(sorted(dates) == dates)
if(dates):
v = scores
if(named_args['variable'] == 'rank'):
v = ranks
plt.plot(dates, v, label=name)
nofLines+=1
else:
if('ignore-not-found' not in bool_args):
if('ranklist' in named_args):
await ctx.send(f"No {named_args['ranklist']} history found for the user {name} :(")
else:
await ctx.send(f"No history found for the user {name} :(")
return
elif(named_args['type'] == 'uni'):
history = kattis_conn.history(mintimestamp, 'uni', named_args['name'], named_args.get('ranklist', 'all'))
for name,s_history in history:
timestamps, ranks, names, places, nof_users, scores = [[x[i] for x in s_history] for i in range(6)]
dates = list(map(datetime.fromtimestamp, timestamps))
assert(sorted(dates) == dates)
if(dates):
v = scores
if(named_args['variable'] == 'rank'):
v = ranks
if(named_args['variable'] == 'nof_users'):
v = nof_users
plt.plot(dates, v, label=name)
nofLines+=1
else:
if('ignore-not-found' not in bool_args):
if('ranklist' in named_args):
await ctx.send(f"No {named_args['ranklist']} history found for the uni {name} :(")
else:
await ctx.send(f"No history found for the uni {name} :(")
return
elif(named_args['type'] == 'country'):
history = kattis_conn.history(mintimestamp, 'country', named_args['name'], named_args.get('ranklist', 'all'))
for name,s_history in history:
timestamps, ranks, names, nof_users, nof_unis, scores = [[x[i] for x in s_history] for i in range(6)]
dates = list(map(datetime.fromtimestamp, timestamps))
assert(sorted(dates) == dates)
if(dates):
v = scores
if(named_args['variable'] == 'rank'):
v = ranks
if(named_args['variable'] == 'nof_users'):
v = nof_users
if(named_args['variable'] == 'nof_unis'):
v = nof_unis
plt.plot(dates, v, label=name)
nofLines+=1
else:
if('ignore-not-found' not in bool_args):
if('ranklist' in named_args):
await ctx.send(f"No {named_args['ranklist']} history found for the country {name} :(")
else:
await ctx.send(f"No history found for the country {name} :(")
return
if(nofLines == 0):
await ctx.send("Nothing to show.")
return
if('nozoom' in bool_args):
#if(named_args['variable'] == 'rank'): plt.gca().set_ylim(top=1)
#else: plt.gca().set_ylim(bottom=0)
# small white space this way :)
xmin, xmax = plt.gca().get_xlim()
plt.plot([datetime.fromtimestamp((num2date(xmin).timestamp()+num2date(xmax).timestamp())/2)], [0 + (named_args['variable']=='rank')]) #add point at (x_avg,0)
if('legend' in bool_args and 'nolegend' in bool_args):
legs = [plt.gca().legend(loc=x) for x in range(11)]
for x in legs: plt.gca().add_artist(x)
await ctx.send(":D")
elif('legend' in bool_args or ('nolegend' not in bool_args and nofLines <= 5)):
plt.legend()
# order legend
handles, labels = plt.gca().get_legend_handles_labels()
labels, handles = zip(*sorted(zip(labels, handles), key=lambda t: [-1,1][named_args['variable']=='rank'] * t[1].get_ydata()[-1]))
plt.gca().legend(handles, labels)
plt.grid()
await sendgraph(ctx)
async def sendgraph(ctx):
with io.BytesIO() as image_binary:
plt.savefig(image_binary, format='png')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='graph.png'))
if(__name__ == "__main__"):
main()