Newer
Older
zorg / feed.go
Loweel on 17 Aug 1 KB First commit
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

	if len(feed.Items) > 0 {
		b = feed.Items[0]
		log.Printf("%d - %s %s\n", len(feed.Items), "Feeds from: ", feed.Title)
	} else {
		return
	}

	if time.Since(*b.PublishedParsed) < 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

}