2014-10-04 11:45:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
This is a minimal sample application, demonstrating how to set up an RSS feed
|
|
|
|
for regular polling of new channels/items.
|
|
|
|
|
|
|
|
Build & run with:
|
|
|
|
|
2014-11-16 16:07:39 +00:00
|
|
|
$ go run handlerexample.go
|
2014-10-04 11:45:05 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-02-09 14:26:29 +00:00
|
|
|
rss "github.com/jteeuwen/go-pkg-rss"
|
2014-10-04 11:45:05 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
// This sets up a new feed and polls it for new channels/items.
|
|
|
|
// Invoke it with 'go PollFeed(...)' to have the polling performed in a
|
|
|
|
// separate goroutine, so you can continue with the rest of your program.
|
|
|
|
PollFeed("http://blog.case.edu/news/feed.atom", 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PollFeed(uri string, timeout int) {
|
|
|
|
|
2015-02-09 14:26:29 +00:00
|
|
|
handlers := &MyHandlers{}
|
|
|
|
feed := rss.NewWithHandlers(timeout, true, handlers, handlers)
|
2014-10-04 11:45:05 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
if err := feed.Fetch(uri, nil); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "[e] %s: %s", uri, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(time.Duration(10 * time.Second))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:26:29 +00:00
|
|
|
type MyHandlers struct{}
|
2014-10-04 11:45:05 +00:00
|
|
|
|
2015-02-09 14:26:29 +00:00
|
|
|
func (m *MyHandlers) ProcessChannels(feed *rss.Feed, newchannels []*rss.Channel) {
|
2014-10-04 11:45:05 +00:00
|
|
|
fmt.Printf("%d new channel(s) in %s\n", len(newchannels), feed.Url)
|
|
|
|
}
|
|
|
|
|
2015-02-09 14:26:29 +00:00
|
|
|
func (m *MyHandlers) ProcessItems(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
|
2014-10-04 11:45:05 +00:00
|
|
|
fmt.Printf("%d new rad item(s) in %s\n", len(newitems), feed.Url)
|
|
|
|
}
|