-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
197 lines (175 loc) · 6.85 KB
/
db.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
import hashlib
import MySQLdb
import env_config as config
class Db(object):
@staticmethod
def epoch_prefix(epoched):
if epoched:
return 'epoch_'
return ''
def __init__(self):
self.host = config.db_host
self.username = config.db_username
self.password = config.db_password
self.database = config.db_database
self.port = config.db_port
def connect(self):
self.cxn = MySQLdb.connect(
host=self.host,
user=self.username,
passwd=self.password,
db=self.database,
port=self.port,
charset='utf8')
def count_sightings(self, cookie, signature):
c = self.cxn.cursor()
c.execute("""SELECT COUNT(cookie_id) FROM cookies
WHERE cookie_id=%s AND signature=%s""", (str(cookie), signature))
return c.fetchone()[0]
def record_sighting(self, cookie, signature, ip, google_style_ip):
c = self.cxn.cursor()
c.execute("""INSERT INTO cookies SET
cookie_id=%s,
signature=%s,
ip=%s,
ip34=%s""", (
str(cookie),
signature,
ip,
google_style_ip
)
)
self.cxn.commit()
def record_fingerprint(self, whorls):
c = self.cxn.cursor()
exec_str = "INSERT INTO fingerprint SET "
exec_str += ", ".join(map(lambda x: x + "=%s", whorls.keys()))
exec_str += " ON DUPLICATE KEY UPDATE count=count + 1"
c.execute(exec_str, tuple(whorls.values()))
c.execute(
"INSERT INTO fingerprint_times SET fingerprint_id=%s", (c.lastrowid,))
self.cxn.commit()
def update_totals(self, whorls):
c = self.cxn.cursor()
update_str = """INSERT INTO totals SET
total=1, epoch_total=1, variable=%s, value=%s
ON DUPLICATE KEY UPDATE total=total+1, epoch_total=epoch_total+1"""
c.execute(update_str, ('count', ''))
for i in whorls:
c.execute(update_str, (i, whorls[i]))
self.cxn.commit()
def get_whorl_value_count(self, variable, value, epoched):
c = self.cxn.cursor()
c.execute(
"SELECT " + self.epoch_prefix(epoched) + "total FROM totals WHERE variable=%s AND value=%s", (variable, value))
return c.fetchone()[0]
def get_total_count(self, epoched):
c = self.cxn.cursor()
c.execute("SELECT " + self.epoch_prefix(epoched) +
"total FROM totals WHERE variable='count'")
return c.fetchone()[0]
def get_signature_matches_count(self, signature, epoched):
c = self.cxn.cursor()
c.execute(
"SELECT " + self.epoch_prefix(epoched) + "total FROM totals WHERE variable='signature' AND value=%s", (signature, ))
return c.fetchone()[0]
def record_tracking_results(
self,
cookie,
ip,
google_style_ip,
ad_result,
tracker_result,
dnt_result,
known_blockers):
c = self.cxn.cursor()
c.execute("""INSERT INTO `tracking_test` SET
`cookie_id`=%s,
`ip`=%s,
`ip34`=%s,
`block_tracking_ads`=%s,
`block_invisible_trackers`=%s,
`dnt`=%s,
`canvas_fingerprinting`=NULL,
`known_blockers`=%s""", (
str(cookie),
ip,
google_style_ip,
ad_result,
tracker_result,
dnt_result,
known_blockers
)
)
self.cxn.commit()
def get_epoch_beginning(self):
c = self.cxn.cursor()
c.execute("""SELECT value
FROM totals
WHERE variable='epoch_beginning'""")
res = c.fetchone()
if res == None:
return None
else:
return res[0]
def _epoch_total_helper(self, c, columns_to_update, columns_to_md5, operation):
if(operation != '+' and operation != '-'):
return
row = c.fetchone()
while row != None:
fingerprint_id = row[0]
count = row[1]
fingerprint_c = self.cxn.cursor()
fingerprint_c.execute(
"SELECT " + ", ".join(columns_to_update) +
" FROM fingerprint WHERE id=%s", (fingerprint_id, ))
fingerprint_row = fingerprint_c.fetchone()
if fingerprint_row != None:
i = 0
totals_c = self.cxn.cursor()
for variable in columns_to_update:
if variable in columns_to_md5:
value = hashlib.md5(fingerprint_row[i]).hexdigest()
else:
value = fingerprint_row[i]
i += 1
totals_c.execute(
"""UPDATE totals SET epoch_total=epoch_total""" + operation + """%s
WHERE variable=%s AND value=%s""", (count, variable, value))
totals_c.execute(
"""UPDATE totals SET epoch_total=epoch_total""" + operation + """%s
WHERE variable='count'""", (count,))
row = c.fetchone()
def epoch_update_totals(self, old_epoch_beginning, new_epoch_beginning, columns_to_update, columns_to_md5):
c = self.cxn.cursor()
if old_epoch_beginning == None:
c.execute(
"INSERT INTO totals SET value=%s, variable='epoch_beginning'",
(new_epoch_beginning, ))
else:
c.execute("""SELECT fingerprint_id, count(id)
FROM fingerprint_times
WHERE timestamp BETWEEN %s AND %s GROUP BY fingerprint_id""",
(old_epoch_beginning, new_epoch_beginning))
self._epoch_total_helper(c, columns_to_update, columns_to_md5, '-')
c.execute(
"""UPDATE totals SET value=%s
WHERE variable='epoch_beginning'""", (new_epoch_beginning, ))
self.cxn.commit()
def epoch_calculate_totals(self, old_epoch_beginning, new_epoch_beginning, columns_to_update, columns_to_md5):
c = self.cxn.cursor()
if old_epoch_beginning == None:
c.execute(
"INSERT INTO totals SET value=%s, variable='epoch_beginning'",
(new_epoch_beginning, ))
else:
c.execute(
"""UPDATE totals SET value=%s
WHERE variable='epoch_beginning'""", (new_epoch_beginning, ))
c.execute("UPDATE totals SET epoch_total=0 WHERE 1")
c.execute("""SELECT fingerprint_id, count(id)
FROM fingerprint_times
WHERE timestamp > %s GROUP BY fingerprint_id""",
(new_epoch_beginning, ))
self._epoch_total_helper(c, columns_to_update, columns_to_md5, '+')
self.cxn.commit()