Documentation
¶
Overview ¶
Package rss2 let's you parse, modify, create and render RSS 2.0 feeds. Specification was taken from https://cyber.harvard.edu/rss/rss.html .
Example (ParseRSS) ¶
This example shows how you can parse a simple RSS 2.0 feed. The user must ensure that the input is UTF-8 encoded when parsing.
package main
import (
"encoding/xml"
"fmt"
"github.com/codesoap/rss2"
)
func main() {
input := `
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Channel title</title>
<link>channel.link.net</link>
<description>Channel description</description>
<category>Channel's category</category>
<item>
<title>Title of my RSS item</title>
<pubDate>03 Jun 2019 09:39:21 GMT</pubDate>
</item>
</channel>
</rss>`
var parse rss2.RSS
// Remember to handle any error xml.Unmarshal() gives in your code.
xml.Unmarshal([]byte(input), &parse)
fmt.Println(`RSS Version: `, parse.Version)
fmt.Println(`RSS channel's title:`, parse.Channel.Title)
fmt.Println(`RSS item's title: `, parse.Channel.Items[0].Title)
// The date of any date fields is hidden behind .Time for technical reasons.
fmt.Println(`RSS item's pubDate: `, parse.Channel.Items[0].PubDate.Time)
}
Output:
Example (RenderRSS) ¶
This example shows how you can create a simple RSS 2.0 feed. Using the New<element>() functions to create RSS elements helps to avoid invalid RSS.
package main
import (
"encoding/xml"
"fmt"
"time"
"github.com/codesoap/rss2"
)
func main() {
item, _ := rss2.NewItem(`Title of my RSS item`, ``)
item.PubDate = &rss2.RSSTime{time.Date(2019, 6, 3, 9, 39, 21, 0, time.UTC)}
category, _ := rss2.NewCategory(`MSFT`)
category.Domain = `http://www.fool.com/cusips`
channel, _ := rss2.NewChannel(`Channel title`, `channel.link.net`,
`Channel description`)
channel.Categories = []*rss2.Category{category}
channel.Items = []*rss2.Item{item}
// All created RSS elements are structs with XML tags.
// Thus xml.Marshal() and the likes can be used.
rss, _ := xml.MarshalIndent(rss2.NewRSS(channel), ``, ` `)
fmt.Println(xml.Header + string(rss))
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Category ¶
type Category struct {
XMLName xml.Name `xml:"category"`
Value string `xml:",chardata"`
Domain string `xml:"domain,attr,omitempty"`
}
Category represents a Channel's or Item's category. Domain is optional.
func NewCategory ¶
NewCategory creates a new Category.
type Channel ¶
type Channel struct {
XMLName xml.Name `xml:"channel"`
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Language string `xml:"language,omitempty"`
Copyright string `xml:"copyright,omitempty"`
ManagingEditor string `xml:"managingEditor,omitempty"`
WebMaster string `xml:"webMaster,omitempty"`
PubDate *RSSTime `xml:"pubDate,omitempty"`
LastBuildDate *RSSTime `xml:"lastBuildDate,omitempty"`
Categories []*Category `xml:"category,omitempty"`
Generator string `xml:"generator,omitempty"`
Docs string `xml:"docs,omitempty"`
Cloud *Cloud `xml:"cloud,omitempty"`
TTL int `xml:"ttl,omitempty"`
Image *Image `xml:"image,omitempty"`
Rating string `xml:"rating,omitempty"`
TextInput *TextInput `xml:"textInput,omitempty"`
SkipHours *SkipHours `xml:"skipHours,omitempty"`
SkipDays *SkipDays `xml:"skipDays,omitempty"`
Items []*Item `xml:"item,omitempty"`
}
Channel represents an rss channel element. Title, Link and Description are required.
func NewChannel ¶
NewChannel creates a new Channel.
type Cloud ¶
type Cloud struct {
XMLName xml.Name `xml:"cloud"`
Domain string `xml:"domain,attr"`
Port int `xml:"port,attr"`
Path string `xml:"path,attr"`
RegisterProcedure string `xml:"registerProcedure,attr"`
Protocol string `xml:"protocol,attr"`
}
Cloud represents a Channel's cloud element. All attributes must be present.
type Enclosure ¶
type Enclosure struct {
XMLName xml.Name `xml:"enclosure"`
URL string `xml:"url,attr"`
Length int `xml:"length,attr"`
Type string `xml:"type,attr"`
}
Enclosure represents an Item's enclosure element. All attributes must be present. Type must be a MIME type.
type GUID ¶
type GUID struct {
XMLName xml.Name `xml:"guid"`
Value string `xml:",chardata"`
IsPermaLink bool `xml:"isPermaLink,attr"`
}
GUID represents a Channel's guid element. sPermaLink is optional.
type Image ¶
type Image struct {
XMLName xml.Name `xml:"image"`
URL string `xml:"url"`
Title string `xml:"title"`
Link string `xml:"link"`
Width int `xml:"width,omitempty"`
Height int `xml:"height,omitempty"`
Description string `xml:"description,omitempty"`
}
Image represents a Channel's image. URL, Title and Link must be present. Width must not exceed 144. Height must not exceed 400.
type Item ¶
type Item struct {
XMLName xml.Name `xml:"item"`
Title string `xml:"title,omitempty"`
Link string `xml:"link,omitempty"`
Description string `xml:"description,omitempty"`
Author string `xml:"author,omitempty"`
Categories []*Category `xml:"category,omitempty"`
Comments string `xml:"comments,omitempty"`
Enclosure *Enclosure `xml:"enclosure,omitempty"`
GUID *GUID `xml:"guid,omitempty"`
PubDate *RSSTime `xml:"pubDate,omitempty"`
Source *Source `xml:"source,omitempty"`
}
Item represents an rss item. At least Title or Description must be present.
type RSS ¶
type RSS struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channel *Channel `xml:"channel"`
}
RSS represents an rss feed. All fields are mandatory. Version must be "2.0" for this library.
type RSSTime ¶
RSSTime is a wrapper around time.Time, that makes it possible to define custom MarshalXML() and UnmarshalXML() functions.
func ParseRSSTime ¶
ParseRSSTime parses a time as specified in RFC822, with the difference, that four digit years are allowed.
func (RSSTime) MarshalXML ¶
MarshalXML marshals an RSSTime element.
func (*RSSTime) UnmarshalXML ¶
UnmarshalXML unmarshals an RSSTime element.
type SkipDays ¶
SkipDays represents a Channel's skipDays element.
func NewSkipDays ¶
NewSkipDays creates a new SkipDays element.
type SkipHours ¶
SkipHours represents a Channel's skipHours element. Hours must be between 0 and 23.
func NewSkipHours ¶
NewSkipHours creates a new SkipHours element. Hours must be between 0 and 23.
type Source ¶
type Source struct {
XMLName xml.Name `xml:"source"`
Value string `xml:",chardata"`
URL string `xml:"url,attr"`
}
Source represents an Item's source element. URL must be present.
type TextInput ¶
type TextInput struct {
XMLName xml.Name `xml:"textInput"`
Title string `xml:"title"`
Description string `xml:"description"`
Name string `xml:"name"`
Link string `xml:"link"`
}
TextInput represents a Channel's textInput element. All sub-elements must be present.
func NewTextInput ¶
NewTextInput creates a new TextInput element.