package main import ( "fmt" "io" "log" "time" "dim13.org/rss" ) type Feed struct { Name string URL string Every time.Duration } type News struct { Feed rss.Item } func (n News) String() string { s := fmt.Sprintf("%v: %v", n.Name, n.Title) if n.Author != "" { s += fmt.Sprintf(" (%v) ", n.Author) } else { s += " - " } s += fmt.Sprintf("%v", n.Link) return s } func (f Feed) watch(w io.Writer) { ticker := time.NewTicker(f.Every) defer ticker.Stop() for t := range ticker.C { r, err := rss.Fetch(f.URL) if err != nil { log.Println(f.Name, err) return } last := t.Add(-f.Every) for _, i := range r.Channel.Items { if i.PubDate.After(last) { fmt.Fprint(w, News{f, i}) } } } } func Watch(w io.Writer, feeds []Feed) { for _, feed := range feeds { go feed.watch(w) } }