-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeds.go
80 lines (66 loc) · 1.42 KB
/
feeds.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"os"
"strings"
"time"
"github.com/gorilla/feeds"
)
type Blog struct {
Title string
Date string
Text string
}
func main() {
feed := &feeds.Feed{
Title: "j45's Blog",
Link: &feeds.Link{Href: "https://j45.dev/blog/"},
Description: "j45's Blog",
Author: &feeds.Author{Name: "j45"},
Created: time.Now(),
}
var blogs []Blog
var feedItems []*feeds.Item
dat, err := os.ReadFile("blogs.csv")
if err != nil {
panic(err)
}
lines := strings.Split(string(dat), "\n")
lines = lines[1:] // first line is the layout so remove it
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
fields := strings.Split(line, "|")
if len(fields) < 3 { // invalid line
continue
}
blog := Blog{
Title: fields[0],
Text: fields[2],
}
blogs = append(blogs, blog)
}
for _, blog := range blogs {
feedItems = append(feedItems,
&feeds.Item{
Title: blog.Title,
Link: &feeds.Link{Href: "https://j45.dev/blog/post_" + strings.ToLower(strings.ReplaceAll(blog.Title, " ", "_")) + ".html"},
Description: blog.Text,
})
}
feed.Items = feedItems
rssFeed, err := feed.ToRss()
if err != nil {
panic(err)
}
err = os.WriteFile("feed.rss", []byte(rssFeed), 0644)
if err != nil {
panic(err)
}
jsonFeed, err := feed.ToJSON()
if err != nil {
panic(err)
}
err = os.WriteFile("feed.json", []byte(jsonFeed), 0644)
if err != nil {
panic(err)
}
}