proxy-detector/config/source.go

44 lines
818 B
Go
Raw Normal View History

package config
import (
"os"
2024-12-10 20:19:13 +08:00
"time"
"gopkg.in/yaml.v3"
)
type Source struct {
Type string `json:"type" yaml:"type"`
Options CrawOption `json:"options" yaml:"options"`
}
type CrawOption struct {
Url string `json:"url" yaml:"url"`
DateFormat bool `json:"date-format" yaml:"date-format"`
}
2024-12-10 20:19:13 +08:00
func (c *CrawOption) Urls() []string {
var urls = make([]string, 0)
url := c.Url
if c.DateFormat {
now := time.Now()
urls = append(urls, now.Format(url))
now = now.AddDate(0, 0, -1)
urls = append(urls, now.Format(url))
} else {
urls = append(urls, url)
}
return urls
}
func LoadSource(path string) ([]Source, error) {
var sources []Source
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
err = yaml.NewDecoder(f).Decode(&sources)
return sources, err
}