* Added an example of using a custom charset reader to work around specific charset issues.
This commit is contained in:
parent
28a9b88c87
commit
6dcb8a2f3f
|
@ -11,24 +11,32 @@ Build & run with:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
rss "github.com/jteeuwen/go-pkg-rss"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
rss "github.com/jteeuwen/go-pkg-rss"
|
||||||
|
"github.com/jteeuwen/go-pkg-xmlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// This sets up a new feed and polls it for new channels/items.
|
// 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
|
// Invoking it with 'go PollFeed(...)' to have the polling performed in a
|
||||||
// separate goroutine, so you can continue with the rest of your program.
|
// separate goroutine, so we can poll mutiple feeds.
|
||||||
PollFeed("http://blog.case.edu/news/feed.atom", 5)
|
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)
|
feed := rss.New(timeout, true, chanHandler, itemHandler)
|
||||||
|
|
||||||
for {
|
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)
|
fmt.Fprintf(os.Stderr, "[e] %s: %s", uri, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -44,3 +52,10 @@ func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
|
||||||
func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
|
func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
|
||||||
fmt.Printf("%d new item(s) in %s\n", len(newitems), feed.Url)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue