rss/feed.go

61 lines
1.0 KiB
Go

package rss
import (
"encoding/xml"
"fmt"
"sync"
)
type Feed struct {
addChannel chan *Channel
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
ContentNamespace string `xml:"xmlns:content,attr"`
Channels []*Channel
wg sync.WaitGroup
}
func (f *Feed) start() {
f.wg.Done()
for channel := range f.addChannel {
f.Channels = append(f.Channels, channel)
}
}
func initFeed() *Feed {
feed := &Feed{
addChannel: make(chan *Channel),
Version: "2.0",
ContentNamespace: "http://purl.org/rss/1.0/modules/content/",
}
feed.wg.Add(1)
go feed.start()
feed.wg.Wait()
return feed
}
func (f *Feed) check() error {
if f.Channels == nil {
return fmt.Errorf("error: feed has no channels")
}
for _, channel := range f.Channels {
if err := channel.check(); err != nil {
return fmt.Errorf("error checking channel: %v", err)
}
}
return nil
}
func NewFeed() *Feed {
return initFeed()
}
func (f *Feed) AddChannel(c *Channel) {
f.addChannel <- c
}