Documentation ¶
Overview ¶
Package rss let's you parse, modify, create and render RSS 2.0 feeds. Specification was taken from https://cyber.harvard.edu/rss/rss.html .
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 *Time `xml:"pubDate,omitempty"` LastBuildDate *Time `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 *Time `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.
func (*RSS) Compose ¶
Example ¶
This example shows how you can create a simple RSS 2.0 feed.
package main import ( "fmt" "time" "catinello.eu/rss" ) func main() { item, _ := rss.NewItem(`Title of my RSS item`, ``) item.PubDate = &rss.Time{time.Date(2019, 6, 3, 9, 39, 21, 0, time.UTC)} category, _ := rss.NewCategory(`MSFT`) category.Domain = `http://example.com/` channel, _ := rss.NewChannel(`Channel title`, `channel.link.net`, `Channel description`) channel.Categories = []*rss.Category{category} channel.Items = []*rss.Item{item} r := rss.New() out, err := r.Compose(channel, true) if err != nil { panic(err) } fmt.Println(out) }
Output:
func (*RSS) Parse ¶
Example ¶
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 ( "fmt" "catinello.eu/rss" ) 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>` r := rss.New() err := r.Parse(input) if err != nil { panic(err) } fmt.Println(`RSS Version: `, r.Version) fmt.Println(`RSS channel's title:`, r.Channel.Title) fmt.Println(`RSS item's title: `, r.Channel.Items[0].Title) // The date of any date fields is hidden behind .Time for technical reasons. fmt.Println(`RSS item's pubDate: `, r.Channel.Items[0].PubDate.Time) }
Output:
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.
type Time ¶
Time is a wrapper around time.Time, that makes it possible to define custom MarshalXML() and UnmarshalXML() functions.
func ParseTime ¶
ParseTime parses a time as specified in RFC822, with the difference, that four digit years are allowed.
func (Time) MarshalXML ¶
MarshalXML marshals an Time element.
func (*Time) UnmarshalXML ¶
UnmarshalXML unmarshals an Time element.