owl-blogs/rss.go

66 lines
1.3 KiB
Go
Raw Normal View History

2022-08-13 13:32:26 +00:00
package owl
import (
"bytes"
"encoding/xml"
2022-09-11 15:25:26 +00:00
"time"
2022-08-13 13:32:26 +00:00
)
type RSS struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channel RSSChannel `xml:"channel"`
}
type RSSChannel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Items []RSSItem `xml:"item"`
}
type RSSItem struct {
2022-10-14 19:06:26 +00:00
Guid string `xml:"guid"`
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Description string `xml:"description"`
2022-08-13 13:32:26 +00:00
}
func RenderRSSFeed(user User) (string, error) {
2022-11-03 19:25:53 +00:00
config := user.Config()
2022-08-13 13:32:26 +00:00
rss := RSS{
Version: "2.0",
Channel: RSSChannel{
Title: config.Title,
2022-08-13 16:47:27 +00:00
Link: user.FullUrl(),
2022-08-13 13:32:26 +00:00
Description: config.SubTitle,
Items: make([]RSSItem, 0),
},
}
posts, _ := user.PrimaryFeedPosts()
2022-08-17 19:57:55 +00:00
for _, post := range posts {
meta := post.Meta()
2022-10-14 19:06:26 +00:00
content, _ := renderPostContent(post)
2022-08-13 16:47:27 +00:00
rss.Channel.Items = append(rss.Channel.Items, RSSItem{
2022-10-14 19:06:26 +00:00
Guid: post.FullUrl(),
Title: post.Title(),
Link: post.FullUrl(),
PubDate: meta.Date.Format(time.RFC1123Z),
Description: content,
2022-08-13 16:47:27 +00:00
})
}
2022-08-13 13:32:26 +00:00
buf := new(bytes.Buffer)
err := xml.NewEncoder(buf).Encode(rss)
if err != nil {
return "", err
}
return xml.Header + buf.String(), nil
}