flokati/modules/feeds.go

69 lines
1.4 KiB
Go

package modules
import (
"bufio"
"flag"
"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() {
time.Sleep(15 * time.Second)
file, err := os.Open(*feedsFeeds)
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) {
defer func() {
if err := recover(); err != nil {
xlog.Error(fmt.Sprintf("%v", err))
}
}()
last := time.Now()
for {
fp := gofeed.NewParser()
feed, _ := fp.ParseURL(url)
if feed.Items == nil {
xlog.Error("Feed %s appears to lack any items", feed.Title)
return
}
for _, item := range feed.Items {
if item.PublishedParsed == nil {
xlog.Error("Feed %s appears to lack timestamps", feed.Title)
break
}
if item.PublishedParsed.Unix() > last.Unix() {
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)
}
}