feed.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package feeds
  2. import (
  3. "encoding/json"
  4. "encoding/xml"
  5. "io"
  6. "sort"
  7. "time"
  8. )
  9. type Link struct {
  10. Href, Rel, Type, Length string
  11. }
  12. type Author struct {
  13. Name, Email string
  14. }
  15. type Image struct {
  16. Url, Title, Link string
  17. Width, Height int
  18. }
  19. type Enclosure struct {
  20. Url, Length, Type string
  21. }
  22. type Item struct {
  23. Title string
  24. Link *Link
  25. Source *Link
  26. Author *Author
  27. Description string // used as description in rss, summary in atom
  28. Id string // used as guid in rss, id in atom
  29. IsPermaLink string // an optional parameter for guid in rss
  30. Updated time.Time
  31. Created time.Time
  32. Enclosure *Enclosure
  33. Content string
  34. }
  35. type Feed struct {
  36. Title string
  37. Link *Link
  38. Description string
  39. Author *Author
  40. Updated time.Time
  41. Created time.Time
  42. Id string
  43. Subtitle string
  44. Items []*Item
  45. Copyright string
  46. Image *Image
  47. }
  48. // add a new Item to a Feed
  49. func (f *Feed) Add(item *Item) {
  50. f.Items = append(f.Items, item)
  51. }
  52. // returns the first non-zero time formatted as a string or ""
  53. func anyTimeFormat(format string, times ...time.Time) string {
  54. for _, t := range times {
  55. if !t.IsZero() {
  56. return t.Format(format)
  57. }
  58. }
  59. return ""
  60. }
  61. // interface used by ToXML to get a object suitable for exporting XML.
  62. type XmlFeed interface {
  63. FeedXml() interface{}
  64. }
  65. // turn a feed object (either a Feed, AtomFeed, or RssFeed) into xml
  66. // returns an error if xml marshaling fails
  67. func ToXML(feed XmlFeed) (string, error) {
  68. x := feed.FeedXml()
  69. data, err := xml.MarshalIndent(x, "", " ")
  70. if err != nil {
  71. return "", err
  72. }
  73. // strip empty line from default xml header
  74. s := xml.Header[:len(xml.Header)-1] + string(data)
  75. return s, nil
  76. }
  77. // WriteXML writes a feed object (either a Feed, AtomFeed, or RssFeed) as XML into
  78. // the writer. Returns an error if XML marshaling fails.
  79. func WriteXML(feed XmlFeed, w io.Writer) error {
  80. x := feed.FeedXml()
  81. // write default xml header, without the newline
  82. if _, err := w.Write([]byte(xml.Header[:len(xml.Header)-1])); err != nil {
  83. return err
  84. }
  85. e := xml.NewEncoder(w)
  86. e.Indent("", " ")
  87. return e.Encode(x)
  88. }
  89. // creates an Atom representation of this feed
  90. func (f *Feed) ToAtom() (string, error) {
  91. a := &Atom{f}
  92. return ToXML(a)
  93. }
  94. // WriteAtom writes an Atom representation of this feed to the writer.
  95. func (f *Feed) WriteAtom(w io.Writer) error {
  96. return WriteXML(&Atom{f}, w)
  97. }
  98. // creates an Rss representation of this feed
  99. func (f *Feed) ToRss() (string, error) {
  100. r := &Rss{f}
  101. return ToXML(r)
  102. }
  103. // WriteRss writes an RSS representation of this feed to the writer.
  104. func (f *Feed) WriteRss(w io.Writer) error {
  105. return WriteXML(&Rss{f}, w)
  106. }
  107. // ToJSON creates a JSON Feed representation of this feed
  108. func (f *Feed) ToJSON() (string, error) {
  109. j := &JSON{f}
  110. return j.ToJSON()
  111. }
  112. // WriteJSON writes an JSON representation of this feed to the writer.
  113. func (f *Feed) WriteJSON(w io.Writer) error {
  114. j := &JSON{f}
  115. feed := j.JSONFeed()
  116. e := json.NewEncoder(w)
  117. e.SetIndent("", " ")
  118. return e.Encode(feed)
  119. }
  120. // Sort sorts the Items in the feed with the given less function.
  121. func (f *Feed) Sort(less func(a, b *Item) bool) {
  122. lessFunc := func(i, j int) bool {
  123. return less(f.Items[i], f.Items[j])
  124. }
  125. sort.SliceStable(f.Items, lessFunc)
  126. }