-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecinfo.go
109 lines (95 loc) · 3 KB
/
secinfo.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/infosecstreams/secinfo/streamers"
)
func main() {
active := streamers.StreamerList{}
inactive := streamers.StreamerList{}
// Check environ SECINFO_TEST exists
if os.Getenv("SECINFO_TEST") == "" {
f, err := streamers.OpenCSV("streamers.csv")
if err != nil {
fmt.Printf("Error reading csv: %s\n", err)
os.Exit(1)
}
s, err := streamers.ParseStreamers(f)
if err != nil {
fmt.Println(err)
}
for _, streamer := range s.Streamers {
// Populate the streamer struct with the data SullyGnome has
streamer.GetUID()
streamer.GetStats()
// Append the streamer to the new streamerList
if streamer.ThirtyDayStats > 0 {
active.Streamers = append(active.Streamers, streamer)
} else {
inactive.Streamers = append(inactive.Streamers, streamer)
}
}
} else {
// Read test.json into active struct
f, _ := ioutil.ReadFile("active.json")
_ = json.Unmarshal([]byte(f), &active)
// Read inactive.json into inactive struct
f, _ = ioutil.ReadFile("inactive.json")
_ = json.Unmarshal([]byte(f), &inactive)
}
// Call sort on the active streamer list
active.Sort()
inactive.Sort()
// Write the active struct to active.json if SECINFO_TEST is not set so latest data is available
if os.Getenv("SECINFO_TEST") == "" {
j, _ := json.Marshal(active)
ioutil.WriteFile("active.json", j, 0644)
// write inactive.json
j, _ = json.Marshal(inactive)
ioutil.WriteFile("inactive.json", j, 0644)
}
// Markdown time!
// Read existing index.md into a string
indexMd, _ := ioutil.ReadFile("index.md")
indexStr := string(indexMd)
// Read index.tmpl.md into a string
indexMdTemplate, _ := ioutil.ReadFile("templates/index.tmpl.md")
// Find '---: | --- | :--- | :---' and append each streamer in streamerist using ReturnMarkdownLine()
heading := "---: | --- | :--- | :---\n"
i := strings.Index(string(indexMdTemplate), heading) + len(heading)
// Print line from the i indexMD
newMd := string(indexMdTemplate[:i])
for _, streamer := range active.Streamers {
s, err := streamer.ReturnMarkdownLine(streamer.OnlineNow(indexStr))
if err != nil {
fmt.Println(err)
}
newMd += s
}
newMd += string(indexMdTemplate[i:])
// Write index.md
ioutil.WriteFile("./index.md", []byte(newMd), 0644)
// Clear markdown string
newMd = ""
// Read inactive.tmpl.md into a string
inactiveMD, _ := ioutil.ReadFile("templates/inactive.tmpl.md")
// Fine '--: | --- | :--- | :---' and append each streamer in inactive using ReturnMarkdownLine()
heading = "--: | ---\n"
i = strings.Index(string(inactiveMD), heading) + len(heading)
// Print line to the i indexMD
newMd = string(inactiveMD[:i])
for _, streamer := range inactive.Streamers {
s, err := streamer.ReturnMarkdownLine(false) // Sorry inactive can't be online
if err != nil {
fmt.Println(err)
}
newMd += s
}
// Print line from the i indexMD
newMd += string(inactiveMD[i:])
// Write inactive.md
ioutil.WriteFile("./inactive.md", []byte(newMd), 0644)
}