57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package data
|
|
|
|
import (
|
|
"encoding/gob"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/gorilla/feeds"
|
|
)
|
|
|
|
type Feed struct {
|
|
feeds.Feed
|
|
}
|
|
|
|
func NewFeed(title, link, desc string) *Feed {
|
|
return &Feed{
|
|
Feed: feeds.Feed{
|
|
Title: title,
|
|
Link: &feeds.Link{Href: link},
|
|
Description: desc,
|
|
},
|
|
}
|
|
}
|
|
|
|
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 := &Feed{}
|
|
decoder := gob.NewDecoder(file)
|
|
err = decoder.Decode(feed)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error decoding file %v: %v", filename, err)
|
|
}
|
|
|
|
return feed, nil
|
|
}
|
|
|
|
func (feed *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()
|
|
|
|
encoder := gob.NewEncoder(file)
|
|
err = encoder.Encode(feed)
|
|
if err != nil {
|
|
return fmt.Errorf("error encoding file %v: %v", filename, err)
|
|
}
|
|
|
|
return nil
|
|
}
|