flokati/modules/rss.go

88 lines
2.0 KiB
Go
Raw Normal View History

// vi:ts=4:sts=4:sw=4:noet:tw=72
2016-01-10 17:45:21 +00:00
//
// 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.
2016-02-15 23:17:42 +00:00
package modules
2016-01-10 17:45:21 +00:00
import (
"bufio"
"errors"
2017-06-28 11:08:23 +00:00
"flag"
2016-01-10 17:45:21 +00:00
"fmt"
"io"
"os"
"time"
2017-12-15 08:59:28 +00:00
"git.dnix.de/an/xlog"
2016-03-12 12:09:25 +00:00
2018-02-21 11:03:28 +00:00
gorss "github.com/muesli/go-pkg-rss"
"github.com/muesli/go-pkg-xmlx"
2016-01-10 17:45:21 +00:00
)
var hideOutput = true
2017-06-28 21:28:39 +00:00
var rssFeeds = flag.String("rss_feeds", "feeds.txt", "Feed list file")
var rssChannel = flag.String("rss_channel", "", "Target channel")
2016-01-10 17:45:21 +00:00
2016-02-15 23:17:42 +00:00
func init() {
MsgFuncs["rss"] = rssHandleMessage
RunFuncs["rss"] = rssRun
}
func rssRun() {
time.Sleep(5 * time.Second)
2017-06-28 11:08:23 +00:00
file, err := os.Open(*rssFeeds)
2016-01-10 17:45:21 +00:00
if err != nil {
2016-03-12 12:09:25 +00:00
xlog.Fatal(err.Error())
2016-01-10 17:45:21 +00:00
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
2016-02-15 23:17:42 +00:00
go rssPollFeed(scanner.Text(), 5, rssCharsetReader)
2016-01-10 17:45:21 +00:00
}
if err := scanner.Err(); err != nil {
2016-03-12 12:09:25 +00:00
xlog.Fatal(err.Error())
2016-01-10 17:45:21 +00:00
}
2016-02-15 23:17:42 +00:00
go func() {
time.Sleep(60 * time.Second)
hideOutput = false
}()
2016-01-10 17:45:21 +00:00
}
2017-06-28 21:28:39 +00:00
func rssHandleMessage(m *Message) {
2016-02-15 23:17:42 +00:00
}
func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) {
feed := gorss.New(timeout, true, rssChanHandler, rssItemHandler)
2016-01-10 17:45:21 +00:00
for {
2016-03-12 12:09:25 +00:00
xlog.Info("Polling feed: %s", uri)
2016-01-10 17:45:21 +00:00
if err := feed.Fetch(uri, cr); err != nil {
2016-03-12 12:09:25 +00:00
xlog.Info("[e] %s: %s", "*", uri, err)
2017-06-28 21:28:39 +00:00
SayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", *rssChannel, uri, err)
2016-01-10 17:45:21 +00:00
return
}
<-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9))
}
}
2016-02-15 23:17:42 +00:00
func rssChanHandler(feed *gorss.Feed, newchannels []*gorss.Channel) {
2016-02-28 11:38:32 +00:00
//SayCh <- fmt.Sprintf("%s\n[RSS] %d new channel(s) in %s", "*", len(newchannels), feed.Url)
2016-01-10 17:45:21 +00:00
}
2016-02-15 23:17:42 +00:00
func rssItemHandler(feed *gorss.Feed, ch *gorss.Channel, newitems []*gorss.Item) {
2016-01-10 17:45:21 +00:00
if hideOutput {
return
}
for _, ni := range newitems {
2017-06-28 21:28:39 +00:00
SayCh <- fmt.Sprintf("%s\n[RSS] %v - %v", *rssChannel, ni.Title, ni.Links[0].Href)
2016-01-10 17:45:21 +00:00
}
}
2016-02-15 23:17:42 +00:00
func rssCharsetReader(charset string, r io.Reader) (io.Reader, error) {
2016-01-10 17:45:21 +00:00
if charset == "ISO-8859-1" || charset == "iso-8859-1" {
return r, nil
}
return nil, errors.New("Unsupported character set encoding: " + charset)
}