-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_directory.py
252 lines (209 loc) · 5.74 KB
/
generate_directory.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
# coding: utf-8
from collections import namedtuple
import csv
import random
import sys
#reload(sys)
#sys.setdefaultencoding("utf-8")
INPUT_DATA = 'data.csv'
INPUT_TEMPLATE = 'template.html'
SWAPS_FILE = 'swaps.csv'
BLACKLIST_FILE = 'blacklist.csv'
DataRow = namedtuple(
'DataRow',
[
'ttimestamp',
'email',
'fullname',
'facebook',
'linkedin',
'twitter',
'role',
#'student_type',
'first_time',
'experience',
'affiliation',
'job_title',
'gender',
'country_ori',
'country_resi',
'bio_short',
'bio_long',
'picture',
'secret_power',
'secret_fact',
'offline_hobby',
'github',
'itunes',
'google_play',
'preferred_pl',
'preferred_frontend',
'preferred_backend',
'looking_for',
'tech_interest',
'topics',
'my_idea',
'ideation_talk_yes_no',
'technical_talk_yes_no',
'anything_else',
'degree_pursiung', # new fields start here
'qc_skills',
'qc_interests',
'qc_hackathons_participate',
'qc_language',
'qc_hardware',
'social_good_topic'])
"""
Loads a manual blacklist from 'blacklist.csv' for people who have been having problems with their profile pictures. Reason is that people without images must appear at the end of the website, otherwise it looks bad.
Blacklist file format:
```
Foo Bar # fullname, be careful with collisions
...
```
"""
def load_blacklist():
blacklist = []
try:
with open(BLACKLIST_FILE) as blacklist_fp:
for blacklist_entry in blacklist_fp:
blacklist_entry = blacklist_entry.rstrip("\n\r")
if blacklist_entry:
print("INFO: blacklisting: ", blacklist_entry, file=sys.stderr)
blacklist.append(blacklist_entry)
except IOError as e:
print("INFO: blacklist.csv file not found.", file=sys.stderr)
return blacklist
# Todo: try to minimeze collisions, somehow...
def is_blacklisted(blacklist, person_tuple):
if (person_tuple.email in blacklist):
return True
if (person_tuple.fullname in blacklist):
return True
return False
"""
Loads a CSV file that contains <URL_FROM>,<URL_TO> for people who are having troubles getting the URL right, and the logic in this script deson't solve it
"""
def load_swaps():
swaps = {}
try:
with open(SWAPS_FILE) as swaps_fp:
reader = csv.reader(swaps_fp)
for rawRow in reader:
(src, dst) = rawRow
dst = dst.rstrip("\n\r")
swaps[src] = dst
except IOError as e:
print("INFO: swaps file not found.", file=sys.stderr)
return swaps
def convert_data_row(rawRow):
return DataRow(*rawRow)
def put_no_pictures_at_end(data, blacklist):
result = []
for row in data:
if (not row.picture):
result.append(row)
elif is_blacklisted(blacklist, row):
result.append(row)
else:
result.insert(0, row)
return result
def shuffle_participants(data):
random.shuffle(data)
return data
def mix_women_men(data, blacklist):
women = [row for row in data if row.gender == 'Female']
menAndDefault = [row for row in data if row.gender != 'Female']
i = 0
j = 0
result = []
while (i < len(women) and j < len(menAndDefault)):
if is_blacklisted(blacklist, women[i]):
break
if is_blacklisted(blacklist, menAndDefault[i]):
break
result.append(women[i])
result.append(menAndDefault[j])
i += 1
j += 1
# put any leftover people with pictures
while (i < len(women)):
if is_blacklisted(blacklist, women[i]):
break
result.append(women[i])
i += 1
while (j < len(menAndDefault)):
if is_blacklisted(blacklist, menAndDefault[i]):
break
result.append(menAndDefault[j])
j += 1
# put any leftover people without pictures
while (i < len(women)):
result.append(women[i])
i += 1
while (j < len(menAndDefault)):
result.append(menAndDefault[j])
j += 1
return result
def process_profile_picture(person_tuple, swaps):
profile_picture_url = person_tuple.picture
if (profile_picture_url in swaps):
profile_picture_url = swaps[profile_picture_url]
profile_picture_url = profile_picture_url.replace(' ', '')
if not profile_picture_url.startswith('https://drive.google.com'):
return profile_picture_url
slices = profile_picture_url.split('/')
if len(slices) == 7:
return 'https://drive.google.com/uc?export=view&id=' + slices[5]
elif len(slices) == 4:
return 'https://drive.google.com/uc?export=view&id=' + profile_picture_url.split('=')[1]
else:
print("ERROR %s (%s) with image %s" % (
person_tuple.fullname, person_tuple.email, person_tuple.picture), file=sys.stderr)
return ""
def main():
print("INFO: loading blacklist.", file=sys.stderr)
blacklist = load_blacklist()
print("INFO: loading swaps", file=sys.stderr)
swaps = load_swaps()
print("\n")
print("INFO: loading people.", file=sys.stderr)
users = []
data = []
with open(INPUT_DATA) as csvfile:
reader = csv.reader(csvfile)
next(reader)
for rawRow in reader:
row = convert_data_row(rawRow)
data.append(row)
data = shuffle_participants(data)
data = put_no_pictures_at_end(data, blacklist)
data = mix_women_men(data, blacklist)
for row in data:
profile_picture_url = process_profile_picture(row, swaps)
user = {
'fullname': row.fullname,
'role': row.role,
'affiliation': row.affiliation,
'picture': profile_picture_url,
'country_ori': row.country_ori,
'country_resi': row.country_resi,
'bio': row.bio_short,
'secret_fact': row.secret_fact,
'secret_power': row.secret_power,
'looking_for': row.looking_for,
'tech_interest': row.tech_interest,
'offline_hobby': row.offline_hobby,
'linkedin': row.linkedin,
'github': row.github};
users.append(user)
from jinja2 import Environment, FileSystemLoader
env = Environment(
loader=FileSystemLoader("."),
extensions=['jinja2.ext.with_'])
template = env.get_template(INPUT_TEMPLATE)
html = template.render({'users': users})
print(html)
print("\nCompleted!", file=sys.stderr)
main()