2024-03-05 15:00:09 +01:00
|
|
|
package rss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-03-05 15:46:51 +01:00
|
|
|
type Content struct {
|
|
|
|
XMLName xml.Name `xml:"content:encoded"`
|
|
|
|
Value string `xml:",cdata"`
|
|
|
|
}
|
|
|
|
|
2024-03-05 15:00:09 +01:00
|
|
|
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
|
2024-03-05 15:46:51 +01:00
|
|
|
Content *Content
|
2024-03-05 15:00:09 +01:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-03-05 15:53:59 +01:00
|
|
|
item := initItem()
|
|
|
|
|
|
|
|
item.wg.Add(1)
|
|
|
|
go item.start()
|
|
|
|
item.wg.Wait()
|
|
|
|
|
|
|
|
return item
|
2024-03-05 15:00:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Item) AddCategory(category string) {
|
|
|
|
i.addCategory <- category
|
|
|
|
}
|