gruik

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

commit 153b44aa6c868d0132b872f2b1a24f9f86943137
parent 6b93082010a752a9849edebfdb27c5a934cb700a
Author: Emile 'iMil' Heitor <imil@NetBSD.org>
Date:   Thu, 13 Jul 2023 23:01:25 +0200

feat: added hash to compare news and for future xpost

Diffstat:
MREADME.md | 1+
Mmain.go | 23+++++++++++++++++++----
2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md @@ -20,6 +20,7 @@ irc: title: pink # ditto news: bold # ditto link: lightblue # ditto + hash: gray # ditto ops: # ditto - MrFoo # ditto - MrsBar # ditto diff --git a/main.go b/main.go @@ -1,6 +1,8 @@ package main import ( + "crypto/sha256" + "encoding/hex" "encoding/json" "fmt" "io" @@ -19,13 +21,21 @@ type News struct { Origin string `json:"origin"` Title string `json:"title"` Link string `json:"link"` + Hash string `json:"hash"` } const feedJson = "feed.json" +func mkHash(s1, s2 string) string { + s := s1 + s2 + h := sha256.Sum256([]byte(s)) + hash := hex.EncodeToString(h[:]) + return hash[:8] +} + func newsExists(newslist []News, news News) bool { for _, n := range newslist { - if n.Title == news.Title && n.Link == news.Link { + if n.Hash == news.Hash { return true } } @@ -83,10 +93,11 @@ func newsFetch(client *girc.Client, channel string) { Origin: feed.Title, Title: item.Title, Link: item.Link, + Hash: mkHash(item.Title, item.Link), } // Check if item was already posted if newsExists(newslist, news) { - log.Printf("already posted %s\n", item.Title) + log.Printf("already posted %s (%s)\n", item.Title, news.Hash) continue } // don't paste news older than feeds.maxage @@ -100,13 +111,15 @@ func newsFetch(client *girc.Client, channel string) { break } - post := fmt.Sprintf("[{%s}%s{c}] {%s}%s{c}: {%s}%s{c}", + post := fmt.Sprintf("[{%s}%s{c}] {%s}%s{c}: {%s}%s{c} {%s}#%s{c}", viper.GetString("irc.colors.title"), feed.Title, viper.GetString("irc.colors.news"), item.Title, viper.GetString("irc.colors.link"), - item.Link) + item.Link, + viper.GetString("irc.colors.hash"), + news.Hash) client.Cmd.Message(channel, girc.Fmt(post)) @@ -116,6 +129,7 @@ func newsFetch(client *girc.Client, channel string) { newslist = append(newslist, news) } } + // save news list to disk to avoid repost when restarting if err = encoder.Encode(newslist); err != nil { client.Cmd.Message(channel, "could write newslist") } @@ -134,6 +148,7 @@ func confDefault() { "irc.colors.title": "pink", "irc.colors.news": "bold", "irc.colors.link": "lightblue", + "irc.colors.hash": "gray", "feeds.urls": []string{}, "feeds.maxnews": 10, "feeds.maxage": "1h",