commit 1b0b58cb66c6e417ad571d32b92945ae52686f40
parent 153b44aa6c868d0132b872f2b1a24f9f86943137
Author: Emile 'iMil' Heitor <imil@NetBSD.org>
Date: Thu, 13 Jul 2023 23:31:32 +0200
fix: one feed file per channel + reset backup file at new reload
Diffstat:
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/main.go b/main.go
@@ -24,8 +24,6 @@ type News struct {
Hash string `json:"hash"`
}
-const feedJson = "feed.json"
-
func mkHash(s1, s2 string) string {
s := s1 + s2
h := sha256.Sum256([]byte(s))
@@ -58,18 +56,18 @@ func newsFetch(client *girc.Client, channel string) {
time.Sleep(viper.GetDuration("feeds.frequency"))
}
+ feedFile := channel + "-feed.json"
// load saved news
- f, err := os.OpenFile(feedJson, os.O_CREATE|os.O_RDWR, 0o644)
+ f, err := os.OpenFile(feedFile, os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
- log.Fatalf("can't open %s: %v", feedJson, err)
+ log.Fatalf("can't open %s: %v", feedFile, err)
}
defer f.Close()
// read news from disk
decoder := json.NewDecoder(f)
if err := decoder.Decode(&newslist); err != nil {
- client.Cmd.Message(channel, "could load news list, empty?")
+ client.Cmd.Message(channel, "could not load news list, empty?")
}
- encoder := json.NewEncoder(f) // for later writing
oneDay := 24 * time.Hour
@@ -111,7 +109,7 @@ func newsFetch(client *girc.Client, channel string) {
break
}
- post := fmt.Sprintf("[{%s}%s{c}] {%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"),
@@ -130,6 +128,13 @@ func newsFetch(client *girc.Client, channel string) {
}
}
// save news list to disk to avoid repost when restarting
+ if err := f.Truncate(0); err != nil {
+ log.Fatal(err)
+ }
+ if _, err = f.Seek(0, 0); err != nil {
+ log.Fatal(err)
+ }
+ encoder := json.NewEncoder(f)
if err = encoder.Encode(newslist); err != nil {
client.Cmd.Message(channel, "could write newslist")
}