diff --git a/feed.go b/feed.go index 333f204..7881a9a 100644 --- a/feed.go +++ b/feed.go @@ -1,8 +1,10 @@ package rss import ( + "encoding/gob" "encoding/xml" "fmt" + "os" "sync" ) @@ -30,10 +32,6 @@ func initFeed() *Feed { ContentNamespace: "http://purl.org/rss/1.0/modules/content/", } - feed.wg.Add(1) - go feed.start() - feed.wg.Wait() - return feed } @@ -52,9 +50,48 @@ func (f *Feed) check() error { } func NewFeed() *Feed { - return initFeed() + feed := initFeed() + + feed.wg.Add(1) + go feed.start() + feed.wg.Wait() + + return feed } func (f *Feed) AddChannel(c *Channel) { f.addChannel <- c } + +func (f *Feed) Save(filename string) error { + file, err := os.Create(filename) + if err != nil { + return fmt.Errorf("error creating file %v: %v", filename, err) + } + defer file.Close() + + if err := gob.NewEncoder(file).Encode(f); err != nil { + return fmt.Errorf("error encoding feed in file %v: %v", filename, err) + } + + return nil +} + +func OpenFeed(filename string) (*Feed, error) { + file, err := os.Open(filename) + if err != nil { + return nil, fmt.Errorf("error opening file %v: %v", filename, err) + } + defer file.Close() + + feed := initFeed() + feed.wg.Add(1) + go feed.start() + feed.wg.Wait() + + if err := gob.NewDecoder(file).Decode(feed); err != nil { + return nil, fmt.Errorf("error decoding file %v: %v", filename, err) + } + + return nil, nil +} diff --git a/item.go b/item.go index 142e63e..689d73b 100644 --- a/item.go +++ b/item.go @@ -7,6 +7,11 @@ import ( "time" ) +type Content struct { + XMLName xml.Name `xml:"content:encoded"` + Value string `xml:",cdata"` +} + type Item struct { addCategory chan string XMLName xml.Name `xml:"item"` @@ -19,6 +24,7 @@ type Item struct { Guid string `xml:"guid,omitempty"` PubDate string `xml:"pubDate,omitempty"` Source *Source + Content *Content Categories []string `xml:"category,omitempty"` wg sync.WaitGroup }