2018-04-21 07:38:42 +00:00
|
|
|
// 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() {
|
2020-05-16 06:40:04 +00:00
|
|
|
// Suppress output of new items for 15 minutes after startup
|
|
|
|
// in order to prevent spam.
|
|
|
|
time.Sleep(15 * time.Minute)
|
2018-04-21 07:38:42 +00:00
|
|
|
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 {
|
2019-12-23 11:06:07 +00:00
|
|
|
xlog.Error("Feed error: %s: %s", "*", uri, err)
|
2018-04-21 07:38:42 +00:00
|
|
|
}
|
2019-12-23 11:06:07 +00:00
|
|
|
xlog.Info("Polled %s, next poll in %d seconds", uri, feed.SecondsTillUpdate())
|
2018-04-21 07:38:42 +00:00
|
|
|
<-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 {
|
2019-08-21 16:24:26 +00:00
|
|
|
ti := ni.Title
|
|
|
|
short := ""
|
|
|
|
if len(ti) > 80 {
|
|
|
|
runes := []rune(ti)
|
2019-08-21 16:30:40 +00:00
|
|
|
short = string(runes[0:80]) + " [...]"
|
2019-08-21 16:24:26 +00:00
|
|
|
} else {
|
|
|
|
short = ti
|
|
|
|
}
|
2019-08-21 14:25:37 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n[RSS] %v - %v", *rssChannel, short, ni.Links[0].Href)
|
2018-04-21 07:38:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|