my CMS/Blog engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rss.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. // Adapted from https://github.com/krautchan/gbt
  3. //
  4. // "THE PIZZA-WARE LICENSE" (derived from "THE BEER-WARE LICENCE"):
  5. // <whoami@dev-urandom.eu> wrote these files. As long as you retain this notice
  6. // you can do whatever you want with this stuff. If we meet some day, and you think
  7. // this stuff is worth it, you can buy me a pizza in return.
  8. /*
  9. Package to parse and create RSS-Feeds
  10. */
  11. import (
  12. "encoding/xml"
  13. "os"
  14. "time"
  15. )
  16. // The root Rss structure
  17. type Rss struct {
  18. XMLName xml.Name `xml:"rss"`
  19. Version string `xml:"version,attr"`
  20. Channels []*Channel `xml:"channel"`
  21. }
  22. // The Rss channel structure
  23. type Channel struct {
  24. Title string `xml:"title"`
  25. Description string `xml:"description"`
  26. Link string `xml:"link"`
  27. LastBuildDate string `xml:"lastBuildDate"`
  28. Generator string `xml:"generator"`
  29. Image []*Image `xml:"image"`
  30. Item []*Item `xml:"item"`
  31. }
  32. // The rss image structure
  33. type Image struct {
  34. Url string `xml:"url"`
  35. Title string `xml:"title"`
  36. Link string `xml:"link"`
  37. }
  38. // The Rss item structure
  39. type Item struct {
  40. Title string `xml:"title"`
  41. Link string `xml:"link"`
  42. Description string `xml:"description"`
  43. Author string `xml:"author"`
  44. Category string `xml:"category"`
  45. PubDate string `xml:"pubDate"`
  46. Image []*Image `xml:"image"`
  47. }
  48. // Create a new RSS feed
  49. func NewRss(title string, description string, link string) *Rss {
  50. rss := &Rss{Version: "2.0",
  51. Channels: []*Channel{
  52. {
  53. Title: title,
  54. Description: description,
  55. Link: link,
  56. Generator: "trofaf (https://github.com/PuerkitoBio/trofaf)",
  57. Image: make([]*Image, 0),
  58. Item: make([]*Item, 0),
  59. },
  60. },
  61. }
  62. return rss
  63. }
  64. // Create a new, orphan Rss Item.
  65. func NewRssItem(title, link, description, author, category string, pubTime time.Time) *Item {
  66. return &Item{
  67. Title: title,
  68. Link: link,
  69. Description: description,
  70. Author: author,
  71. Category: category,
  72. PubDate: pubTime.Format(time.RFC822),
  73. Image: make([]*Image, 0),
  74. }
  75. }
  76. // Add an Item to the feed, under this Channel
  77. func (ch *Channel) AppendItem(i *Item) {
  78. ch.Item = append(ch.Item, i)
  79. }
  80. // Writes the data in RSS 2.0 format to a given file
  81. func (rss *Rss) WriteToFile(path string) error {
  82. rss.Channels[0].LastBuildDate = time.Now().Format(time.RFC822)
  83. file, err := os.Create(path)
  84. if err != nil {
  85. return err
  86. }
  87. defer file.Close()
  88. _, err = file.WriteString(xml.Header)
  89. if err != nil {
  90. return err
  91. }
  92. enc := xml.NewEncoder(file)
  93. return enc.Encode(rss)
  94. }