-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
62 lines (52 loc) · 1.28 KB
/
feed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/xml"
"fmt"
"strings"
"time"
)
type Feed struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channel Channel `xml:"channel"`
}
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
LastBuildDate string `xml:"lastBuildDate"`
Items []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
}
func MakeFeed(index *Index) (string, error) {
feed := Feed{
Version: "2.0",
Channel: Channel{
Title: TITLE,
Link: HOST,
LastBuildDate: time.Now().Format(time.RFC1123),
},
}
for _, t := range index.Titles {
pub, err := time.Parse("2006-01-02", strings.Split(t, " ")[0])
if err != nil {
return "", fmt.Errorf("error parsing post timestamp: %w", err)
}
item := Item{
Title: t,
Link: HOST + "/posts/" + t,
PubDate: pub.Format(time.RFC1123),
}
feed.Channel.Items = append(feed.Channel.Items, item)
}
out, err := xml.MarshalIndent(feed, "", "\t")
if err != nil {
return "", fmt.Errorf("error marshalling feed: %w", err)
}
return xml.Header + string(out), nil
}