-
Notifications
You must be signed in to change notification settings - Fork 68
/
example.go
46 lines (35 loc) · 1.1 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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:
$ 6g example.go && 6l example.6 && ./6.out
*/
import (
"fmt"
"os"
"time"
rss "github.com/dustywilson/go-pkg-rss"
)
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://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)
}