You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.0 KiB

package main
import (
"log"
"os"
"github.com/jessevdk/go-flags"
"gopkg.in/yaml.v2"
)
type RSSItem struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
}
type Config struct {
Dbpath string `yaml:"dbpath"`
Telegram struct {
SendDebug bool `yaml:"senddebug"`
ChatId int64 `yaml:"chatid"`
Token string `yaml:"token"`
} `yaml:"telegram"`
RssList []RSSItem `yaml:"rsslist"`
}
func NewConfig(configPath string) (*Config, error) {
// Create config structure
config := &Config{}
// Open config file
file, err := os.Open(configPath)
if err != nil {
return nil, err
}
defer file.Close()
// Init new YAML decode
d := yaml.NewDecoder(file)
// Start YAML decoding from file
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
type Options struct {
ConfigPath string `short:"c" long:"configpath" description:"Config file path"`
}
var ConfigPath = "./config.yml"
func main() {
var options Options
var parser = flags.NewParser(&options, flags.Default)
if _, err := parser.Parse(); err != nil {
switch flagsErr := err.(type) {
case flags.ErrorType:
if flagsErr == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
default:
os.Exit(1)
}
}
log.Println("Flags processed")
if options.ConfigPath != "" {
ConfigPath = options.ConfigPath
}
// Get config
log.Printf("Config file: %s\n", ConfigPath)
cfg, err := NewConfig(ConfigPath)
if err != nil {
log.Fatal(err)
}
log.Println("Start to send")
for _, v := range cfg.RssList {
log.Printf("Feed: %s, URL: %s\n", v.Name, v.URL)
rss, err := GetRSS(v.Name, v.URL)
if err != nil {
log.Fatalln(err)
}
send, err := ProcessRss(*rss, cfg.Dbpath, v.Name)
if err != nil {
log.Fatal(err)
}
if nil != send && len(send.ItemList) > 0 {
if err := SendAndWriteToDB(*send, cfg.Dbpath, v.Name, cfg.Telegram.Token, cfg.Telegram.ChatId, cfg.Telegram.SendDebug); err != nil {
log.Fatal(err)
}
} else {
log.Println("Nothing to send")
}
}
log.Println("Stop to send")
}