-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtgjson.go
211 lines (186 loc) · 3.85 KB
/
mtgjson.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package mtgjson
import (
"compress/gzip"
"crypto/rand"
"encoding/base64"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
func init() {
gob.Register(AllPrintings{})
}
type allPrintings struct {
// Meta Meta `json:"meta"`
Data AllPrintings `json:"data"`
}
func DownloadAllPrintings(w io.Writer) error {
res, err := http.Get("https://mtgjson.com/api/v5/AllPrintings.json.gz")
if err != nil {
return err
}
defer res.Body.Close()
r, err := gzip.NewReader(res.Body)
if err != nil {
return err
}
if _, err = io.Copy(w, r); err != nil {
return err
}
return r.Close()
}
func ReadAllPrintingsJSON(r io.Reader) (AllPrintings, error) {
j := json.NewDecoder(r)
d := allPrintings{}
if err := j.Decode(&d); err != nil {
return nil, err
}
return d.Data, nil
}
func ReadAllPrintingsGOB(r io.Reader) (AllPrintings, error) {
dec := gob.NewDecoder(r)
d := AllPrintings{}
if err := dec.Decode(&d); err != nil {
return nil, err
}
return d, nil
}
func WriteAllPrintingsGOB(w io.Writer, p AllPrintings) error {
enc := gob.NewEncoder(w)
return enc.Encode(p)
}
func WriteCardsGOB(dir string, cards []Card) error {
w := 8
ch := make(chan Card, w)
errs := make(chan error, w)
for i := 0; i < w; i++ {
go func() {
for c := range ch {
err := WriteCardGOB(dir, c)
errs <- err
}
}()
}
go func() {
for _, c := range cards {
ch <- c
}
}()
var gerr error
for range cards {
err := <-errs
if err != nil {
gerr = err
}
}
return gerr
}
type noopWriteCloser struct{ w io.Writer }
type noopReadCloser struct{ r io.Reader }
func (n *noopWriteCloser) Write(b []byte) (int, error) { return n.w.Write(b) }
func (n *noopWriteCloser) Close() error { return nil }
func (n *noopReadCloser) Read(b []byte) (int, error) { return n.r.Read(b) }
func (n *noopReadCloser) Close() error { return nil }
const compress = false
func compressWriter(w io.Writer) io.WriteCloser {
if compress {
return gzip.NewWriter(w)
}
return &noopWriteCloser{w}
}
func compressReader(r io.Reader) (io.ReadCloser, error) {
if compress {
return gzip.NewReader(r)
}
return &noopReadCloser{r}, nil
}
func WriteCardGOB(dir string, card Card) error {
dir, fp, err := uuidFP(dir, string(card.UUID))
if err != nil {
return err
}
_ = os.MkdirAll(dir, 0700)
tmp := tmpFile(fp)
f, err := os.Create(tmp)
if err != nil {
return err
}
gz := compressWriter(f)
enc := gob.NewEncoder(gz)
encErr := enc.Encode(card)
if err := gz.Close(); err != nil {
f.Close()
os.Remove(tmp)
return err
}
f.Close()
if encErr != nil {
os.Remove(tmp)
return err
}
return os.Rename(tmp, fp)
}
func ReadCardsGOB(dir string, cb func(Card)) error {
g, err := filepath.Glob(filepath.Join(dir, "*", "*", "*"))
if err != nil {
return err
}
for _, fp := range g {
uuid := filepath.Base(fp)
c, err := ReadCardGOB(dir, UUID(uuid))
if err != nil {
return err
}
cb(c)
}
return nil
}
func ReadCardGOB(dir string, uuid UUID) (Card, error) {
_, fp, err := uuidFP(dir, string(uuid))
var fc Card
if err != nil {
return fc, err
}
f, err := os.Open(fp)
if err != nil {
return fc, err
}
gz, err := compressReader(f)
if err != nil {
f.Close()
return fc, err
}
dec := gob.NewDecoder(gz)
err = dec.Decode(&fc)
f.Close()
return fc, err
}
func uuidFP(dir string, uuid string) (string, string, error) {
if len(uuid) != 36 {
return "", "", errors.New("invalid uuid")
}
d1, d2 := uuid[0:2], uuid[2:4]
rdir := filepath.Join(dir, d1, d2)
return rdir, filepath.Join(rdir, uuid), nil
}
func tmpFile(file string) string {
stamp := strconv.FormatInt(time.Now().UnixNano(), 36)
rnd := make([]byte, 32)
_, err := io.ReadFull(rand.Reader, rnd)
if err != nil {
panic(err)
}
return fmt.Sprintf(
"%s.%s-%s.tmp",
file,
stamp,
base64.RawURLEncoding.EncodeToString(rnd),
)
}