Merge pull request #61 from charl/master

Added an example of using a custom charset reader to work around specific charset issues
This commit is contained in:
Jim Teeuwen 2015-03-30 12:13:52 +02:00
commit 5576b8d83e
1 changed files with 21 additions and 6 deletions

27
testdata/example.go vendored
View File

@ -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)
}