tss/app/config.go

108 lines
2.2 KiB
Go
Raw Normal View History

2022-04-27 00:19:05 -04:00
package app
2022-04-26 09:55:59 -04:00
import (
"os"
2022-04-28 19:24:21 -04:00
"path"
2022-04-26 09:55:59 -04:00
2022-05-03 17:14:37 -04:00
"go.balki.me/tss/parser"
2022-04-26 09:55:59 -04:00
"gopkg.in/yaml.v3"
)
2022-05-02 23:23:56 -04:00
type FeedLimit int
const NoLimit FeedLimit = -1
2022-04-28 19:24:21 -04:00
type FeedCfg struct {
2022-05-03 17:14:37 -04:00
Name string `yaml:"name"`
Channel string `yaml:"channel"`
Rhash string `yaml:"rhash"`
Url string `yaml:"url"`
Cron string `yaml:"cron"`
Proxy string `yaml:"proxy"`
Type parser.FeedType `yaml:"type"`
2022-05-07 21:01:14 -04:00
Title string `yaml:"title"`
2022-05-03 17:14:37 -04:00
FTL *int `yaml:"first_time_limit"`
FirstTimeLimit FeedLimit `yaml:"-"`
2022-04-28 19:24:21 -04:00
}
2022-04-26 09:55:59 -04:00
type Config struct {
2022-05-02 23:23:56 -04:00
Proxy string `yaml:"proxy"`
TelegramProxy string `yaml:"telegram_proxy"`
TelegramAuthToken string `yaml:"telegram_auth_token"` //TODO: read from credential file
2022-05-01 23:28:54 -04:00
DataDir string `yaml:"data_dir"`
LastSuccessPath string `yaml:"last_loaded_path"`
DbDir string `yaml:"db_dir"`
2022-05-02 23:23:56 -04:00
FTL *int `yaml:"first_time_limit"`
2022-05-01 23:28:54 -04:00
Feeds []FeedCfg `yaml:"feeds"`
2022-04-26 09:55:59 -04:00
}
2022-04-27 00:19:05 -04:00
func ParseConfig(configPath string) (*Config, error) {
2022-04-28 19:24:21 -04:00
2022-04-27 00:19:05 -04:00
cfg, err := os.ReadFile(configPath)
2022-04-26 09:55:59 -04:00
if err != nil {
return nil, err
}
c := Config{}
err = yaml.Unmarshal(cfg, &c)
if err != nil {
return nil, err
}
2022-04-28 19:24:21 -04:00
if c.DataDir == "" {
c.DataDir = "."
}
if c.LastSuccessPath == "" {
c.LastSuccessPath = path.Join(c.DataDir, "last_success.yml")
}
2022-05-01 15:32:41 -04:00
if c.DbDir == "" {
c.DbDir = path.Join(c.DataDir, "feed_data")
}
2022-05-02 23:23:56 -04:00
err = os.MkdirAll(c.DbDir, 0755)
if err != nil {
return nil, err
}
err = os.MkdirAll(c.DataDir, 0755)
if err != nil {
return nil, err
}
2022-05-01 19:13:27 -04:00
if c.Proxy != "" {
if c.TelegramProxy == "" {
c.TelegramProxy = c.Proxy
}
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "" {
feedCfg.Proxy = c.Proxy
}
}
}
2022-05-02 11:36:24 -04:00
if c.TelegramProxy == "NONE" {
c.TelegramProxy = ""
}
2022-05-01 19:13:27 -04:00
for i, _ := range c.Feeds {
feedCfg := &c.Feeds[i]
if feedCfg.Proxy == "NONE" {
feedCfg.Proxy = ""
}
2022-05-02 23:23:56 -04:00
if feedCfg.FTL != nil {
feedCfg.FirstTimeLimit = FeedLimit(*feedCfg.FTL)
} else if c.FTL != nil {
feedCfg.FirstTimeLimit = FeedLimit(*c.FTL)
} else {
feedCfg.FirstTimeLimit = NoLimit
}
2022-05-01 19:13:27 -04:00
}
2022-04-26 09:55:59 -04:00
return &c, nil
}