-
Notifications
You must be signed in to change notification settings - Fork 203
/
rss.go
103 lines (96 loc) · 2.54 KB
/
rss.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package templates
import (
"bytes"
"github.com/gorilla/feeds"
"github.com/kabukky/journey/database"
"github.com/kabukky/journey/structure"
"net/http"
"time"
)
func ShowIndexRss(writer http.ResponseWriter) error {
// 15 posts in rss for now
posts, err := database.RetrievePostsForIndex(15, 0)
if err != nil {
return err
}
blog, err := database.RetrieveBlog()
if err != nil {
return err
}
blogData := &structure.RequestData{Posts: posts, Blog: blog}
feed := createFeed(blogData)
err = feed.WriteRss(writer)
return err
}
func ShowTagRss(writer http.ResponseWriter, slug string) error {
tag, err := database.RetrieveTagBySlug(slug)
if err != nil {
return err
}
// 15 posts in rss for now
posts, err := database.RetrievePostsByTag(tag.Id, 15, 0)
if err != nil {
return err
}
blog, err := database.RetrieveBlog()
if err != nil {
return err
}
blogData := &structure.RequestData{Posts: posts, Blog: blog}
feed := createFeed(blogData)
err = feed.WriteRss(writer)
return err
}
func ShowAuthorRss(writer http.ResponseWriter, slug string) error {
author, err := database.RetrieveUserBySlug(slug)
if err != nil {
return err
}
// 15 posts in rss for now
posts, err := database.RetrievePostsByUser(author.Id, 15, 0)
if err != nil {
return err
}
blog, err := database.RetrieveBlog()
if err != nil {
return err
}
blogData := &structure.RequestData{Posts: posts, Blog: blog}
feed := createFeed(blogData)
err = feed.WriteRss(writer)
return err
}
func createFeed(values *structure.RequestData) *feeds.Feed {
now := time.Now()
feed := &feeds.Feed{
Title: string(makeCdata(values.Blog.Title)),
Description: string(makeCdata(values.Blog.Description)),
Link: &feeds.Link{Href: string(values.Blog.Url)},
Created: now,
}
for i := 0; i < len(values.Posts); i++ {
if values.Posts[i].Id != 0 {
// Make link
var buffer bytes.Buffer
buffer.Write(values.Blog.Url)
buffer.WriteString("/")
buffer.WriteString(values.Posts[i].Slug)
feed.Items = append(feed.Items, &feeds.Item{
Title: string(makeCdata(values.Posts[i].Title)),
Description: string(makeCdata(values.Posts[i].Html)),
Link: &feeds.Link{Href: buffer.String()},
Id: string(values.Posts[i].Uuid),
Author: &feeds.Author{Name: string(values.Posts[i].Author.Name), Email: ""},
Created: *values.Posts[i].Date,
})
}
}
return feed
}
func makeCdata(input []byte) []byte {
var buffer bytes.Buffer
buffer.WriteString("<![CDATA[")
buffer.Write(input)
buffer.WriteString("]]>")
return buffer.Bytes()
}