zorg/feed.go

110 lines
2.0 KiB
Go
Raw Normal View History

2020-10-08 14:33:26 -05:00
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
2023-07-14 17:25:03 -05:00
var postAge time.Time
2020-10-08 14:33:26 -05:00
if len(feed.Items) > 0 {
b = feed.Items[0]
log.Printf("%d - %s %s\n", len(feed.Items), "Feeds from: ", feed.Title)
} else {
return
}
2023-07-14 17:25:03 -05:00
if b.PublishedParsed != nil {
postAge = *b.PublishedParsed
2023-07-14 17:37:54 -05:00
} else if b.UpdatedParsed != nil {
log.Println("Ops, no Published in the feed. Using Update Date")
2023-07-14 17:25:03 -05:00
postAge = *b.UpdatedParsed
2023-07-14 17:37:54 -05:00
} else {
log.Println("No Published date or update date. This feed is crap, giving up")
2023-07-14 18:06:56 -05:00
postAge = time.Now().Add(Zint + (1 * time.Hour))
2023-07-14 17:25:03 -05:00
}
2023-07-14 18:06:56 -05:00
log.Println("Last Post Age: ", postAge)
2023-07-14 17:25:03 -05:00
if time.Since(postAge) < Zint {
2020-10-08 14:33:26 -05:00
2023-07-14 19:04:50 -05:00
TheTitle := fmt.Sprintf("[ %s ]", feed.Title)
TheBody := fmt.Sprintf("%s \n\n%s \n\n%s", b.Author.Name, feed.Description, b.Link)
postOnMastodon(TheBody, TheTitle)
2023-07-14 18:06:56 -05:00
log.Println("New content from: ", feed.Title, b.Title, feed.Description)
2020-10-08 14:33:26 -05:00
} else {
2023-07-14 18:06:56 -05:00
log.Println("No new content from: ", feed.Title, feed.Description)
2020-10-08 14:33:26 -05:00
}
}
2023-07-14 17:25:03 -05:00
// but we need to iterate on feeds....
2020-10-08 14:33:26 -05:00
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
}