/
types.go
207 lines (184 loc) · 3.76 KB
/
types.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
package setting
import (
"fmt"
"io"
"regexp"
"time"
"github.com/capric98/t-rss/unit"
"gopkg.in/yaml.v2"
)
// Int64 is int64
type Int64 struct {
I int64
}
// Duration wraps time.Duration
type Duration struct {
T time.Duration
}
// C :)
type C struct {
Global Global `yaml:"GLOBAL"`
Tasks map[string]*Task `yaml:"TASKS"`
}
// Global is global configs.
type Global struct {
LogFile string `yaml:"log_file"`
History struct {
MaxNum int `yaml:"max_num"`
Save string `yaml:"save_to"`
} `yaml:"history"`
Timeout Duration `yaml:"timeout"`
}
// Task is task part.
type Task struct {
Rss *Rss `yaml:"rss"`
Filter Filter `yaml:"filter"`
Quota Quota `yaml:"quota"`
Edit *Edit `yaml:"edit"`
Receiver Receiver `yaml:"receiver"`
}
// Rss :)
type Rss struct {
URL string `yaml:"url"`
Method string `yaml:"method"`
Headers Header `yaml:"headers"`
Interval Duration `yaml:"interval"`
}
// Filter :)
type Filter struct {
ContentSize ContentSize `yaml:"content_size"`
Regexp RegexpConfig `yaml:"regexp"`
}
// Quota :)
type Quota struct {
Num int `yaml:"num"`
Size Int64 `yaml:"size"`
}
// Edit :)
type Edit struct {
Tracker Tracker `yaml:"tracker"`
}
// Receiver defines tasks' receiver(s).
type Receiver struct {
Delay Duration `yaml:"delay"`
Save *string `yaml:"save_path"`
Client map[string]map[string]interface{} `yaml:"client"`
}
// ContentSize :)
type ContentSize struct {
Min Int64 `yaml:"min"`
Max Int64 `yaml:"max"`
}
// RegexpConfig :)
type RegexpConfig struct {
Accept []Reg `yaml:"accept"`
Reject []Reg `yaml:"reject"`
}
// Reg :)
type Reg struct {
R *regexp.Regexp
C string
}
// Tracker :)
type Tracker struct {
Delete []Reg `yaml:"delete"`
Add []string `yaml:"add"`
}
// Header :)
type Header struct {
H map[string][]string
}
// Parse :)
func Parse(r io.Reader) (config *C, e error) {
config = new(C)
e = yaml.NewDecoder(r).Decode(config)
if e != nil {
return
}
config.standardize()
return
}
func (c *C) standardize() {
if c.Global.Timeout.T == 0 {
c.Global.Timeout.T = 30 * time.Second
}
if c.Global.History.MaxNum == 0 {
c.Global.History.MaxNum = 500
}
if c.Global.History.Save == "" {
c.Global.History.Save = ".t-rss_History/"
}
if l := len(c.Global.History.Save); l > 0 && c.Global.History.Save[l-1] != '/' {
c.Global.History.Save = c.Global.History.Save + "/"
}
for _, v := range c.Tasks {
if v.Rss.Method == "" {
v.Rss.Method = "GET"
}
if v.Rss.Interval.T == 0 {
v.Rss.Interval.T = 30 * time.Second
}
if v.Filter.ContentSize.Max.I == 0 {
v.Filter.ContentSize.Max.I = 1 << 62
}
if v.Quota.Num == 0 {
v.Quota.Num = 1 << 30
}
if v.Quota.Size.I == 0 {
v.Quota.Size.I = 1 << 62
}
}
}
// UnmarshalYAML :)
func (t *Duration) UnmarshalYAML(uf func(interface{}) error) (e error) {
var s string
e = uf(&s)
if e != nil {
return
}
t.T = unit.ParseDuration(s)
return nil
}
// UnmarshalYAML :)
func (r *Reg) UnmarshalYAML(uf func(interface{}) error) (e error) {
var s string
e = uf(&s)
if e != nil {
return
}
r.C = s
r.R, e = regexp.Compile(s)
return
}
// UnmarshalYAML :)
func (n *Int64) UnmarshalYAML(uf func(interface{}) error) (e error) {
var s string
e = uf(&s)
if e != nil {
return
}
n.I = unit.ParseSize(s)
e = nil
return
}
// UnmarshalYAML :)
func (h *Header) UnmarshalYAML(uf func(interface{}) error) (e error) {
if h.H == nil {
h.H = make(map[string][]string)
}
var header map[string]interface{}
e = uf(&header)
for k, v := range header {
switch vi := v.(type) {
case string:
h.H[k] = append(h.H[k], vi)
case []string:
for i := range vi {
h.H[k] = append(h.H[k], vi[i])
}
default:
h.H[k] = append(h.H[k], fmt.Sprintf("%v", vi))
}
}
return
}