gruik

Fork of GCU-Squad's RSS-to-IRC bridge
git clone https://git.instinctive.eu/gruik.git
Log | Files | Refs | README | LICENSE

commit 5029b742e515a14ac7d861ee1a5a141c2b4c6dba
parent 893f060181f9aa836c063396efe84965a3ed3f1e
Author: Emile 'iMil' Heitor <imil@NetBSD.org>
Date:   Tue, 11 Jul 2023 18:58:35 +0200

feat: default parameters and don't post old news

Diffstat:
Mmain.go | 70+++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 53 insertions(+), 17 deletions(-)

diff --git a/main.go b/main.go @@ -13,10 +13,8 @@ import ( "github.com/spf13/viper" ) +// Fetch and post news from RSS feeds func newsFetch(client *girc.Client, channel string) { - // Fetch and post news from RSS feeds - frequency := viper.GetDuration("news_frequency") - maxnews := viper.GetInt("feeds.maxnews") postedItems := make(map[string]bool) @@ -28,9 +26,11 @@ func newsFetch(client *girc.Client, channel string) { } log.Printf("%v, not connected, waiting...\n", client.ChannelList()) - time.Sleep(frequency) + time.Sleep(viper.GetDuration("feeds.frequency")) } + oneDay := 24 * time.Hour + for { if time.Since(start) > oneDay { postedItems = make(map[string]bool) @@ -52,8 +52,13 @@ func newsFetch(client *girc.Client, channel string) { log.Printf("already posted %s\n", itemID) continue } + // don't paste news older than feeds.maxage + if time.Since(*item.PublishedParsed) > viper.GetDuration("feeds.maxage") { + log.Printf("news too old (%s)\n", item.Published) + continue + } i++ - if i > maxnews { + if i > viper.GetInt("feeds.maxnews") { log.Println("too many lines to post") break } @@ -67,12 +72,38 @@ func newsFetch(client *girc.Client, channel string) { postedItems[itemID] = true } } - time.Sleep(frequency) + time.Sleep(viper.GetDuration("feeds.frequency")) + } +} + +func confDefault() { + kv := map[string]interface{}{ + "irc.server": "irc.libera.chat", + "irc.nick": "gruik", + "irc.password": "piggypiggy", + "irc.channel": "goaste", + "irc.debug": false, + "irc.port": 6667, + "feeds.urls": []string{}, + "feeds.maxnews": 10, + "feeds.maxage": 10, + "feeds.frequency": 5, + } + + for k, v := range kv { + if !viper.IsSet(k) { + viper.Set(k, v) + } } } func main() { - viper.SetConfigName("config") + config := "config" + if len(os.Args) > 1 { + config = os.Args[1] + } + + viper.SetConfigName(config) viper.SetConfigType("yaml") viper.AddConfigPath(".") viper.WatchConfig() @@ -82,6 +113,7 @@ func main() { } nick := viper.GetString("irc.nick") + password := viper.GetString("irc.password") name := nick user := strings.ToLower(nick) channel := "#" + viper.GetString("irc.channel") @@ -90,19 +122,23 @@ func main() { debug = os.Stdout } + confDefault() // load defaults for unset parameters + client := girc.New(girc.Config{ - Server: viper.GetString("irc.server"), - Nick: nick, - Port: viper.GetInt("irc.port"), - Debug: debug, - User: user, - Name: name, - SASL: &girc.SASLPlain{ - User: user, - Pass: viper.GetString("irc.password"), - }, + Server: viper.GetString("irc.server"), + Nick: nick, + Port: viper.GetInt("irc.port"), + Debug: debug, + User: user, + Name: name, AllowFlood: true, }) + if len(password) > 0 { + client.Config.SASL = &girc.SASLPlain{ + User: user, + Pass: password, + } + } client.Handlers.Add(girc.CONNECTED, func(c *girc.Client, e girc.Event) { c.Cmd.Join(channel)