rss

package module
v0.0.0-...-f930f01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2023 License: MIT Imports: 5 Imported by: 1

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

func NewCategory(value string) (*Category, error)

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

func NewChannel(title, link, description string) (*Channel, error)

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.

func NewCloud

func NewCloud(domain string, port int, path, rp, protocol string) (*Cloud, error)

NewCloud creates a new Cloud element.

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.

func NewEnclosure

func NewEnclosure(url string, length int, t string) (*Enclosure, error)

NewEnclosure creates a new Enclosure element.

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.

func NewGUID

func NewGUID(value string, permalink bool) (*GUID, error)

NewGUID creates a new GUID element.

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.

func NewImage

func NewImage(url, title, link string) (*Image, error)

NewImage creates new Image.

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.

func NewItem

func NewItem(title, description string) (*Item, error)

NewItem creates a new Item. Either title or description may be empty.

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 New

func New() *RSS

New creates a new RSS element.

func (*RSS) Compose

func (r *RSS) Compose(ch *Channel, xmlHeader bool) (string, error)
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

func (r *RSS) Parse(in string) error
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&#39;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

type SkipDays struct {
	XMLName xml.Name `xml:"skipDays"`
	Days    []string `xml:"day"`
}

SkipDays represents a Channel's skipDays element.

func NewSkipDays

func NewSkipDays(days []time.Weekday) *SkipDays

NewSkipDays creates a new SkipDays element.

type SkipHours

type SkipHours struct {
	XMLName xml.Name `xml:"skipHours"`
	Hours   []int    `xml:"hour"`
}

SkipHours represents a Channel's skipHours element. Hours must be between 0 and 23.

func NewSkipHours

func NewSkipHours(hours []int) (*SkipHours, error)

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.

func NewSource

func NewSource(value, url string) (*Source, error)

NewSource creates a new Source element.

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

func NewTextInput(title, description, name, link string) (*TextInput, error)

NewTextInput creates a new TextInput element.

type Time

type Time struct {
	Time time.Time
}

Time is a wrapper around time.Time, that makes it possible to define custom MarshalXML() and UnmarshalXML() functions.

func ParseTime

func ParseTime(in string) (out Time, err error)

ParseTime parses a time as specified in RFC822, with the difference, that four digit years are allowed.

func (Time) MarshalXML

func (t Time) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals an Time element.

func (*Time) UnmarshalXML

func (t *Time) UnmarshalXML(decoder *xml.Decoder,
	start xml.StartElement) (err error)

UnmarshalXML unmarshals an Time element.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL