diff options
author | Dimitri Sokolyuk <demon@dim13.org> | 2019-07-06 17:17:46 +0200 |
---|---|---|
committer | Dimitri Sokolyuk <demon@dim13.org> | 2019-07-06 17:17:46 +0200 |
commit | 1d2ca509c77cbb2af0475b1319cd840f8ce9a1d0 (patch) | |
tree | 05a838baaf4f96fbcce03d06090a2403ee56c878 /internal/feeds | |
parent | 87e820722cf02054225b47a58f97d0824118292f (diff) |
Split in packages
Diffstat (limited to 'internal/feeds')
-rw-r--r-- | internal/feeds/feeds.go | 31 | ||||
-rw-r--r-- | internal/feeds/rss.go | 56 |
2 files changed, 87 insertions, 0 deletions
diff --git a/internal/feeds/feeds.go b/internal/feeds/feeds.go new file mode 100644 index 0000000..8c9bd72 --- /dev/null +++ b/internal/feeds/feeds.go @@ -0,0 +1,31 @@ +package feeds + +import "time" + +var Feeds = []Feed{ + { + Name: "LOR News", + URL: `https://www.linux.org.ru/section-rss.jsp?section=1`, + Every: time.Hour, + }, + { + Name: "LOR Forum", + URL: `https://www.linux.org.ru/section-rss.jsp?section=2&filter=tech`, + Every: 5 * time.Minute, + }, + { + Name: "LOR Gallery", + URL: `https://www.linux.org.ru/section-rss.jsp?section=3`, + Every: 2 * time.Hour, + }, + { + Name: "LOR Polls", + URL: `https://www.linux.org.ru/section-rss.jsp?section=5`, + Every: 2 * time.Hour, + }, + { + Name: "OpenNET", + URL: `http://www.opennet.ru/opennews/opennews_all_noadv.rss`, + Every: 4 * time.Hour, + }, +} diff --git a/internal/feeds/rss.go b/internal/feeds/rss.go new file mode 100644 index 0000000..f3dd39c --- /dev/null +++ b/internal/feeds/rss.go @@ -0,0 +1,56 @@ +package feeds + +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) + } +} |