rss/rss.go

106 lines
2.1 KiB
Go

package rss
import (
"encoding/xml"
"fmt"
)
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"` // max: 144, default: 88
Height int `xml:"height,omitempty"` // max: 400, default: 31
}
type TextInput struct {
XMLName xml.Name `xml:"textInput"`
Title string `xml:"title,omitempty"`
Description string `xml:"description,omitempty"`
Name string `xml:"name,omitempty"`
Link string `xml:"link,omitempty"`
}
type Enclosure struct {
XMLName xml.Name `xml:"enclosure"`
Url string `xml:"url,attr"`
Type string `xml:"type,attr"`
Lenght int `xml:"lenght,attr"`
}
type Source struct {
XMLName xml.Name `xml:"source"`
Url string `xml:"url,attr"`
Value string
}
func (i *Image) check() error {
if len(i.Url) == 0 {
return fmt.Errorf("error: url not set")
}
if len(i.Title) == 0 {
return fmt.Errorf("error: title not set")
}
if len(i.Link) == 0 {
return fmt.Errorf("error: link not set")
}
if i.Width == 0 || i.Width > 144 {
i.Width = 88
}
if i.Height == 0 || i.Height > 400 {
i.Height = 31
}
return nil
}
func (t *TextInput) check() error {
if len(t.Title) == 0 &&
len(t.Description) == 0 &&
len(t.Name) == 0 &&
len(t.Link) == 0 {
return fmt.Errorf("error: textInput is empty")
}
return nil
}
func (e *Enclosure) check() error {
if len(e.Url) == 0 {
return fmt.Errorf("error: url not set")
}
if e.Lenght == 0 {
return fmt.Errorf("error: length not set or 0")
}
if len(e.Type) == 0 {
return fmt.Errorf("error: type not set")
}
return nil
}
func (s *Source) check() error {
if len(s.Url) == 0 {
return fmt.Errorf("error: url not set")
}
if len(s.Value) == 0 {
return fmt.Errorf("error: value not set")
}
return nil
}
func (f *Feed) ToXML() (string, error) {
if err := f.check(); err != nil {
return "", fmt.Errorf("error checking RSS feed: %v", err)
}
xml, err := xml.MarshalIndent(f, "", " ")
if err != nil {
return "", fmt.Errorf("error XML encoding feed: %v", err)
}
return string(xml), nil
}