51 lines
1,021 B
Go
51 lines
1,021 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"log"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func loadNotifiedEntries() {
|
||
|
file, err := os.Open(notifiedFilePath)
|
||
|
if err != nil {
|
||
|
if os.IsNotExist(err) {
|
||
|
return
|
||
|
}
|
||
|
log.Fatalf("Error loading notified entries: %v", err)
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
err = json.NewDecoder(file).Decode(¬ifiedEntries)
|
||
|
if err != nil {
|
||
|
log.Fatalf("Error decoding notified entries: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func saveNotifiedEntry(entryNumber int) {
|
||
|
notifiedEntries[entryNumber] = true
|
||
|
file, err := os.Create(notifiedFilePath)
|
||
|
if err != nil {
|
||
|
log.Fatalf("Error saving notified entries: %v", err)
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
err = json.NewEncoder(file).Encode(notifiedEntries)
|
||
|
if err != nil {
|
||
|
log.Fatalf("Error encoding notified entries: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func checkAndSendNotifications() {
|
||
|
for _, blog := range blogs {
|
||
|
for _, entry := range blog.Entries {
|
||
|
if !entry.Notified && !time.Now().Before(entry.Date) {
|
||
|
sendNotifications(entry)
|
||
|
entry.Notified = true
|
||
|
saveNotifiedEntry(entry.Number)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|