-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (117 loc) · 2.77 KB
/
main.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
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
package main
import (
"encoding/json"
"fmt"
"os"
"net/http"
"io/ioutil"
"math/rand"
"strconv"
"crypto/tls"
"io"
"log"
"sort"
)
func main() {
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
downloadFiles()
}
genStats()
}
func genStats() {
files, err := ioutil.ReadDir("./tmp")
if err != nil {
log.Fatal(err)
}
stat := make(map[string]map[string]int)
for _, file := range files {
content, err := ioutil.ReadFile("./tmp/" + file.Name())
if err == nil {
var value interface{}
err = json.Unmarshal(content, &value)
if err == nil {
castedValue := value.(map[string]interface{})
apiVersion, ok := castedValue["api"].(string)
if ok {
if _, ok := stat[apiVersion]; !ok {
stat[apiVersion] = make(map[string]int)
}
stat[apiVersion] = flatten(castedValue, stat[apiVersion], "")
}
}
}
}
for key, value := range stat {
keys := make([]string, len(value))
idx := 0
for k := range value {
keys[idx] = k
idx++
}
sort.Strings(keys)
count := value["/api"]
fmt.Println("# --- " + key + " --- " + "(percent: " + strconv.Itoa((count * 100) / len(files)) + "%, total: " + strconv.Itoa(count) + ")")
fmt.Println("| key | count | percent |")
fmt.Println("|:--|:--|--:|")
for _, innerKey := range keys {
percent := (value[innerKey] * 100) / count
fmt.Println("| " + innerKey + " | " + strconv.Itoa(value[innerKey]) + " | " + strconv.Itoa(percent) + "% |")
}
}
}
func flatten(from map[string]interface{}, to map[string]int, prepend string) map[string]int {
for key, value := range from {
obj, isObject := value.(map[string]interface{})
if isObject {
to = flatten(obj, to, prepend + "/" + key)
} else {
to[prepend + "/" + key] = to[prepend + "/" + key] + 1
}
}
return to
}
func isString(i interface{}) bool {
_, ok := i.(string)
return ok
}
func isFloat(i interface{}) bool {
_, ok := i.(float64)
return ok
}
func isBool(i interface{}) bool {
_, ok := i.(bool)
return ok
}
func isArray(i interface{}) bool {
_, ok := i.([]interface{})
return ok
}
func downloadFiles() {
response, err := http.Get("https://spaceapi.fixme.ch/directory.json")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var f interface{}
err = json.Unmarshal(body, &f)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
os.Mkdir("tmp", 0755)
for _, value := range f.(map[string]interface{}) {
spaceResponse, err := client.Get(value.(string))
if err == nil {
out, err := os.Create("tmp/" + strconv.Itoa(rand.Intn(999999)) + ".json")
if err == nil {
io.Copy(out, spaceResponse.Body)
} else {
fmt.Println(err)
}
} else {
fmt.Println(err)
}
}
}