2011-05-21 17:12:43 +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.
|
2011-05-22 14:20:05 +00:00
|
|
|
|
|
|
|
Build & run with:
|
|
|
|
|
|
|
|
$ 6g example.go && 6l example.6 && ./6.out
|
|
|
|
|
2011-05-21 17:12:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
rss "github.com/jteeuwen/go-pkg-rss"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2011-05-22 14:20:05 +00:00
|
|
|
// 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.
|
2011-05-21 17:12:43 +00:00
|
|
|
PollFeed("http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", 5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PollFeed(uri string, timeout int) {
|
|
|
|
feed := rss.New(timeout, true, chanHandler, itemHandler)
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err := feed.Fetch(uri); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "[e] %s: %s", uri, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(feed.SecondsTillUpdate() * 1e9)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
|
|
|
|
fmt.Printf("%d new channel(s) in %s\n", len(newchannels), feed.Url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
|
|
|
|
fmt.Printf("%d new item(s) in %s\n", len(newitems), feed.Url)
|
|
|
|
}
|