105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
|
package rss
|
||
|
|
||
|
import (
|
||
|
"encoding/xml"
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Channel struct {
|
||
|
addCategory chan string
|
||
|
addItem chan *Item
|
||
|
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 string `xml:"pubDate,omitempty"`
|
||
|
LastBuildDate string `xml:"lastBuildDate,omitempty"`
|
||
|
Categories []string `xml:"category,omitempty"`
|
||
|
Generator string `xml:"generator,omitempty"`
|
||
|
Docs string `xml:"docs,omitempty"`
|
||
|
Cloud string `xml:"cloud,omitempty"`
|
||
|
Image *Image
|
||
|
Rating string `xml:"rating,omitempty"`
|
||
|
TextInput *TextInput
|
||
|
SkipHours []int `xml:"skipHours,omitempty"`
|
||
|
SkipDays []int `xml:"skipDays,omitempty"`
|
||
|
Items []*Item
|
||
|
Ttl int `xml:"ttl,omitempty"`
|
||
|
wg sync.WaitGroup
|
||
|
}
|
||
|
|
||
|
func (c *Channel) start() {
|
||
|
c.wg.Done()
|
||
|
|
||
|
for {
|
||
|
select {
|
||
|
case category := <-c.addCategory:
|
||
|
c.Categories = append(c.Categories, category)
|
||
|
case item := <-c.addItem:
|
||
|
c.Items = append(c.Items, item)
|
||
|
}
|
||
|
|
||
|
c.LastBuildDate = time.Now().Format(time.RFC1123Z)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func initChannel() *Channel {
|
||
|
now := time.Now().Format(time.RFC1123Z)
|
||
|
|
||
|
channel := &Channel{
|
||
|
PubDate: now,
|
||
|
LastBuildDate: now,
|
||
|
}
|
||
|
|
||
|
channel.wg.Add(1)
|
||
|
go channel.start()
|
||
|
channel.wg.Wait()
|
||
|
|
||
|
return channel
|
||
|
}
|
||
|
|
||
|
func (c *Channel) check() error {
|
||
|
if len(c.Title) == 0 {
|
||
|
return fmt.Errorf("error: title not set")
|
||
|
}
|
||
|
if len(c.Link) == 0 {
|
||
|
return fmt.Errorf("error: link not set")
|
||
|
}
|
||
|
if len(c.Description) == 0 {
|
||
|
return fmt.Errorf("error: description not set")
|
||
|
}
|
||
|
|
||
|
if err := c.Image.check(); err != nil {
|
||
|
return fmt.Errorf("error checking image: %v", err)
|
||
|
}
|
||
|
if err := c.TextInput.check(); err != nil {
|
||
|
return fmt.Errorf("error checking textInput: %v", err)
|
||
|
}
|
||
|
|
||
|
for _, item := range c.Items {
|
||
|
if err := item.check(); err != nil {
|
||
|
return fmt.Errorf("error checking item: %v", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NewChannel() *Channel {
|
||
|
return initChannel()
|
||
|
}
|
||
|
|
||
|
func (c *Channel) AddCategory(category string) {
|
||
|
c.addCategory <- category
|
||
|
}
|
||
|
|
||
|
func (c *Channel) AddItem(i *Item) {
|
||
|
c.addItem <- i
|
||
|
}
|