From 000ea0f269982c2bf3af3fa20ea23c57d416b0a3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 30 Jul 2015 17:25:17 +0200 Subject: Refactor feed fetcher --- rss.go | 75 +++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 24 deletions(-) (limited to 'rss.go') diff --git a/rss.go b/rss.go index c394edb..b74655b 100644 --- a/rss.go +++ b/rss.go @@ -9,50 +9,77 @@ import ( irc "github.com/fluffle/goirc/client" ) -var Feed = map[string]string{ - "LOR News": `https://www.linux.org.ru/section-rss.jsp?section=1`, - "LOR Forum": `https://www.linux.org.ru/section-rss.jsp?section=2&filter=tech`, - "LOR Gallery": `https://www.linux.org.ru/section-rss.jsp?section=3`, - "LOR Polls": `https://www.linux.org.ru/section-rss.jsp?section=5`, - "OpenNET": `http://www.opennet.ru/opennews/opennews_all_noadv.rss`, - "Undeadly": `http://undeadly.org/cgi?action=rss`, +type Feed struct { + Name string + URL string + Period time.Duration } -type News struct { - Source string - Item rss.Item +var Feeds = []Feed{ + { + Name: "LOR News", + URL: `https://www.linux.org.ru/section-rss.jsp?section=1`, + Period: time.Hour, + }, + { + Name: "LOR Forum", + URL: `https://www.linux.org.ru/section-rss.jsp?section=2`, + Period: 5 * time.Minute, + }, + { + Name: "LOR Gallery", + URL: `https://www.linux.org.ru/section-rss.jsp?section=3`, + Period: time.Hour, + }, + { + Name: "OpenNET", + URL: `http://www.opennet.ru/opennews/opennews_all_noadv.rss`, + Period: time.Hour, + }, + { + Name: "Undeadly", + URL: `http://undeadly.org/cgi?action=rss`, + Period: 2 * time.Hour, + }, + { + Name: "Kernel", + URL: `https://www.kernel.org/feeds/kdist.xml`, + Period: 6 * time.Hour, + }, } -const timeOut = 30 * time.Minute +type News struct { + Feed + rss.Item +} var news = make(chan News) func ShowNews(conn *irc.Conn, _ *irc.Line) { - for { - n := <-news - s := fmt.Sprintf("%v: %v - %v", n.Source, - n.Item.Title, n.Item.Link) + for n := range news { + s := fmt.Sprintf("%v: %v - %v", n.Name, n.Title, n.Link) conn.Notice(*room, s) } } func init() { - for k, v := range Feed { - go func(c chan News, source, url string) { - for { - r, err := rss.Fetch(url) + for _, f := range Feeds { + go func(c chan News, f Feed) { + ticker := time.NewTicker(f.Period) + defer ticker.Stop() + for t := range ticker.C { + r, err := rss.Fetch(f.URL) if err != nil { - log.Println(source, err) + log.Println(f.Name, err) return } - to := time.Now().Add(-timeOut) + to := t.Add(-f.Period) for _, i := range r.Channel.Items { if i.PubDate.After(to) { - c <- News{source, i} + c <- News{f, i} } } - time.Sleep(timeOut) } - }(news, k, v) + }(news, f) } } -- cgit v1.2.3