all repos

rss-tools @ d1f2b81

get rss feed from sources that(i need and) dont provide one

rss-tools/app/atom.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
init, 1 month ago
1
package app
2
3
import (
4
	"encoding/xml"
5
	"net/http"
6
	"time"
7
)
8
9
type AtomFeed struct {
10
	XMLName xml.Name    `xml:"feed"`
11
	XMLNS   string      `xml:"xmlns,attr"`
12
	Title   string      `xml:"title"`
13
	ID      string      `xml:"id"`
14
	Updated string      `xml:"updated"`
15
	Entries []AtomEntry `xml:"entry"`
16
}
17
18
type AtomEntry struct {
19
	Title   string      `xml:"title"`
20
	ID      string      `xml:"id"`
21
	Updated string      `xml:"updated"`
22
	Content AtomContent `xml:"content"`
23
}
24
25
type AtomContent struct {
26
	XMLName xml.Name `xml:"content"`
27
	Type    string   `xml:"type,attr,omitempty"`
28
	Value   string   `xml:",chardata"`
29
}
30
31
type FeedBuilder struct {
32
	feed AtomFeed
33
}
34
35
func NewFeed(title, id string) *FeedBuilder {
36
	return &FeedBuilder{feed: AtomFeed{
37
		XMLNS:   "http://www.w3.org/2005/Atom",
38
		Title:   title,
39
		ID:      id,
40
		Updated: time.Now().Format(time.RFC3339),
41
	}}
42
}
43
44
func (f *FeedBuilder) Add(title, id, content string, date time.Time) *FeedBuilder {
45
	f.feed.Entries = append(f.feed.Entries, AtomEntry{
46
		Title:   title,
47
		ID:      id,
48
		Updated: date.Format(time.RFC3339),
49
		Content: content,
50
	})
51
	return f
52
}
53
54
func (f *FeedBuilder) Render(w http.ResponseWriter) error {
55
	w.Header().Set("Content-Type", "application/atom+xml")
56
	enc := xml.NewEncoder(w)
57
	enc.Indent("", "  ")
58
	return enc.Encode(f.feed)
59
}