syndfeed
is a Go library for RSS 2.0 and Atom 1.0 feeds, supported implement extension module to parse any RSS and Atom extension element.
feed, _ := syndfeed.LoadURL("https://cn.engadget.com/rss.xml")
fmt.Println(feed.Title)
feedData := `<rss version="2.0">
<channel>
<title>Sample Feed</title>
</channel>
</rss>`
feed, _ := syndfeed.Parse(strings.NewReader(feedData))
fmt.Println(feed.Title)
feedData := `<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Atom</title>
</feed>`
feed, _ := syndfeed.ParseAtom(strings.NewReader(feedData))
fmt.Println(feed.Title)
feedData := `<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<dc:creator>[email protected] (Example Name)</dc:creator>
</channel>`
feed, _ := syndfeed.ParseRSS(strings.NewReader(feedData))
fmt.Println(feed.Authors[0].Name)
In some syndication feeds, they have some valid XML elements but are not specified in either the Atom 1.0 or RSS 2.0 specifications. You can add extension module to process these elements.
The syndfeed
build-in supported following modules: Dublin Core, Content, Syndication.
You can implement your own modules to parse any extension element like: iTunes RSS, Media RSS.
iTunesHandler := func(n *xmlquery.Node, v interface{}) {
item := v.(*syndfeed.Item)
switch n.Data {
case "artist":
item.Authors = append(item.Authors, &syndfeed.Person{Name: n.InnerText()})
case "releaseDate":
item.PublishDate = ParseDate(n.InnerText())
}
}
// Register a new module to the syndfeed module list.
syndfeed.RegisterExtensionModule("https://rss.itunes.apple.com", syndfeed.ModuleHandlerFunc(iTunesHandler))
// Now can parse iTunes feed.
feed, err := syndfeed.LoadURL("https://github.com/zhengchun/syndfeed/blob/master/_samples/itunes.atom")
fmt.Println(feed.Items[0].Authors[0].Name)
// Output author name.
- Add RSS/Atom format output.