-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
114 lines (93 loc) · 2.69 KB
/
data.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
package legionhq
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// RootDataURL is the base URL for all data files
const RootDataURL = "https://raw.githubusercontent.com/NicholasCBrown/legion-hq-web/master/src/constants"
// CardDataJS is the JavaScript file that defines the card data for LegionHQ
const CardDataJS = "cards.js"
// KeywordDataJS is the JavaScript file that defines the keyword data for LegionHQ
const KeywordDataJS = "keywords.js"
// GetData retrieves all data from files in the master branch
func GetData() (Data, error) {
data := Data{
AllCards: map[string]Card{},
KeywordDict: map[string]string{},
CommunityLinks: map[string][]Link{},
}
// cards
cardsJSON, err := execCardsJS()
if err != nil {
return data, err
}
var cards map[string]interface{}
if err := json.Unmarshal([]byte(cardsJSON), &cards); err != nil {
return data, fmt.Errorf("could not unmarshal cards: %v", err)
}
for key := range cards {
cardMap := cards[key]
cardBytes, _ := json.Marshal(&cardMap)
card := jsonToCard(string(cardBytes))
data.AllCards[card.ID] = card
}
// keywords
keywordsJSON, err := execKeywordsJS()
if err != nil {
return data, err
}
var keywords map[string]string
if err := json.Unmarshal([]byte(keywordsJSON), &keywords); err != nil {
return data, fmt.Errorf("could not unmarshal keywords: %v", err)
}
data.KeywordDict = keywords
return data, nil
}
func getScript(script string) ([]byte, error) {
url := fmt.Sprintf("%s/%s", RootDataURL, script)
client := http.Client{}
resp, err := client.Get(url)
if err != nil {
return []byte{}, err
}
if resp.StatusCode != 200 {
return []byte{}, fmt.Errorf("non-Ok response code %s (%d)", resp.Status, resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
return body, err
}
func execCardsJS() (string, error) {
var data string
scriptBytes, err := getScript(CardDataJS)
if err != nil {
return data, err
}
data = string(scriptBytes)
data = strings.ReplaceAll(data, "const cards = JSON.parse(JSON.stringify(", "")
data = strings.ReplaceAll(data, "));", "")
data = strings.ReplaceAll(data, "export default cards;", "")
data = strings.TrimSpace(data)
return data, nil
}
func execKeywordsJS() (string, error) {
var data string
scriptBytes, err := getScript(KeywordDataJS)
if err != nil {
return data, err
}
data = string(scriptBytes)
data = strings.ReplaceAll(data, "const keywords = ", "")
data = strings.ReplaceAll(data, "};", "}")
data = strings.ReplaceAll(data, "export default keywords;", "")
data = strings.TrimSpace(data)
return data, nil
}
func jsonToCard(cardJSON string) Card {
var card Card
cardBytes := []byte(cardJSON)
_ = json.Unmarshal(cardBytes, &card)
return card
}