cpolis/cmd/feed/rss.go

71 lines
1.2 KiB
Go
Raw Normal View History

2024-02-18 12:41:49 +01:00
package feed
import (
"encoding/gob"
2024-02-18 12:41:49 +01:00
"fmt"
"os"
2024-02-18 12:41:49 +01:00
"time"
"github.com/gorilla/feeds"
)
type Feed struct {
feeds.Feed
2024-02-18 12:41:49 +01:00
}
func NewFeed(title, link, desc string) *Feed {
return &Feed{
Feed: feeds.Feed{
2024-02-18 12:41:49 +01:00
Title: title,
Link: &feeds.Link{Href: link},
Description: desc,
},
}
}
func SaveFeed(feed *Feed, filename string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error cmd/feed/rss.go WriteFeed os.Create(): %v", err)
}
defer file.Close()
encoder := gob.NewEncoder(file)
encoder.Encode(feed)
return nil
}
func OpenFeed(filename string) (*Feed, error) {
var feed *Feed
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error cmd/feed/rss.go ReadFeed os.Open(): %v", err)
}
defer file.Close()
decoder := gob.NewDecoder(file)
decoder.Decode(feed)
return feed, nil
}
func AddToFeed(feed *Feed, title, desc, content string) error {
2024-02-18 12:41:49 +01:00
item := feeds.Item{
Title: title,
Created: time.Now(),
Description: desc,
Content: content,
2024-02-18 12:41:49 +01:00
}
feed.Add(&item)
rss, err := feed.ToRss()
if err != nil {
return fmt.Errorf("error cmd/feed/rss.go AddToFeed feed.ToRss(): %v", err)
}
fmt.Println(rss)
return nil
}