-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_data.py
70 lines (63 loc) · 2.74 KB
/
import_data.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
from random import SystemRandom
import sqlite3
import csv
import dateparser
import db
def import_meetups(path, dataset_db):
"""
Inserts meetups event details into the db
"""
cursor = dataset_db.cursor()
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader) # Skip headers
for row in csv_reader:
name, date, unknown_attendees = row
print("[import_meetups] Inserting meetup {}".format(name))
db.insert_meetup(cursor, name, date, unknown_attendees)
cursor.close()
dataset_db.commit()
def import_user(anon_db, meetup_user_id):
"""
This function reads Meetup dump information and creates a mapping between a real
user_id and our id. The mapping will be used to keep a _user identity_
without revealing his real data allowing metrics like churn rate to be calculated.
"""
random = SystemRandom()
print("[import_user] Procesing {}".format(meetup_user_id))
ethereumba_meetup_id = db.get_user_mapping(anon_db, meetup_user_id)
if ethereumba_meetup_id is None:
new_ethereumba_meetup_id = random.randint(0, 2**32)
print("[import_user] User not found. Assigning {} to {}".format(
new_ethereumba_meetup_id, meetup_user_id))
db.insert_userid_mapping(
anon_db, meetup_user_id, new_ethereumba_meetup_id)
return new_ethereumba_meetup_id
else:
print("[import_user] User found")
return ethereumba_meetup_id[0]
def import_meetup_data(path, dataset_db, anon_db):
"""
Imports Meetup dump into the dataset using anonymized names
"""
cursor = dataset_db.cursor()
with open(path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader) # Skip headers
for row in csv_reader:
meetup_name = row[0]
meetup_user_id = row[2]
rsvp_date = dateparser.parse(row[7]) if row[7] != '' else None
attended = row[10] == 'x'
print("[import_meetup_data] Processing row {}".format(row))
ethereumba_user_id = import_user(anon_db, meetup_user_id)
print("[import_meetup_data] ethereumba user found {}".format(
ethereumba_user_id))
meetup_id = db.look_for_meetup(cursor, meetup_name)
if meetup_id is not None and ethereumba_user_id is not None:
print("[import_meetup_data] Inserting meetup_id: {}, ethereumba_user_id: {}, rsvp: {}, attended: {}".format(
meetup_id, ethereumba_user_id, rsvp_date, attended))
db.insert_meetup_data(
cursor, meetup_id[0], ethereumba_user_id, rsvp_date, attended)
cursor.close()
dataset_db.commit()