diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2016-12-20 23:11:53 +0100 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2016-12-20 23:11:53 +0100 |
commit | d3203ad6128ec64ce5422e7609b71f518bc9a01a (patch) | |
tree | 86fd69b8d41f5b4bd1e10f32dc42718961232598 | |
parent | 5fe432b7c1d6d626699e8091005a0de788d8168a (diff) |
Watch news
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | rss.go | 44 |
2 files changed, 23 insertions, 23 deletions
@@ -23,7 +23,7 @@ func main() { c.HandleFunc(irc.CONNECTED, func(conn *irc.Conn, line *irc.Line) { conn.Join(*room) - ShowNews(conn, *room) + go ShowNews(conn, *room) }) c.HandleFunc(irc.KICK, func(conn *irc.Conn, line *irc.Line) { @@ -89,32 +89,32 @@ func (n News) String() string { return s } +func (f Feed) Watch(news chan News) { + 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) { + news <- News{f, i} + } + } + } +} + func ShowNews(conn *irc.Conn, room string) { news := make(chan News) for _, f := range Feeds { - go func(f Feed) { - 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 - } - to := t.Add(-f.Every) - for _, i := range r.Channel.Items { - if i.PubDate.After(to) { - news <- News{f, i} - } - } - } - }(f) + go f.Watch(news) } - go func() { - for n := range news { - conn.Notice(room, n.String()) - } - }() + for n := range news { + conn.Notice(room, n.String()) + } } |