rss.go (view raw)
1//
2// Copyright (c) 2018 Ted Unangst <tedu@tedunangst.com>
3//
4// Permission to use, copy, modify, and distribute this software for any
5// purpose with or without fee is hereby granted, provided that the above
6// copyright notice and this permission notice appear in all copies.
7//
8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16package main
17
18import (
19 "encoding/xml"
20 "io"
21)
22
23type Rss struct {
24 XMLName xml.Name `xml:"rss"`
25 Version string `xml:"version,attr"`
26 Feed *RssFeed
27}
28
29type RssFeed struct {
30 XMLName xml.Name `xml:"channel"`
31 Title string `xml:"title"`
32 Link string `xml:"link"`
33 Description string `xml:"description"`
34 FeedImage *RssFeedImage
35 Items []*RssItem
36}
37
38type RssFeedImage struct {
39 XMLName xml.Name `xml:"image"`
40 URL string `xml:"url"`
41 Title string `xml:"title"`
42 Link string `xml:"link"`
43}
44
45type RssItem struct {
46 XMLName xml.Name `xml:"item"`
47 Title string `xml:"title"`
48 Description RssCData `xml:"description"`
49 Link string `xml:"link"`
50 PubDate string `xml:"pubDate"`
51}
52
53type RssCData struct {
54 Data string `xml:",cdata"`
55}
56
57func (fd *RssFeed) Write(w io.Writer) error {
58 r := Rss{Version: "2.0", Feed: fd}
59 io.WriteString(w, xml.Header)
60 enc := xml.NewEncoder(w)
61 enc.Indent("", " ")
62 err := enc.Encode(r)
63 io.WriteString(w, "\n")
64 return err
65}