imitater/config.go

97 lines
1.8 KiB
Go
Raw Normal View History

2018-11-26 01:30:02 +00:00
package imitate
import (
"io/ioutil"
"log"
"os"
"strings"
yaml "gopkg.in/yaml.v2"
)
// Config 任务加载的默认配置
type Config struct {
Session int `yaml:"session"`
Mode int `yaml:"mode"`
Proxies []string `yaml:"proxies"`
Retry int `yaml:"retry"`
Priority int `yaml:"priority"`
Curl string `yaml:"curl"`
ExecuteInterval `yaml:"execute_interval"`
ExecuteAt `yaml:"execute_at"`
Device string `yaml:"device"`
Platform string `yaml:"platform"`
AreaCC string `yaml:"area_cc"`
Channel int `yaml:"channel"`
Media int `yaml:"media"`
SpiderID int `yaml:"spider_id"`
CatchAccountID int `yaml:"catch_account_id"`
}
// newDefaultConfig create a default config
func newDefaultConfig() *Config {
conf := &Config{
Session: 1,
Mode: 0,
Retry: 0,
Priority: 10000,
ExecuteInterval: ExecuteInterval{
TimeInterval: -1,
},
ExecuteAt: ExecuteAt{
Year: -1,
Month: -1,
Day: -1,
Hour: -1,
Min: -1,
Sec: -1,
},
Device: "",
Platform: "",
AreaCC: "",
Channel: -1,
Media: -1,
SpiderID: -1,
CatchAccountID: -1,
}
return conf
}
// NewConfig 加载并返回Config
func NewConfig(p string) *Config {
f, err := os.Open(p)
defer f.Close()
if err != nil {
panic(err)
}
conf := newDefaultConfig()
err = yaml.NewDecoder(f).Decode(conf)
if err != nil {
panic(err)
}
// spew.Dump(conf)
if conf.Curl == "" {
log.Println("the path ", p, "curl is \"\"")
} else {
if conf.Curl[0] == '@' {
curlfile, err := os.Open(conf.Curl[1:])
defer curlfile.Close()
if err != nil {
panic(err)
}
curldata, err := ioutil.ReadAll(curlfile)
conf.Curl = strings.Trim(string(curldata), "\r\n ")
}
}
return conf
}