-
Notifications
You must be signed in to change notification settings - Fork 86
/
step2_cleanup_users.py
executable file
·278 lines (221 loc) · 7.48 KB
/
step2_cleanup_users.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
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 nu
import json
import logging
from africa_data import countries
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG)
hdlr = logging.FileHandler('cleaned_up.log')
fmt = logging.Formatter('%(message)s')
hdlr.setFormatter(fmt)
logger.addHandler(hdlr)
UNKNOWN = u"Unknown"
DEBUG = False
all_users = json.load(open('step1.json'))
bads = []
def country_city_by_location(location):
ll = location.lower()
country_matches = []
city_matches = []
for ccode, cc in countries.items():
for ct in cc.get('patterns'):
for ct_name in ct.get('patterns', [ct.get('name')]):
if ct_name.lower() in location.lower():
city_matches.append((ccode, ct.get('name')))
for cc_name in cc.get('names', [ct.get('name')]):
if cc_name.lower() in location.lower():
country_matches.append(ccode)
city_matches = list(set(city_matches))
country_matches = list(set(country_matches))
err = (None, None)
# HACKS
if "benin" in ll and "city" in ll:
return 'NG', "Benin City"
if "cape town" in ll:
if not "Capte Town" in [x[1] for x in city_matches]:
city_matches.append(('ZA', "Cape Town"))
if 'africa' in ll and not 'south' in ll:
while True:
try:
country_matches.remove('ZA')
except ValueError:
break
if ('addis' in ll and 'ababa' in ll
) or 'rabat' in ll:
while True:
try:
city_matches.remove(('NG', 'Aba'))
except ValueError:
break
if 'kampala' in ll or 'uganda' in ll:
while True:
try:
city_matches.remove(('TD', 'Pala'))
except ValueError:
break
if 'nigeria' in ll:
while True:
try:
country_matches.remove('NE')
except ValueError:
break
# Saint Louis in Senegal and Missouri/USA
if 'saint' in ll and 'louis' in ll \
and (not 'sn' in ll or not 'senegal' in ll or not 'sénégal' in ll):
return err
# Man vs Isle of Man
if 'isle of man' in ll:
return err
if 'juba' in ll:
while True:
try:
country_matches.remove('SD')
except ValueError:
break
# Joar Sahara in Bangladesh
if 'sahara' in ll and 'joar' in ll:
return err
if 'verde' in ll and (not 'cape' in ll and not 'cabo' in ll):
return err
# Praia Grande, Brazil
if 'praia' in ll and 'grande' in ll:
return err
# Tripoli in Lebanon
if 'tripoli' in ll and 'lebanon' in ll:
return err
# Lagos, Chile
if 'lagos' in ll and 'chile' in ll:
return err
if 'lagos' in ll and 'juan' in ll:
return err
if 'lagos' in ll and ('pt' in ll or 'portugal' in ll or 'island' in ll):
return err
if 'drc' in ll and 'madrid' in ll:
return err
# Monrovia, CA
if 'monrovia' in ll and ('ca' in ll or 'california' in ll or 'us' in ll):
return err
# Saint Maurice
if 'maurice' in ll and 'saint' in ll:
return err
# more than ile maurice
if 'maurice' in ll and len(ll) > 13:
return err
# Mali / Somalia
if 'somali' in ll:
while True:
try:
country_matches.remove('ML')
except ValueError:
break
# Cape Elisabeth in Maine
if 'elizabeth' in ll and ('cape' in ll or 'maine' in ll \
or 'nj' in ll or 'nc' in ll or 'co' in ll):
return err
# Alexandria, Virginia
if 'alexandria' in ll and ('virginia' in ll or 'va' in ll):
return err
# DR Congo and RD Congo
if 'congo' in ll and ('rd' in ll or 'dr' in ll):
while True:
try:
country_matches.remove('CG')
except ValueError:
break
country_matches.append('CD')
# basically, we found a single match on city
if len(city_matches) == 1:
if len(country_matches) == 0 \
or (len(country_matches) == 1
and country_matches[-1] == city_matches[-1][0]):
return city_matches[-1]
elif city_matches[-1][0] in country_matches:
return city_matches[-1]
# single match on country
elif len(city_matches) == 0 and len(country_matches) == 1:
return country_matches[-1], None
# multiple locations
elif len(city_matches) > 1 or len(country_matches) > 1:
# pick first city that matches a country
if len(city_matches):
for cc, ct in city_matches:
if cc in country_matches:
return cc, ct
# pick first country
if len(country_matches):
return country_matches[0], None
# pick what's left: a city
return city_matches[0]
if DEBUG:
print(len(city_matches))
print(len(country_matches))
from pprint import pprint as pp ; pp(city_matches)
from pprint import pprint as pp ; pp(country_matches)
country_code = None
city_name = None
return country_code, city_name
def guess_location(user):
country_code, city_name = country_city_by_location(user.get('location'))
if country_code is None:
bads.append(user)
return None
if city_name is not None:
latitude, longitude = [(x.get('latitude'), x.get('longitude'))
for x in countries.get(country_code).get('patterns')
if city_name in x.get('patterns', []) + [x.get('name')]][0]
else:
latitude = countries.get(country_code, {}).get('latitude')
longitude = countries.get(country_code, {}).get('longitude')
try:
country_name = countries.get(country_code).get('name')
except AttributeError:
return None
del(user['country'])
del(user['city'])
user.update({'country_code': country_code,
'country_name': country_name,
'city_name': city_name,
'has_city': city_name is not None,
'latitude': latitude,
'longitude': longitude,
'id_int': int(user.get('id').split('-', 1)[1])})
return user
# DEBUG = True
if DEBUG:
from pprint import pprint as pp ; pp(country_city_by_location(u"San Juan de los Lagos"))
exit(0)
failed = []
valid_users = []
existing = []
unique_users = []
for user in all_users:
new_user = guess_location(user)
if new_user is None:
continue
userdesc = (u"%(country)s/%(city)s: @%(user)s: %(location)s"
% {'country': new_user.get('country_name'),
'city': new_user.get('city_name'),
'user': new_user.get('username'),
'location': new_user.get('location')})
if new_user.get('country_name') == UNKNOWN:
failed.append(userdesc)
else:
valid_users.append(new_user)
logger.info(userdesc)
for fail in failed:
logger.info(fail)
logger.info("%d failed users" % len(failed))
logger.info("%d success users" % len(all_users))
for bad in bads:
while True:
try:
all_users.remove(bad)
except ValueError:
break
for user in valid_users:
if user['username'] in existing:
continue
unique_users.append(user)
existing.append(user['username'])
json.dump(unique_users, open('step2.json', 'w'), indent=4)