From 08ef8abf3554ae560baf3863b0057305c8978b07 Mon Sep 17 00:00:00 2001 From: Greg Date: Wed, 13 Mar 2019 15:47:38 -0400 Subject: [PATCH] Initial commit. --- .gitignore | 2 + main.go | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92ad372 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +rssd.conf +output.conf diff --git a/main.go b/main.go new file mode 100644 index 0000000..32d7fc5 --- /dev/null +++ b/main.go @@ -0,0 +1,108 @@ +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") +}