-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_user_stats.py
304 lines (234 loc) · 10.9 KB
/
calc_user_stats.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import json
import httplib, urllib
import dateutil.parser
import pymongo
from operator import itemgetter
import os
import datetime, pytz
import json
import locale
locale.setlocale(locale.LC_ALL, ('en_US', 'utf-8'))
import sys
sys.path.insert(0, './Data Requests')
sys.path.insert(0, './Phone Number Lookup')
from get_class_data import returnClassData
from analyze_session import returnInsights
from phone_lookup_whitepages import returnDataForNumber
def get_pacific_time(time_str):
time_datetime = dateutil.parser.parse(time_str)
time_localized = pytz.utc.localize(time_datetime)
return time_localized.astimezone(pytz.timezone('US/Pacific'))
# open/read config file
old_time = ""
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, "last_updated.txt")
if os.path.isfile(file_path):
with open (file_path, "r") as config_file:
old_time = config_file.read().replace('\n', '')
print "Old -- Pacific: %s UTC: %s" % (get_pacific_time(old_time), old_time)
# record/print new last_updated time
new_time = datetime.datetime.utcnow().isoformat()
print "New -- Pacific: %s UTC: %s" % (get_pacific_time(new_time), new_time)
# get data from Parse
user_data = returnClassData("_User", sort_by="createdAt", last_updated=old_time, new_updated=new_time)
link_data = returnClassData("Link", last_updated=old_time, new_updated=new_time)
logs_data = returnClassData("Logs", last_updated=old_time, new_updated=new_time)
# exit if no data returned
if user_data is None or link_data is None or logs_data is None:
sys.exit("Error getting Parse data from module")
# print "New user data: %s \n" % user_data
# print "New link data: %s \n" % link_data
# print "New logs data: %s \n" % logs_data
# initialization
user_stats_list = []
print "Number of new/updated users: %u" % len(user_data)
# initialize mongo connection
connection = pymongo.MongoClient('localhost', 27017)
db = connection.test
# create users collection
users = db.users
# create user_stats_list
for user in user_data:
user_objectId = user.get('objectId', None)
# name else username
user_name = user.get('name', None)
if user_name is None:
user_name = user.get('username', None)
user_username = user.get('username', None)
user_createdAt = user.get('createdAt', None)
user_mobile_number = user.get('mobile_number', None)
user_stats = {
"objectId": user_objectId,
"createdAt": user_createdAt,
"username": user_username,
"mobile_number": user_mobile_number,
"links_sent": 0,
"friends": 0,
"sessions": 0,
"notifications": None,
"device_model": None,
"whitepages_data": None,
"city": None,
"state": None,
"country": None,
"latitude": None,
"longitude": None,
"verified": (user.get('mobile_number', None) is not None)
}
# special case 1: v1.* users who signed up with Facebook
if (user.get('mobileVerified', None) is None and user.get('facebook_id', None) is not None):
user_stats["verified"] = None
# special case 2: v1.* users who created account with LinkMeUp
if (user.get('mobileVerified', None) is None and user.get('mobile_number', None) is not None):
user_stats["verified"] = None
# reverse phone number look up
if user_stats["mobile_number"] is not None:
file_path = os.path.join(script_dir, ("Whitepages Data/%s_phone_data" % user_username))
# data for user already exists
if os.path.isfile(file_path):
with open(file_path, 'r') as infile:
result = json.loads(infile.read())
else: # look up phone number and save result to file
print user_stats["mobile_number"]
if user_stats["mobile_number"][:1] == "+":
result = returnDataForNumber(user_stats["mobile_number"][1:])
else:
result = returnDataForNumber(user_stats["mobile_number"])
with open(file_path, 'w') as outfile:
json.dump(result, outfile)
# process result
best_location = {}
if 'current_addresses' in result: # api v3
print user_stats["username"], result
if not result['id']:
continue
best_location = result['current_addresses'][0]
elif 'results' in result: # api v2
entry = result['results'][0]
if entry.get("best_location", None):
best_location = entry["best_location"]
elif entry.get("associated_locations", None):
best_location = entry["associated_locations"][0]
else:
continue
else:
continue
if best_location is not None:
city = best_location["city"]
state = best_location["state_code"]
country = best_location["country_code"]
latitude = best_location["lat_long"]["latitude"]
longitude = best_location["lat_long"]["longitude"]
user_stats["city"] = city
user_stats["state"] = state
user_stats["country"] = country
user_stats["latitude"] = latitude
user_stats["longitude"] = longitude
print "User: %s Location: %s, %s, %s" % (user_username, city, state, country)
# add to user_stats_list
user_stats_list.append(user_stats)
# link data
for link in link_data:
link_createdAt = link.get('createdAt', None)
sender_pointer = link.get('sender', None)
sender_id = None
if sender_pointer is not None:
sender_id = sender_pointer.get('objectId', None)
# number of links sent
if any(user_stats['objectId'] == sender_id for user_stats in user_stats_list):
index = map(itemgetter('objectId'), user_stats_list).index(sender_id)
# increment links_sent, if haven't counted this link before
if (not old_time or link_createdAt >= old_time):
user_stats_list[index]["links_sent"] += 1
# log data
for log in logs_data:
log_createdAt = log.get('createdAt', None)
user_pointer = log.get('user', None)
user_id = None
if user_pointer is not None:
user_id = user_pointer.get('objectId', None)
# users_stats corresponding to this objectId
results_list = filter(lambda user_stats: user_stats['objectId'] == user_id, user_stats_list)
user_stats = results_list[0] if results_list else None
if user_stats is not None:
# increment sessions, if haven't counted this session before
if (not old_time or log_createdAt >= old_time):
user_stats["sessions"] += 1
# log insights
insights = returnInsights(log)
# print insights
if user_stats["notifications"] is None:
user_stats["notifications"] = insights["notifications"]
if user_stats["device_model"] is None:
user_stats["device_model"] = insights["device_model"]
# sort user_stats_list
# user_stats_list_links = sorted(user_stats_list, key=itemgetter('links_sent'), reverse=True)
# user_stats_list_sessions = sorted(user_stats_list, key=itemgetter('sessions'), reverse=True)
# write new_time to config file
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, "last_updated.txt")
with open (file_path, "w") as config_file:
config_file.write(new_time)
config_file.truncate()
# insert into/update mongo database
for user_stats in user_stats_list:
user_id = user_stats.get('objectId', None)
user_username = user_stats.get('username', None)
user_createdAt = user_stats.get('createdAt', None)
user_links_sent = user_stats.get('links_sent', None)
user_sessions = user_stats.get('sessions', None)
user_verified = user_stats.get('verified', None)
user_notifications = user_stats.get('notifications', None)
user_device_model = user_stats.get('device_model', None)
user_city = user_stats.get('city', None)
user_state = user_stats.get('state', None)
user_country = user_stats.get('country', None)
user_latitude = user_stats.get('latitude', None)
user_longitude = user_stats.get('longitude', None)
user = db.users.find_one({"_id": user_id})
# update, if already exists in DB
if user is not None:
db.users.update_one({"_id": user_id},
{"$set": {"verified": user_verified,
"city": user_city,
"state": user_state,
"country": user_country,
"latitude": user_latitude,
"longitude": user_longitude,
"links_sent": user["links_sent"] + user_links_sent,
"sessions": user["sessions"] + user_sessions,
"notifications": (user_notifications if user_notifications is not None else user["notifications"]),
"device_model": (user_device_model if user_device_model is not None else user["device_model"])
}})
# insert into DB
else:
db.users.insert_one({"_id": user_id,
"username": user_username,
"createdAt": user_createdAt,
"verified": user_verified,
"city": user_city,
"state": user_state,
"country": user_country,
"latitude": user_latitude,
"longitude": user_longitude,
"links_sent": user_links_sent,
"sessions": user_sessions,
"notifications": user_notifications,
"device_model": user_device_model})
# print list
for user_stats in user_stats_list:
user_id = user_stats.get('objectId', None)
user_username = user_stats.get('username', None)
user_createdAt = user_stats.get('createdAt', None)
user_links_sent = user_stats.get('links_sent', None)
user_sessions = user_stats.get('sessions', None)
user_verified = user_stats.get('verified', None)
user_notifications = user_stats.get('notifications', None)
user_device_model = user_stats.get('device_model', None)
user_city = user_stats.get('city', None)
user_state = user_stats.get('state', None)
user_country = user_stats.get('country', None)
user_latitude = user_stats.get('latitude', None)
user_longitude = user_stats.get('longitude', None)
user_location_str = "%s, %s, %s" % (user_city[:12] if user_city is not None else user_city, user_state, user_country)
print "%-20s %-12s Links sent: %-4u Sessions: %-4u Verified: %-7s Notifs: %-7s Device: %-11s Location: %-22s" % (user_username[:18], user_id, user_links_sent, user_sessions, user_verified, user_notifications, user_device_model, user_location_str)