flokati/modules/feeds.go

69 lines
1.4 KiB
Go
Raw Normal View History

2024-07-05 09:58:57 +00:00
package modules
import (
"bufio"
2024-07-06 21:55:40 +00:00
"flag"
2024-07-05 09:58:57 +00:00
"fmt"
"os"
"time"
"git.dnix.de/an/xlog"
"github.com/mmcdole/gofeed"
)
var feedsFeeds = flag.String("feeds_feeds", "feeds.txt", "Feed list file")
var feedsChannel = flag.String("feeds_channel", "", "Target channel")
func init() {
MsgFuncs["feeds"] = feedsHandleMessage
RunFuncs["feeds"] = feedsRun
}
func feedsRun() {
2024-07-06 21:55:40 +00:00
time.Sleep(15 * time.Second)
file, err := os.Open(*feedsFeeds)
2024-07-05 09:58:57 +00:00
if err != nil {
xlog.Fatal(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
go feedsPoll(scanner.Text())
}
if err := scanner.Err(); err != nil {
xlog.Fatal(err.Error())
}
}
func feedsHandleMessage(m *Message) {
}
func feedsPoll(url string) {
2024-07-06 21:55:40 +00:00
defer func() {
if err := recover(); err != nil {
xlog.Error(fmt.Sprintf("%v", err))
}
}()
2024-07-05 09:58:57 +00:00
last := time.Now()
for {
fp := gofeed.NewParser()
feed, _ := fp.ParseURL(url)
2024-07-06 21:55:40 +00:00
if feed.Items == nil {
xlog.Error("Feed %s appears to lack any items", feed.Title)
return
}
2024-07-05 09:58:57 +00:00
for _, item := range feed.Items {
2024-07-06 21:55:40 +00:00
if item.PublishedParsed == nil {
xlog.Error("Feed %s appears to lack timestamps", feed.Title)
break
}
if item.PublishedParsed.Unix() > last.Unix() {
2024-07-05 09:58:57 +00:00
SayCh <- fmt.Sprintf("%s\n[FEEDS] %s : %s - %s\n", *feedsChannel, feed.Title, item.Title, item.Link)
}
}
last = time.Now()
time.Sleep(15 * time.Minute)
}
}