1
0
mirror of https://git.keinpfusch.net/loweel/zorg synced 2024-12-18 14:58:35 +01:00
zorg/feed.go
2023-07-15 00:25:03 +02:00

106 lines
1.6 KiB
Go

package main
import (
"bufio"
"fmt"
"log"
"os"
"time"
"github.com/mmcdole/gofeed"
)
func init() {
go feedDaemon()
log.Println("Feed daemon started.")
}
func forwardLastFeed(url string) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
log.Println(err)
return
}
var b *gofeed.Item
var postAge time.Time
if len(feed.Items) > 0 {
b = feed.Items[0]
log.Printf("%d - %s %s\n", len(feed.Items), "Feeds from: ", feed.Title)
} else {
return
}
postAge = time.Now()
log.Println("Post Age: ", postAge)
if b.PublishedParsed != nil {
postAge = *b.PublishedParsed
} else {
postAge = *b.UpdatedParsed
}
log.Println("Post Age: ", postAge)
if time.Since(postAge) < Zint {
postOnMastodon(b.Link, "["+feed.Title+"] "+b.Title)
log.Println("New content from: ", feed.Title, b.Title)
} else {
log.Println("No new content from: ", feed.Title)
}
}
// but we need to iterate on feeds....
func feedDaemon() {
ticker := time.NewTicker(Zint)
defer ticker.Stop()
scanFeeds(fileByLines("feeds.conf"))
log.Println("RSS poll done. Next in ", Zint.String())
for range ticker.C {
scanFeeds(fileByLines("feeds.conf"))
log.Println("RSS poll done. Next in ", Zint.String())
}
}
func scanFeeds(fiids []string) {
for _, a := range fiids {
forwardLastFeed(a)
}
}
func fileByLines(filename string) (blurls []string) {
file, err := os.Open(filename)
if err != nil {
fmt.Println(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
d := scanner.Text()
blurls = append(blurls, d)
}
if err := scanner.Err(); err != nil {
fmt.Println(err.Error())
}
return
}