-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcleanse.c
190 lines (156 loc) · 4.23 KB
/
cleanse.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <unistd.h>
#define BITS ((1LL << 31) * 8)
#define BYTES (BITS / 8 + 1)
#define FOOT .00000274
#define BUCKET (250 * FOOT)
#define MULT 10000000
#define LATRANGE (180LL * MULT)
#define LONRANGE (360LL * MULT)
#define LATSIZE (LATRANGE / 8 + 1)
#define LONSIZE (LONRANGE / 8 + 1)
unsigned long long hash (char *cp) {
unsigned long long loc = 0;
for (; *cp; cp++) {
loc = (37 * loc + *cp) % BITS;
}
return loc;
}
int main(int argc, char **argv) {
char s[2000];
char user[2000];
char date[2000];
char time[2000];
char where[2000];
char url[2000];
char agent[2000] = "";
double lat, lon;
char userloc[2000];
char odate[2000] = "";
char rest[2000];
int use_agent = 0;
long long date_lines = 0;
int i;
while ((i = getopt(argc, argv, "u")) != -1) {
switch (i) {
case 'u':
use_agent = 1;
break;
default:
fprintf(stderr, "Usage: %s [-u]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
unsigned char *field = calloc(BYTES, 1);
unsigned char *lats, *lons;
lats = calloc(LATSIZE, 1);
lons = calloc(LONSIZE, 1);
if (field == NULL || lats == NULL || lons == NULL) {
fprintf(stderr, "Couldn't allocate memory\n");
exit(EXIT_FAILURE);
}
int why_agent = 0, why_personal = 0, why_stripe = 0, why_global = 0, why_ok = 0;
while (fgets(s, 2000, stdin)) {
int iphone = 0;
memset(rest, '\0', 2000);
if (use_agent) {
if (sscanf(s, "%s %s %s %s %s %s %2000c", user, date, time, where, url, agent, rest) != 7) {
fprintf(stderr, "parse error: %s", s);
continue;
}
} else {
if (sscanf(s, "%s %s %s %s %2000c", user, date, time, where, rest) != 5) {
fprintf(stderr, "parse error: %s", s);
continue;
}
}
if (strcmp(date, odate) != 0) {
strcpy(odate, date);
if (date_lines > 50000) {
fprintf(stderr, "%s agent %d, personal %d, stripe %d, global %d, ok %d\n",
date, why_agent, why_personal, why_stripe, why_global, why_ok);
date_lines = 0;
}
}
if (use_agent) {
if (strncmp(agent, "source:Twitter_for_", 19) != 0 &&
strcmp(agent, "source:Instagram") != 0 &&
strcmp(agent, "source:foursquare") != 0 &&
strcmp(agent, "source:Foursquare") != 0 &&
strcmp(agent, "source:Path") != 0 &&
strcmp(agent, "source:Twitter_Web_Client") != 0
) {
why_agent++;
continue;
}
}
if (sscanf(where, "%lf,%lf", &lat, &lon) != 2) {
fprintf(stderr, "parse error: %s\n", where);
continue;
}
if (strcmp(agent, "source:Twitter_for_iPhone") == 0 && strcmp(date, "2011-08-24") > 0) {
iphone = 1;
}
long long rlat, rlon;
long long ilat, ilon;
if (iphone) {
// Unique for this latitude and longitude
ilat = ((long long) ((lat + 90) * MULT)) / 8;
ilon = ((long long) ((lon + 180) * MULT)) / 8;
rlat = ((long long) ((lat + 90) * MULT)) % 8;
rlon = ((long long) ((lon + 180) * MULT)) % 8;
if (ilat < 0 || ilat >= LATSIZE || ilon < 0 || ilon >= LONSIZE) {
fprintf(stderr, "Out of range: %s", s);
continue;
}
int latseen = lats[ilat] & (1 << rlat);
int lonseen = lons[ilon] & (1 << rlon);
if (latseen != 0 || lonseen != 0) {
why_stripe++;
continue;
}
}
// Unique for this user within 250 feet
{
int ilat = floor(lat / BUCKET);
double rat = cos(ilat * BUCKET * M_PI / 180);
int ilon = floor(lon * rat / BUCKET);
sprintf(userloc, "%s:%d,%d", user, ilat, ilon);
}
long long uloc = hash(userloc);
if (field[uloc / 8] & (1 << (uloc % 8))) {
why_personal++;
continue;
}
long long gloc = 0;
if (!iphone) {
// Unique for this location
gloc = hash(where);
if (field[gloc / 8] & (1 << (gloc % 8))) {
why_global++;
continue;
}
}
why_ok++;
if (use_agent) {
printf("%s %s %s %s %s %s %s", user, date, time, where, url, agent, rest);
} else {
printf("%s %s %s %s %s", user, date, time, where, rest);
}
date_lines++;
field[uloc / 8] |= 1 << (uloc % 8);
if (iphone) {
lats[ilat] |= (1 << rlat);
lons[ilon] |= (1 << rlon);
} else {
field[gloc / 8] |= 1 << (gloc % 8);
}
}
fprintf(stderr, "%s agent %d, personal %d, stripe %d, global %d, ok %d\n",
date, why_agent, why_personal, why_stripe, why_global, why_ok);
return 0;
}