diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2015-07-30 17:25:17 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2015-07-30 17:25:17 +0200 |
commit | 000ea0f269982c2bf3af3fa20ea23c57d416b0a3 (patch) | |
tree | 65721fd143bc69553634a3a53e77e70f38bc05f0 /rss.go | |
parent | c0ea5525a97a4af9d876c33b0f93445a4767557b (diff) |
Refactor feed fetcher
Diffstat (limited to 'rss.go')
-rw-r--r-- | rss.go | 75 |
1 files changed, 51 insertions, 24 deletions
@@ -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) } } |