rss.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package feeds
  2. // rss support
  3. // validation done according to spec here:
  4. // http://cyber.law.harvard.edu/rss/rss.html
  5. import (
  6. "encoding/xml"
  7. "fmt"
  8. "time"
  9. )
  10. // private wrapper around the RssFeed which gives us the <rss>..</rss> xml
  11. type RssFeedXml struct {
  12. XMLName xml.Name `xml:"rss"`
  13. Version string `xml:"version,attr"`
  14. ContentNamespace string `xml:"xmlns:content,attr"`
  15. Channel *RssFeed
  16. }
  17. type RssContent struct {
  18. XMLName xml.Name `xml:"content:encoded"`
  19. Content string `xml:",cdata"`
  20. }
  21. type RssImage struct {
  22. XMLName xml.Name `xml:"image"`
  23. Url string `xml:"url"`
  24. Title string `xml:"title"`
  25. Link string `xml:"link"`
  26. Width int `xml:"width,omitempty"`
  27. Height int `xml:"height,omitempty"`
  28. }
  29. type RssTextInput struct {
  30. XMLName xml.Name `xml:"textInput"`
  31. Title string `xml:"title"`
  32. Description string `xml:"description"`
  33. Name string `xml:"name"`
  34. Link string `xml:"link"`
  35. }
  36. type RssFeed struct {
  37. XMLName xml.Name `xml:"channel"`
  38. Title string `xml:"title"` // required
  39. Link string `xml:"link"` // required
  40. Description string `xml:"description"` // required
  41. Language string `xml:"language,omitempty"`
  42. Copyright string `xml:"copyright,omitempty"`
  43. ManagingEditor string `xml:"managingEditor,omitempty"` // Author used
  44. WebMaster string `xml:"webMaster,omitempty"`
  45. PubDate string `xml:"pubDate,omitempty"` // created or updated
  46. LastBuildDate string `xml:"lastBuildDate,omitempty"` // updated used
  47. Category string `xml:"category,omitempty"`
  48. Generator string `xml:"generator,omitempty"`
  49. Docs string `xml:"docs,omitempty"`
  50. Cloud string `xml:"cloud,omitempty"`
  51. Ttl int `xml:"ttl,omitempty"`
  52. Rating string `xml:"rating,omitempty"`
  53. SkipHours string `xml:"skipHours,omitempty"`
  54. SkipDays string `xml:"skipDays,omitempty"`
  55. Image *RssImage
  56. TextInput *RssTextInput
  57. Items []*RssItem `xml:"item"`
  58. }
  59. type RssItem struct {
  60. XMLName xml.Name `xml:"item"`
  61. Title string `xml:"title"` // required
  62. Link string `xml:"link"` // required
  63. Description string `xml:"description"` // required
  64. Content *RssContent
  65. Author string `xml:"author,omitempty"`
  66. Category string `xml:"category,omitempty"`
  67. Comments string `xml:"comments,omitempty"`
  68. Enclosure *RssEnclosure
  69. Guid *RssGuid // Id used
  70. PubDate string `xml:"pubDate,omitempty"` // created or updated
  71. Source string `xml:"source,omitempty"`
  72. }
  73. type RssEnclosure struct {
  74. //RSS 2.0 <enclosure url="http://example.com/file.mp3" length="123456789" type="audio/mpeg" />
  75. XMLName xml.Name `xml:"enclosure"`
  76. Url string `xml:"url,attr"`
  77. Length string `xml:"length,attr"`
  78. Type string `xml:"type,attr"`
  79. }
  80. type RssGuid struct {
  81. //RSS 2.0 <guid isPermaLink="true">http://inessential.com/2002/09/01.php#a2</guid>
  82. XMLName xml.Name `xml:"guid"`
  83. Id string `xml:",chardata"`
  84. IsPermaLink string `xml:"isPermaLink,attr,omitempty"` // "true", "false", or an empty string
  85. }
  86. type Rss struct {
  87. *Feed
  88. }
  89. // create a new RssItem with a generic Item struct's data
  90. func newRssItem(i *Item) *RssItem {
  91. item := &RssItem{
  92. Title: i.Title,
  93. Description: i.Description,
  94. PubDate: anyTimeFormat(time.RFC1123Z, i.Created, i.Updated),
  95. }
  96. if i.Id != "" {
  97. item.Guid = &RssGuid{Id: i.Id, IsPermaLink: i.IsPermaLink}
  98. }
  99. if i.Link != nil {
  100. item.Link = i.Link.Href
  101. }
  102. if len(i.Content) > 0 {
  103. item.Content = &RssContent{Content: i.Content}
  104. }
  105. if i.Source != nil {
  106. item.Source = i.Source.Href
  107. }
  108. // Define a closure
  109. if i.Enclosure != nil && i.Enclosure.Type != "" && i.Enclosure.Length != "" {
  110. item.Enclosure = &RssEnclosure{Url: i.Enclosure.Url, Type: i.Enclosure.Type, Length: i.Enclosure.Length}
  111. }
  112. if i.Author != nil {
  113. item.Author = i.Author.Name
  114. }
  115. return item
  116. }
  117. // create a new RssFeed with a generic Feed struct's data
  118. func (r *Rss) RssFeed() *RssFeed {
  119. pub := anyTimeFormat(time.RFC1123Z, r.Created, r.Updated)
  120. build := anyTimeFormat(time.RFC1123Z, r.Updated)
  121. author := ""
  122. if r.Author != nil {
  123. author = r.Author.Email
  124. if len(r.Author.Name) > 0 {
  125. author = fmt.Sprintf("%s (%s)", r.Author.Email, r.Author.Name)
  126. }
  127. }
  128. var image *RssImage
  129. if r.Image != nil {
  130. image = &RssImage{Url: r.Image.Url, Title: r.Image.Title, Link: r.Image.Link, Width: r.Image.Width, Height: r.Image.Height}
  131. }
  132. var href string
  133. if r.Link != nil {
  134. href = r.Link.Href
  135. }
  136. channel := &RssFeed{
  137. Title: r.Title,
  138. Link: href,
  139. Description: r.Description,
  140. ManagingEditor: author,
  141. PubDate: pub,
  142. LastBuildDate: build,
  143. Copyright: r.Copyright,
  144. Image: image,
  145. }
  146. for _, i := range r.Items {
  147. channel.Items = append(channel.Items, newRssItem(i))
  148. }
  149. return channel
  150. }
  151. // FeedXml returns an XML-Ready object for an Rss object
  152. func (r *Rss) FeedXml() interface{} {
  153. // only generate version 2.0 feeds for now
  154. return r.RssFeed().FeedXml()
  155. }
  156. // FeedXml returns an XML-ready object for an RssFeed object
  157. func (r *RssFeed) FeedXml() interface{} {
  158. return &RssFeedXml{
  159. Version: "2.0",
  160. Channel: r,
  161. ContentNamespace: "http://purl.org/rss/1.0/modules/content/",
  162. }
  163. }