-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
268 lines (240 loc) · 9.49 KB
/
utils.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
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Google Code-In unofficial leaderboard utils file
# Copyright (C) 2013-2015 Ignacio Rodríguez <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from HTMLParser import HTMLParser
import sys
import urllib2
import json
import tmp
BASE_URL = "http://www.google-melange.com/gci"
TASKS_URL = BASE_URL + \
"/org/google/gci{year}/{orgname}?fmt=json&limit=1000&idx=1"
ACCEPTED_ORGS_URL = BASE_URL + \
"/org/list/public/google/gci{year}?fmt=json&limit=50&idx=1"
LEADERBOARD_URL = BASE_URL + \
"/org_scores/google/gci{year}/{orgname}?fmt=json&limit=1000&idx=0"
TASK_URL = BASE_URL + "/task/view/google/gci{year}/{taskKey}"
CURRENT_CONTEST = 2014
ORG_TASKS = {2010: {}, 2011: {}, 2012: {}, 2013: {}, 2014: {}}
ORG_LEADERBOARD = {2010: {}, 2011: {}, 2012: {}, 2013: {}, 2014: {}}
CONTEST_LEADERBOARD = {2010: {}, 2011: {}, 2012: {}, 2013: {}, 2014: {}}
ORGS_DATA = {2010: {}, 2011: {}, 2012: {}, 2013: {}, 2014: {}}
ORG_TASKS = tmp.tasks
ORGS_DATA = tmp.data
PARSER = HTMLParser()
class GCIUtils():
def __init__(self):
self.updating = False
# Start data. Take a few minutes
for year in ORGS_DATA.keys():
"""
DISABLED FOR TEST
data = self.get_orglist(year)
ORGS_DATA[year]['orglist'] = data[0]
ORGS_DATA[year]['chart_data'] = data[1]
self.update_orgs_tasks(year)
"""
self.update_leaderboard(int(year))
self.change_process_name()
def change_process_name(self, name='gci-leaderboard'):
if sys.platform == 'linux2':
import ctypes
libc = ctypes.cdll.LoadLibrary('libc.so.6')
libc.prctl(15, name, 0, 0, 0)
libc.prctl(15, name, 0, 0, 0)
def get_orglist(self, year):
url = ACCEPTED_ORGS_URL.format(year=str(year))
request = urllib2.Request(url)
data = json.loads(urllib2.urlopen(request).read())['data']['']
orglist = ['all']
# Data for the chart
orglist_data = {}
for item in data:
org_id = item['columns']['org_id']
org_name = item['columns']['name']
orglist.append(org_id)
orglist_data[org_id] = [org_name, 0]
return (orglist, orglist_data)
def update_orgs_tasks(self, year):
"""
Update org tasks, used in a Thread.
When GCI is running, this function is called every 5 minutes.
"""
self.updating = True
orglist = ORGS_DATA[year]['orglist']
for org in orglist:
if org == 'all':
continue
url = TASKS_URL.format(orgname=org, year=str(year))
print url
request = urllib2.Request(url)
data = json.loads(urllib2.urlopen(request).read())['data']['']
ORG_TASKS[year][org] = data
print "Updated orgs %s for year %d." % (str(orglist), year)
self.updating = False
def _update_leaderboard(self, year):
"""
Update org leaderboards, used in a Thread.
When GCI is running, this function is called every 5 minutes.
Currently unused.. Only works with 2014.. GCI lost data?
"""
orglist = ORGS[year]['orglist']
for org in orglist:
url = LEADERBOARD_URL.format(orgname=org, year=str(year))
request = urllib2.Request(url)
data = json.loads(urllib2.urlopen(request).read())
ORG_LEADERBOARD[year][org] = data
def update_leaderboard(self, year):
"""
Update org leaderboards, used in a Thread.
When GCI is running, this function is called every 5 minutes.
"""
orglist = ORGS_DATA[year]['orglist']
CONTEST_LEADERBOARD[year]['all'] = {}
for org in orglist:
if org == 'all':
continue
data = ORG_TASKS[year][org]
CONTEST_LEADERBOARD[year][org] = {}
for row in data:
student_name = row['columns']['student']
if student_name not in CONTEST_LEADERBOARD[year][org]:
CONTEST_LEADERBOARD[year][org][student_name] = {
'name': PARSER.unescape(student_name),
'tasks': 1}
else:
CONTEST_LEADERBOARD[year][org][student_name]['tasks'] += 1
if student_name not in CONTEST_LEADERBOARD[year]['all']:
CONTEST_LEADERBOARD[year]['all'][student_name] = {
'name': PARSER.unescape(student_name),
'tasks': 1}
else:
CONTEST_LEADERBOARD[year]['all'][
student_name]['tasks'] += 1
def get_student_tasks(self, username, year, org):
"""
Return the tasks of the student,
requires username (str), year (int), org (str)
"""
tasks_data = {
'total_tags': {
'User Interface': 0,
'Code': 0,
'Documentation/Training': 6,
'Outreach/Research': 0,
'Quality Assurance': 0},
'tasks': {}}
orgs = [org]
if org == 'all':
orgs = ORGS_DATA[year]['orglist']
for org in orgs:
if org == 'all':
continue
try:
data = ORG_TASKS[year][org]
except KeyError:
return tasks_data
for row in data:
realdata = row['columns']
if realdata['student'] != username:
continue
title = PARSER.unescape(realdata['title']).capitalize()
link = TASK_URL.format(
year=str(year),
taskKey=str(
realdata['key']))
tags = realdata['types'].split(', ')
for tag in tags:
if tag in ["Training", "Documentation"]:
tag = "Documentation/Training"
elif tag in ["Outreach", "Research"]:
tag = "Outreach/Research"
elif tag == "Testing":
tag = "Quality Assurance"
elif tag == "Translation":
tag = "User Interface"
try:
tasks_data['total_tags'][tag] += 1
except KeyError:
continue
tasks_data['tasks'][
realdata['key']] = {
'title': title.capitalize(),
'link': link,
'tags': tags,
'org': self.get_org_name(year, org),
'key': realdata['key']}
return tasks_data
def get_org_name(self, year, org_id):
if org_id == 'all':
return 'All organizations'
return ORGS_DATA[year]['chart_data'][org_id][0]
def get_tasks_count(self, year, org_id):
tags = {
'User Interface': 0,
'Code': 0,
'Documentation/Training': 0,
'Outreach/Research': 0,
'Quality Assurance': 0}
orgs = [org_id]
if org_id == 'all':
orgs = ORGS_DATA[year]['orglist']
for org in orgs:
if org == 'all':
continue
data = ORG_TASKS[year][org]
for row in data:
realdata = row['columns']
tag_s = realdata['types'].split(', ')
for tag in tag_s:
try:
if tag in ["Training", "Documentation"]:
tag = "Documentation/Training"
elif tag in ["Outreach", "Research"]:
tag = "Outreach/Research"
elif tag == "Testing":
tag = "Quality Assurance"
elif tag == "Translation":
tag = "User Interface"
tags[tag] += 1
except KeyError:
continue
return tags
def get_tasks(self, year, orgname):
orgs = list(ORGS_DATA[int(year)]['orglist'])
if orgname not in orgs:
return page_not_found(404)
pageOrgs = []
for org in orgs:
pageOrgs.append(
{'id': org, 'name': self.get_org_name(year, org)})
if orgname != 'all':
totalTasks = len(ORG_TASKS[year][orgname])
else:
totalTasks = 0
for org in ORGS_DATA[year]['orglist']:
if org == 'all':
continue
totalTasks += len(ORG_TASKS[year][org])
userTasks = sorted(
CONTEST_LEADERBOARD[year][orgname].iteritems(),
key=lambda x: x[1]['tasks'],
reverse=True)
return {
"userTasks": userTasks,
"totalTasks": totalTasks,
"pageOrgs": pageOrgs}