77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
|
// vim: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 news
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"log"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
rss "github.com/jteeuwen/go-pkg-rss"
|
||
|
"github.com/jteeuwen/go-pkg-xmlx"
|
||
|
)
|
||
|
|
||
|
var sayCh chan string
|
||
|
var hideOutput = true
|
||
|
|
||
|
func Init(ch chan string, path string) {
|
||
|
log.Printf("Initializing news module")
|
||
|
sayCh = ch
|
||
|
file, err := os.Open(path)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer file.Close()
|
||
|
scanner := bufio.NewScanner(file)
|
||
|
<-time.After(60 * time.Second)
|
||
|
for scanner.Scan() {
|
||
|
go PollFeed(scanner.Text(), 5, charsetReader)
|
||
|
}
|
||
|
if err := scanner.Err(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
time.Sleep(60 * time.Second)
|
||
|
hideOutput = false
|
||
|
}
|
||
|
|
||
|
func PollFeed(uri string, timeout int, cr xmlx.CharsetFunc) {
|
||
|
feed := rss.New(timeout, true, chanHandler, itemHandler)
|
||
|
for {
|
||
|
log.Printf("Polling feed: %s", uri)
|
||
|
if err := feed.Fetch(uri, cr); err != nil {
|
||
|
log.Printf("[e] %s: %s", "*", uri, err)
|
||
|
//sayCh <- fmt.Sprintf("%s\n[e] %s: %s", "*", uri, err)
|
||
|
return
|
||
|
}
|
||
|
<-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
|
||
|
//sayCh <- fmt.Sprintf("%s\n%d new channel(s) in %s", "*", len(newchannels), feed.Url)
|
||
|
}
|
||
|
|
||
|
func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
|
||
|
if hideOutput {
|
||
|
return
|
||
|
}
|
||
|
for _, ni := range newitems {
|
||
|
sayCh <- fmt.Sprintf("%s\n*** [NEWS] %v - %v", "*", ni.Title, ni.Links[0].Href)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func charsetReader(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)
|
||
|
}
|