From 6dcb8a2f3fe902c93c7577a8186a8c81c16fdf14 Mon Sep 17 00:00:00 2001 From: Charl Matthee Date: Mon, 30 Mar 2015 11:24:44 +0200 Subject: [PATCH] * Added an example of using a custom charset reader to work around specific charset issues. --- testdata/example.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/testdata/example.go b/testdata/example.go index be3ef5b..6d1343a 100644 --- a/testdata/example.go +++ b/testdata/example.go @@ -11,24 +11,32 @@ Build & run with: */ import ( + "errors" "fmt" - rss "github.com/jteeuwen/go-pkg-rss" + "io" "os" "time" + + rss "github.com/jteeuwen/go-pkg-rss" + "github.com/jteeuwen/go-pkg-xmlx" ) 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) + // Invoking it with 'go PollFeed(...)' to have the polling performed in a + // separate goroutine, so we can poll mutiple feeds. + go PollFeed("http://blog.case.edu/news/feed.atom", 5, nil) + + // Poll with a custom charset reader. This is to avoid the following error: + // ... xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nil. + PollFeed("https://status.rackspace.com/index/rss", 5, charsetReader) } -func PollFeed(uri string, timeout int) { +func PollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { feed := rss.New(timeout, true, chanHandler, itemHandler) for { - if err := feed.Fetch(uri, nil); err != nil { + if err := feed.Fetch(uri, cr); err != nil { fmt.Fprintf(os.Stderr, "[e] %s: %s", uri, err) return } @@ -44,3 +52,10 @@ func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) { func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) { fmt.Printf("%d new item(s) in %s\n", len(newitems), feed.Url) } + +func charsetReader(charset string, r io.Reader) (io.Reader, error) { + if charset == "ISO-8859-1" || charset == "iso-8859-1" { + return r, nil + } + return nil, errors.New("Unsupported character set encoding: " + charset) +}