From 43b0fbc7131cd2fb073a42b39fd9390e41f1e03d Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 24 Jul 2015 16:09:08 +0200 Subject: Automatic RSS watcher --- main.go | 14 ++++++++---- rss.go | 78 +++++++++++++++++++++++++++++------------------------------------ 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/main.go b/main.go index db6a92e..7c38442 100644 --- a/main.go +++ b/main.go @@ -18,19 +18,25 @@ func init() { } func main() { - c := irc.SimpleClient(*name) c.EnableStateTracking() quit := make(chan bool) c.HandleFunc(irc.DISCONNECTED, - func(conn *irc.Conn, line *irc.Line) { quit <- true }) + func(conn *irc.Conn, line *irc.Line) { + quit <- true + }) c.HandleFunc(irc.CONNECTED, - func(conn *irc.Conn, line *irc.Line) { conn.Join(*room) }) + func(conn *irc.Conn, line *irc.Line) { + conn.Join(*room) + go ShowNews(conn, line) + }) c.HandleFunc(irc.KICK, - func(conn *irc.Conn, line *irc.Line) { conn.Join(*room) }) + func(conn *irc.Conn, line *irc.Line) { + conn.Join(*room) + }) c.HandleFunc(irc.PRIVMSG, func(conn *irc.Conn, line *irc.Line) { diff --git a/rss.go b/rss.go index 0c42046..bde4adf 100644 --- a/rss.go +++ b/rss.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "log" + "time" "dim13.org/rss" irc "github.com/fluffle/goirc/client" @@ -11,56 +13,46 @@ var Feed = map[string]string{ "News": `https://www.linux.org.ru/section-rss.jsp?section=1`, "Forum": `https://www.linux.org.ru/section-rss.jsp?section=2`, "Gallery": `https://www.linux.org.ru/section-rss.jsp?section=3`, + "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`, + "Kernel": `https://www.kernel.org/feeds/kdist.xml`, } -type RSS struct{ Command } +type News struct { + Source string + Item rss.Item +} -func (v RSS) Handle(conn *irc.Conn, line *irc.Line) { - news, err := rss.Fetch(v.Arg) - if err != nil { - conn.Privmsg(line.Target(), err.Error()) - return - } - if line.Public() && len(news.Channel.Items) > 3 { - news.Channel.Items = news.Channel.Items[:3] - } - for n, i := range news.Channel.Items { - s := fmt.Sprintf("%2d. %v - %v", n+1, i.Title, i.Link) - conn.Privmsg(line.Target(), s) +const timeOut = 5 * time.Minute + +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) + conn.Privmsg(*room, s) } } func init() { - Register("news", &RSS{ - Command{ - Help: "LOR news (msg private to see all)", - Arg: Feed["News"], - }, - }) - Register("forum", &RSS{ - Command{ - Help: "LOR forum (msg private to see all)", - Arg: Feed["Forum"], - }, - }) - Register("gallery", &RSS{ - Command{ - Help: "LOR gallery (msg private to see all)", - Arg: Feed["Gallery"], - }, - }) - Register("bsd", &RSS{ - Command{ - Help: "Undeadly news (msg private to see all)", - Arg: Feed["Undeadly"], - }, - }) - Register("opennet", &RSS{ - Command{ - Help: "OpenNET news (msg private to see all)", - Arg: Feed["OpenNET"], - }, - }) + for k, v := range Feed { + go func(c chan News, source, url string) { + for { + to := time.Now().Add(-timeOut) + r, err := rss.Fetch(url) + if err != nil { + log.Println(err) + } + for _, i := range r.Channel.Items { + if i.PubDate.After(to) { + c <- News{source, i} + } + } + time.Sleep(timeOut) + } + }(news, k, v) + } } -- cgit v1.2.3