-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
111 lines (96 loc) · 2.4 KB
/
common.go
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"strings"
// "log"
)
const filePath string = "./data/users.txt"
func SlowSearch(out io.Writer) {
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
r := regexp.MustCompile("@")
seenBrowsers := []string{}
uniqueBrowsers := 0
foundUsers := ""
lines := strings.Split(string(fileContents), "\n")
users := make([]map[string]interface{}, 0)
for _, line := range lines {
user := make(map[string]interface{})
// fmt.Printf("%v %v\n", err, line)
err := json.Unmarshal([]byte(line), &user)
if err != nil {
panic(err)
}
users = append(users, user)
}
for i, user := range users {
isAndroid := false
isMSIE := false
browsers, ok := user["browsers"].([]interface{})
if !ok {
// log.Println("cant cast browsers")
continue
}
for _, browserRaw := range browsers {
browser, ok := browserRaw.(string)
if !ok {
// log.Println("cant cast browser to string")
continue
}
if ok, err := regexp.MatchString("Android", browser); ok && err == nil {
isAndroid = true
notSeenBefore := true
for _, item := range seenBrowsers {
if item == browser {
notSeenBefore = false
}
}
if notSeenBefore {
// log.Printf("SLOW New browser: %s, first seen: %s", browser, user["name"])
seenBrowsers = append(seenBrowsers, browser)
uniqueBrowsers++
}
}
}
for _, browserRaw := range browsers {
browser, ok := browserRaw.(string)
if !ok {
// log.Println("cant cast browser to string")
continue
}
if ok, err := regexp.MatchString("MSIE", browser); ok && err == nil {
isMSIE = true
notSeenBefore := true
for _, item := range seenBrowsers {
if item == browser {
notSeenBefore = false
}
}
if notSeenBefore {
// log.Printf("SLOW New browser: %s, first seen: %s", browser, user["name"])
seenBrowsers = append(seenBrowsers, browser)
uniqueBrowsers++
}
}
}
if !(isAndroid && isMSIE) {
continue
}
// log.Println("Android and MSIE user:", user["name"], user["email"])
email := r.ReplaceAllString(user["email"].(string), " [at] ")
foundUsers += fmt.Sprintf("[%d] %s <%s>\n", i, user["name"], email)
}
fmt.Fprintln(out, "found users:\n"+foundUsers)
fmt.Fprintln(out, "Total unique browsers", len(seenBrowsers))
}