rss/item.go

69 lines
1.4 KiB
Go
Raw Normal View History

package rss
import (
"encoding/xml"
"fmt"
"sync"
"time"
)
type Item struct {
addCategory chan string
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"`
Comments string `xml:"comments,omitempty"`
Enclosure *Enclosure
Guid string `xml:"guid,omitempty"`
PubDate string `xml:"pubDate,omitempty"`
Source *Source
Categories []string `xml:"category,omitempty"`
wg sync.WaitGroup
}
func (i *Item) start() {
i.wg.Done()
for category := range i.addCategory {
i.Categories = append(i.Categories, category)
}
}
func initItem() *Item {
item := &Item{
addCategory: make(chan string),
PubDate: time.Now().Format(time.RFC1123Z),
}
item.wg.Add(1)
go item.start()
item.wg.Wait()
return item
}
func (i *Item) check() error {
if len(i.Title) == 0 && len(i.Description) == 0 {
return fmt.Errorf("error: neither title nor description set")
}
if err := i.Enclosure.check(); err != nil {
return fmt.Errorf("error checking enclosure: %v", err)
}
if err := i.Source.check(); err != nil {
return fmt.Errorf("error checking source: %v", err)
}
return nil
}
func NewItem() *Item {
return initItem()
}
func (i *Item) AddCategory(category string) {
i.addCategory <- category
}