-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (73 loc) · 1.9 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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
// just a 'go run main.go' is all that's needed to update the gods/cards
const (
SmiteGodsCMSEndpoint string = "https://cms.smitegame.com/wp-json/smite-api/all-gods/1"
GodCardFileStr string = "src/img/gods/%s"
GodJSONFileStr string = "src/helpers/gods.json"
)
type God struct {
Name string `json:"name"`
Role string `json:"role"`
Card string `json:"card,omitempty"`
}
func main() {
log.Println("Starting.")
resp, err := http.Get(SmiteGodsCMSEndpoint)
if err != nil {
log.Fatalf("failed to get gods: %v", err)
}
defer resp.Body.Close()
bs, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("failed to read response: %v", err)
}
var gods []*God
err = json.Unmarshal(bs, &gods)
if err != nil {
log.Fatalf("failed to unmarshal response body: %v", err)
}
for _, god := range gods {
cardResp, err := http.Get(strings.ReplaceAll(god.Card, "'", "")) // handle chang'e.jpg
if err != nil {
log.Fatalf("failed to get card %s: %v", god.Card, err)
}
defer cardResp.Body.Close()
fn := strings.ReplaceAll(filepath.Base(god.Card), "'", "") // chang'e.jpg
f, err := os.Create(fmt.Sprintf(GodCardFileStr, fn))
if err != nil {
log.Fatalf("failed to create %s: %v", fn, err)
}
defer f.Close()
_, err = io.Copy(f, cardResp.Body)
if err != nil {
log.Fatalf("failed to copy %s: %v", fn, err)
}
cardResp.Body.Close()
f.Close()
god.Card = fn // set the card to be the filename now, since we won't be requesting it
}
bs, err = json.MarshalIndent(gods, "", " ")
if err != nil {
log.Fatalf("failed to marshal gods: %v", err)
}
f, err := os.Create(GodJSONFileStr)
if err != nil {
log.Fatalf("failed to create gods.json file: %v", err)
}
defer f.Close()
_, err = f.Write(bs)
if err != nil {
log.Fatalf("failed to write gods.json file: %v", err)
}
log.Println("Done.")
}