forked from mna/trofaf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.go
More file actions
106 lines (94 loc) · 2.57 KB
/
rss.go
File metadata and controls
106 lines (94 loc) · 2.57 KB
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
104
105
106
package main
// Adapted from https://github.com/krautchan/gbt
//
// "THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
// <whoami@dev-urandom.eu> wrote these files. As long as you retain this notice
// you can do whatever you want with this stuff. If we meet some day, and you think
// this stuff is worth it, you can buy me a pizza in return.
/*
Package to parse and create RSS-Feeds
*/
import (
"encoding/xml"
"os"
"time"
)
// The root Rss structure
type Rss struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
Channels []*Channel `xml:"channel"`
}
// The Rss channel structure
type Channel struct {
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
LastBuildDate string `xml:"lastBuildDate"`
Generator string `xml:"generator"`
Image []*Image `xml:"image"`
Item []*Item `xml:"item"`
}
// The rss image structure
type Image struct {
Url string `xml:"url"`
Title string `xml:"title"`
Link string `xml:"link"`
}
// The Rss item structure
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Author string `xml:"author"`
Category string `xml:"category"`
PubDate string `xml:"pubDate"`
Image []*Image `xml:"image"`
}
// Create a new RSS feed
func NewRss(title string, description string, link string) *Rss {
rss := &Rss{Version: "2.0",
Channels: []*Channel{
&Channel{
Title: title,
Description: description,
Link: link,
Generator: "trofaf (https://github.com/PuerkitoBio/trofaf)",
Image: make([]*Image, 0),
Item: make([]*Item, 0),
},
},
}
return rss
}
// Create a new, orphan Rss Item.
func NewRssItem(title, link, description, author, category string, pubTime time.Time) *Item {
return &Item{
Title: title,
Link: link,
Description: description,
Author: author,
Category: category,
PubDate: pubTime.Format(time.RFC822),
Image: make([]*Image, 0),
}
}
// Add an Item to the feed, under this Channel
func (ch *Channel) AppendItem(i *Item) {
ch.Item = append(ch.Item, i)
}
// Writes the data in RSS 2.0 format to a given file
func (rss *Rss) WriteToFile(path string) error {
rss.Channels[0].LastBuildDate = time.Now().Format(time.RFC822)
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(xml.Header)
if err != nil {
return err
}
enc := xml.NewEncoder(file)
return enc.Encode(rss)
}