rssd/main.go
2019-03-13 15:47:38 -04:00

109 lines
1.8 KiB
Go

package main
import (
"log"
"os"
"time"
"github.com/BurntSushi/toml"
"github.com/mmcdole/gofeed"
)
type Config struct {
Urls []string
}
type Item struct {
Title, Description, Url, Filename string
}
type Podcast struct {
Title, Description string
Items []Item
}
type pcList struct {
Podcasts []Podcast
}
func (p *pcList) Add(x *Podcast) {
(*p).Podcasts = append((*p).Podcasts,*x)
}
type Db struct {
Podcasts []Podcast
}
type Selector func(*gofeed.Item) bool
func Any(ss []Selector, i *gofeed.Item) bool {
for _, s := range ss {
if s(i) {
return true
}
}
return false
}
func All(ss []Selector, i *gofeed.Item) bool {
for _, s := range ss {
if !s(i) {
return false
}
}
return true
}
func newerThan(t time.Time) Selector {
return func(i *gofeed.Item) bool {
if i.PublishedParsed.After(t) {
return true
}
return false
}
}
func readFeed(url string,ss ...Selector) *Podcast {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
log.Fatal(err)
}
pc := Podcast{ feed.Title, feed.Description, []Item{} }
for _, i := range feed.Items {
if All(ss,i) {
it := Item{ Title: i.Title, Description: i.Description }
pc.Items = append(pc.Items,it)
}
}
return &pc
}
func main() {
log.Print("rssd")
log.Print("reading configuration")
var conf Config
if _, err := toml.DecodeFile("rssd.conf", &conf); err != nil {
log.Fatal("Error reading config file:",err)
}
pl := &pcList{}
pl.Podcasts = make([]Podcast,0)
d := time.Now()
lastMonth := newerThan(time.Date(d.Year(),d.Month()-1,d.Day(),0,0,0,0,time.Local))
for _,url := range conf.Urls {
log.Print(" -> ",url)
pl.Add(readFeed(url,lastMonth))
}
of,err := os.Create("output.conf")
if err != nil {
log.Fatal("Cannot open output file")
}
enc := toml.NewEncoder(of)
log.Print("writing output")
enc.Encode(pl)
of.Close()
log.Print("done")
}