From 995792322699a951832ca2f15f99d89a78d74049 Mon Sep 17 00:00:00 2001 From: jim teeuwen Date: Sat, 21 May 2011 19:12:43 +0200 Subject: [PATCH] Revised README and added example.go with a small idiomatic example on how to use this package. --- README | 19 +++++++++++++++++-- example.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 example.go diff --git a/README b/README index 00db9ef..9620648 100644 --- a/README +++ b/README @@ -1,5 +1,6 @@ -Author: jim teeuwen -Dependencies: go-pkg-xmlx ( http://github.com/jteeuwen/go-pkg-xmlx ) +================================================================================ + RSS +================================================================================ This package allows us to fetch Rss and Atom feeds from the internet. They are parsed into an object tree which is a hybrid of both the RSS and Atom @@ -29,3 +30,17 @@ either a new channel or a new item is found that previously did not exist. This allows you to easily monitor a feed for changes. See src/feed_test.go for an example of how this works. +================================================================================ + DEPENDENCIES +================================================================================ + + goinstall github.com/jteeuwen/go-pkg-xmlx + +================================================================================ + USAGE +================================================================================ + + $ goinstall github.com/jteeuwen/go-pkg-rss + + An idiomatic example program can be found in example.go. + diff --git a/example.go b/example.go new file mode 100644 index 0000000..cc31b03 --- /dev/null +++ b/example.go @@ -0,0 +1,42 @@ +package main + +/* +This is a minimal sample application, demonstrating how to set up an RSS feed +for regular polling of new channels/items. +*/ + +import ( + "fmt" + "os" + "time" + rss "github.com/jteeuwen/go-pkg-rss" +) + +func main() { + // This sets up a new feed and polls it for new channels/items in + // a separate goroutine. 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://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) +}