This commit is contained in:
an 2018-04-21 09:38:42 +02:00
parent 2c7a4a8676
commit eb82de80e8
1 changed files with 86 additions and 0 deletions

86
modules/rss.go Normal file
View File

@ -0,0 +1,86 @@
// vi:ts=4:sts=4:sw=4:noet:tw=72
//
// This code is mostly derived from the example code by Jim Teeuwen
// and is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
// license.
package modules
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"time"
gorss "git.dnix.de/an/go-pkg-rss"
xmlx "git.dnix.de/an/go-pkg-xmlx"
"git.dnix.de/an/xlog"
)
var hideOutput = true
var rssFeeds = flag.String("rss_feeds", "feeds.txt", "Feed list file")
var rssChannel = flag.String("rss_channel", "", "Target channel")
func init() {
MsgFuncs["rss"] = rssHandleMessage
RunFuncs["rss"] = rssRun
}
func rssRun() {
time.Sleep(5 * time.Second)
file, err := os.Open(*rssFeeds)
if err != nil {
xlog.Fatal(err.Error())
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
go rssPollFeed(scanner.Text(), 5, rssCharsetReader)
}
if err := scanner.Err(); err != nil {
xlog.Fatal(err.Error())
}
go func() {
time.Sleep(60 * time.Second)
hideOutput = false
}()
}
func rssHandleMessage(m *Message) {
}
func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) {
feed := gorss.New(timeout, true, rssChanHandler, rssItemHandler)
for {
xlog.Info("Polling feed: %s", uri)
if err := feed.Fetch(uri, cr); err != nil {
xlog.Info("[e] %s: %s", "*", uri, err)
SayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", *rssChannel, uri, err)
return
}
<-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9))
}
}
func rssChanHandler(feed *gorss.Feed, newchannels []*gorss.Channel) {
//SayCh <- fmt.Sprintf("%s\n[RSS] %d new channel(s) in %s", "*", len(newchannels), feed.Url)
}
func rssItemHandler(feed *gorss.Feed, ch *gorss.Channel, newitems []*gorss.Item) {
if hideOutput {
return
}
for _, ni := range newitems {
SayCh <- fmt.Sprintf("%s\n[RSS] %v - %v", *rssChannel, ni.Title, ni.Links[0].Href)
}
}
func rssCharsetReader(charset string, r io.Reader) (io.Reader, error) {
if charset == "ISO-8859-1" || charset == "iso-8859-1" {
return r, nil
}
return nil, errors.New("Unsupported character set encoding: " + charset)
}