Revised README and added example.go with a small idiomatic example on how to use this package.
This commit is contained in:
parent
09a1cbe77d
commit
9957923226
19
README
19
README
|
@ -1,5 +1,6 @@
|
||||||
Author: jim teeuwen <jimteeuwen@gmail.com>
|
================================================================================
|
||||||
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.
|
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
|
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
|
This allows you to easily monitor a feed for changes. See src/feed_test.go for
|
||||||
an example of how this works.
|
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.
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue