-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
import.go
89 lines (75 loc) · 2.28 KB
/
import.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
package main
import (
"encoding/csv"
"encoding/json"
"log"
"os"
"strings"
)
const jsonOpenTag = "["
const jsonCloseTag = "]"
const jsonSeparator = ","
func readFile(fileName string, separator rune) ([][]string, error) {
var err error
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
//sed -i 's/\\\"/""/g' Portal_Export.csv
//sed -i 's/None\tNone$/None/g' game_log.tsv
// Initialize our csv reader
csvReader := csv.NewReader(file)
csvReader.Comma = separator
csvReader.LazyQuotes = true
// Read the data
if content, err := csvReader.ReadAll(); err == nil {
return content, nil
}
return nil, err
}
func processData(fileContents [][]string, output []json.RawMessage, matcher func([]string) bool) []json.RawMessage {
for _, line := range fileContents {
if matcher(line) {
item := []byte(jsonOpenTag + line[1] + jsonSeparator + line[2] + jsonCloseTag)
_, output = AddToUniqueSortedJSONSet(output, item)
}
}
return output
}
func getVisitsAndCaptures() []byte {
var fileContents [][]string
var err error
portals := make([]json.RawMessage, 0)
if fileContents, err = readFile("dump/Portal_Export.csv", ','); err != nil || len(fileContents) == 0 {
log.Println("Could not parse scraped portal list, ignoring…")
} else {
portals = processData(fileContents[1:], portals, func(line []string) bool {
return true
})
}
if fileContents, err = readFile("dump/game_log.tsv", '\t'); err != nil {
log.Println(err)
return nil
}
capturedPortals := make([]json.RawMessage, 0)
capturedPortals = processData(fileContents, capturedPortals, func(line []string) bool {
return line[3] == "captured portal" && line[4] != "failed"
})
visitedPortals := make([]json.RawMessage, 0)
visitedPortals = processData(fileContents, visitedPortals, func(line []string) bool {
return ((strings.HasPrefix(line[3], "hacked") && strings.HasSuffix(line[3], "portal")) || strings.HasSuffix(line[3], "deployed")) && line[4] != "failed"
})
result := struct {
Portals []json.RawMessage `json:"portals"`
Visits []json.RawMessage `json:"visits"`
Captures []json.RawMessage `json:"captures"`
}{
portals, visitedPortals, capturedPortals,
}
var encodedJSON []byte
if encodedJSON, err = json.Marshal(result); err != nil {
log.Println(err)
return nil
}
return encodedJSON
}