-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcron.py
257 lines (229 loc) · 8.26 KB
/
cron.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
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
import requests
import bs4
import os, django, json
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "codedigger.settings")
django.setup()
from lists.models import Solved
from user.models import Profile, User
from problem.models import Problem
from django.core.mail import send_mail
from codedigger.settings import EMAIL_HOST_USER
from codeforces.api import user_status
from utils.exception import ValidationException
def cron_codeforces(user):
if user is None:
return
cf_handle = Profile.objects.get(owner=user).codeforces
if cf_handle == None:
return
try:
elements = user_status(handle=cf_handle)
except ValidationException:
return
limit = 10
for ele in elements:
if 'verdict' not in ele or 'contestId' not in ele or ele[
'verdict'] != 'OK':
continue
prob_id = str(ele['problem']['contestId']) + str(
ele['problem']['index'])
prob = Problem.objects.filter(prob_id=prob_id, platform='F')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created:
limit -= 1
if limit <= 0:
break
continue
def cron_uva(user):
if user is None:
return
uva_id = Profile.objects.get(owner=user).uva_id
if uva_id is None:
return
url1 = "https://uhunt.onlinejudge.org/api/subs-user/" + str(uva_id)
req1 = requests.get(url1)
if req1.status_code != 200:
return
req1 = req1.json()
sorted_req1 = sorted(req1['subs'], key=lambda k: k[4], reverse=True)
limit = 10
for ele in sorted_req1:
if str(ele[2]) != '90':
continue
prob = Problem.objects.filter(prob_id=str(ele[1]), platform='U')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created:
limit -= 1
if limit <= 0:
break
continue
def cron_atcoder(user):
if user is None:
return
atcoder_handle = Profile.objects.get(owner=user).atcoder
if atcoder_handle is None:
return
url = 'https://kenkoooo.com/atcoder/atcoder-api/results?user=' + atcoder_handle
req = requests.get(url)
if req.status_code != 200:
return
req = req.json()
sorted_req = sorted(req, key=lambda k: k['epoch_second'], reverse=True)
limit = 10
for ele in sorted_req:
if ele['result'] != "AC":
continue
prob = Problem.objects.filter(prob_id=ele['problem_id'], platform='A')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(user=user,
problem=prob[0])
if not created:
limit -= 1
if limit <= 0:
break
continue
def cron_codechef(user):
if user is None:
return
codechef_handle = Profile.objects.get(owner=user).codechef
if codechef_handle is None:
return
url = 'https://www.codechef.com/users/' + str(codechef_handle)
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
problems_solved = soup.find(
'section', {'class': 'rating-data-section problems-solved'})
if problems_solved is None:
return
if problems_solved.find('h5').text == 'Fully Solved (0)':
return
for ele in problems_solved.find('article').find_all('a'):
prob = Problem.objects.filter(prob_id=ele.text, platform='C')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(problem=prob[0],
user=user)
def cron_spoj(user):
if user is None:
return
spoj_handle = Profile.objects.get(owner=user).spoj
if spoj_handle == None:
return
url = 'https://www.spoj.com/users/' + spoj_handle
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
problems = soup.find('table', {'class': 'table table-condensed'})
if problems is None:
return
for ele in problems.find_all('td'):
if ele.text == "":
continue
prob = Problem.objects.filter(prob_id=ele.text, platform='S')
if not prob.exists():
continue
solve, created = Solved.objects.get_or_create(problem=prob[0],
user=user)
def updater():
subject = 'Codeforces update Problems Started'
message = 'This is automated message from Codedigger which tells that your codeforces updation has started'
recepient = '[email protected]'
send_mail(subject,
message,
EMAIL_HOST_USER, [recepient],
fail_silently=False)
for ele in User.objects.all():
cron_codeforces(ele)
cron_uva(ele)
cron_atcoder(ele)
cron_codechef(ele)
cron_spoj(ele)
subject = 'Codeforces update Problems Started'
message = 'This is automated message from Codedigger which tells that your codeforces updation has ended'
recepient = '[email protected]'
send_mail(subject,
message,
EMAIL_HOST_USER, [recepient],
fail_silently=False)
msg = '{} Update List {}'
def codeforces_updater():
send_mail(msg.format('Codeforces', 'Started'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
for ele in User.objects.all():
cron_codeforces(ele)
send_mail(msg.format('Codeforces', 'Finished'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
def uva_updater():
send_mail(msg.format('Uva', 'Started'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
for ele in User.objects.all():
cron_uva(ele)
send_mail(msg.format('Uva', 'Finished'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
def codechef_updater():
send_mail(msg.format('Codechef', 'Started'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
for ele in User.objects.all():
cron_codechef(ele)
send_mail(msg.format('Codechef', 'Finished'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
def atcoder_updater():
send_mail(msg.format('Atcoder', 'Started'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
for ele in User.objects.all():
cron_atcoder(ele)
send_mail(msg.format('Atcoder', 'Finished'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
def spoj_updater():
send_mail(msg.format('Spoj', 'Started'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
for ele in User.objects.all():
cron_spoj(ele)
send_mail(msg.format('Spoj', 'Finished'),
'OK',
EMAIL_HOST_USER, ['[email protected]'],
fail_silently=True)
def codechef_list(user):
if user is None or user == "":
return
codechef_handle = Profile.objects.get(owner__username=user).codechef
if codechef_handle is None:
return
url = 'https://www.codechef.com/users/' + str(codechef_handle)
res = requests.get(url)
soup = bs4.BeautifulSoup(res.content, 'html.parser')
problems_solved = soup.find(
'section', {'class': 'rating-data-section problems-solved'})
if problems_solved.find('h5').text == 'Fully Solved (0)':
return []
prob_list = set()
for ele in problems_solved.find('article').find_all('a'):
#testing purposes
if not Problem.objects.filter(prob_id=ele.text, platform='C').exists():
continue
prob_list.add(ele.text)
return prob_list