Add saving and loading

This commit is contained in:
Jason Streifling 2024-03-05 15:46:51 +01:00
parent a59d3f1128
commit 88b3d7e159
2 changed files with 48 additions and 5 deletions

47
feed.go
View File

@ -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
}

View File

@ -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
}