/
aodb.go
104 lines (85 loc) · 2.02 KB
/
aodb.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
package otame
import (
"encoding/json"
"fmt"
"io"
)
type AnimeOfflineDatabaseSeason struct {
Season string `json:"season"`
Year *int `json:"year"`
}
type AnimeOfflineDatabaseEntry struct {
Sources []string `json:"sources"`
Title string `json:"title"`
Type string `json:"type"`
Episodes int `json:"episodes"`
Status string `json:"status"`
Season AnimeOfflineDatabaseSeason `json:"animeSeason"`
Picture string `json:"picture"`
Thumbnail string `json:"thumbnail"`
Synonyms []string `json:"synonyms"`
Relations []string `json:"relations"`
Tags []string `json:"tags"`
}
type AnimeOfflineDatabaseDecoder struct {
decoder *json.Decoder
caughtUp bool
}
func NewAnimeOfflineDatabaseDecoder(r io.Reader) *AnimeOfflineDatabaseDecoder {
return &AnimeOfflineDatabaseDecoder{
decoder: json.NewDecoder(r),
caughtUp: false,
}
}
func (a *AnimeOfflineDatabaseDecoder) DecodeAll() (entries []AnimeOfflineDatabaseEntry, err error) {
if !a.caughtUp {
err = a.locateDataArray()
if err != nil {
return
}
}
for a.decoder.More() {
var entry AnimeOfflineDatabaseEntry
err = a.decoder.Decode(&entry)
if err != nil {
return
}
entries = append(entries, entry)
}
return
}
func (a *AnimeOfflineDatabaseDecoder) Next() (entry AnimeOfflineDatabaseEntry, err error) {
if !a.caughtUp {
err = a.locateDataArray()
if err != nil {
return
}
}
if !a.decoder.More() {
err = io.EOF
return
}
err = a.decoder.Decode(&entry)
return
}
func (a *AnimeOfflineDatabaseDecoder) locateDataArray() error {
for {
t, err := a.decoder.Token()
if err != nil {
return err
}
if t == "data" {
break
}
}
// expect a '['
t, err := a.decoder.Token()
if err != nil {
return err
}
if t != json.Delim('[') {
return fmt.Errorf("expected '[' but got '%s'", t)
}
a.caughtUp = true
return nil
}