atom.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package feeds
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "net/url"
  6. "time"
  7. )
  8. // Generates Atom feed as XML
  9. const ns = "http://www.w3.org/2005/Atom"
  10. type AtomPerson struct {
  11. Name string `xml:"name,omitempty"`
  12. Uri string `xml:"uri,omitempty"`
  13. Email string `xml:"email,omitempty"`
  14. }
  15. type AtomSummary struct {
  16. XMLName xml.Name `xml:"summary"`
  17. Content string `xml:",chardata"`
  18. Type string `xml:"type,attr"`
  19. }
  20. type AtomContent struct {
  21. XMLName xml.Name `xml:"content"`
  22. Content string `xml:",chardata"`
  23. Type string `xml:"type,attr"`
  24. }
  25. type AtomAuthor struct {
  26. XMLName xml.Name `xml:"author"`
  27. AtomPerson
  28. }
  29. type AtomContributor struct {
  30. XMLName xml.Name `xml:"contributor"`
  31. AtomPerson
  32. }
  33. type AtomEntry struct {
  34. XMLName xml.Name `xml:"entry"`
  35. Xmlns string `xml:"xmlns,attr,omitempty"`
  36. Title string `xml:"title"` // required
  37. Updated string `xml:"updated"` // required
  38. Id string `xml:"id"` // required
  39. Category string `xml:"category,omitempty"`
  40. Content *AtomContent
  41. Rights string `xml:"rights,omitempty"`
  42. Source string `xml:"source,omitempty"`
  43. Published string `xml:"published,omitempty"`
  44. Contributor *AtomContributor
  45. Links []AtomLink // required if no child 'content' elements
  46. Summary *AtomSummary // required if content has src or content is base64
  47. Author *AtomAuthor // required if feed lacks an author
  48. }
  49. // Multiple links with different rel can coexist
  50. type AtomLink struct {
  51. //Atom 1.0 <link rel="enclosure" type="audio/mpeg" title="MP3" href="http://www.example.org/myaudiofile.mp3" length="1234" />
  52. XMLName xml.Name `xml:"link"`
  53. Href string `xml:"href,attr"`
  54. Rel string `xml:"rel,attr,omitempty"`
  55. Type string `xml:"type,attr,omitempty"`
  56. Length string `xml:"length,attr,omitempty"`
  57. }
  58. type AtomFeed struct {
  59. XMLName xml.Name `xml:"feed"`
  60. Xmlns string `xml:"xmlns,attr"`
  61. Title string `xml:"title"` // required
  62. Id string `xml:"id"` // required
  63. Updated string `xml:"updated"` // required
  64. Category string `xml:"category,omitempty"`
  65. Icon string `xml:"icon,omitempty"`
  66. Logo string `xml:"logo,omitempty"`
  67. Rights string `xml:"rights,omitempty"` // copyright used
  68. Subtitle string `xml:"subtitle,omitempty"`
  69. Link *AtomLink
  70. Author *AtomAuthor `xml:"author,omitempty"`
  71. Contributor *AtomContributor
  72. Entries []*AtomEntry `xml:"entry"`
  73. }
  74. type Atom struct {
  75. *Feed
  76. }
  77. func newAtomEntry(i *Item) *AtomEntry {
  78. id := i.Id
  79. link := i.Link
  80. if link == nil {
  81. link = &Link{}
  82. }
  83. if len(id) == 0 {
  84. // if there's no id set, try to create one, either from data or just a uuid
  85. if len(link.Href) > 0 && (!i.Created.IsZero() || !i.Updated.IsZero()) {
  86. dateStr := anyTimeFormat("2006-01-02", i.Updated, i.Created)
  87. host, path := link.Href, "/invalid.html"
  88. if url, err := url.Parse(link.Href); err == nil {
  89. host, path = url.Host, url.Path
  90. }
  91. id = fmt.Sprintf("tag:%s,%s:%s", host, dateStr, path)
  92. } else {
  93. id = "urn:uuid:" + NewUUID().String()
  94. }
  95. }
  96. var name, email string
  97. if i.Author != nil {
  98. name, email = i.Author.Name, i.Author.Email
  99. }
  100. link_rel := link.Rel
  101. if link_rel == "" {
  102. link_rel = "alternate"
  103. }
  104. x := &AtomEntry{
  105. Title: i.Title,
  106. Links: []AtomLink{{Href: link.Href, Rel: link_rel, Type: link.Type}},
  107. Id: id,
  108. Updated: anyTimeFormat(time.RFC3339, i.Updated, i.Created),
  109. }
  110. // if there's a description, assume it's html
  111. if len(i.Description) > 0 {
  112. x.Summary = &AtomSummary{Content: i.Description, Type: "html"}
  113. }
  114. // if there's a content, assume it's html
  115. if len(i.Content) > 0 {
  116. x.Content = &AtomContent{Content: i.Content, Type: "html"}
  117. }
  118. if i.Enclosure != nil && link_rel != "enclosure" {
  119. x.Links = append(x.Links, AtomLink{Href: i.Enclosure.Url, Rel: "enclosure", Type: i.Enclosure.Type, Length: i.Enclosure.Length})
  120. }
  121. if len(name) > 0 || len(email) > 0 {
  122. x.Author = &AtomAuthor{AtomPerson: AtomPerson{Name: name, Email: email}}
  123. }
  124. return x
  125. }
  126. // create a new AtomFeed with a generic Feed struct's data
  127. func (a *Atom) AtomFeed() *AtomFeed {
  128. updated := anyTimeFormat(time.RFC3339, a.Updated, a.Created)
  129. link := a.Link
  130. if link == nil {
  131. link = &Link{}
  132. }
  133. feed := &AtomFeed{
  134. Xmlns: ns,
  135. Title: a.Title,
  136. Link: &AtomLink{Href: link.Href, Rel: link.Rel},
  137. Subtitle: a.Description,
  138. Id: link.Href,
  139. Updated: updated,
  140. Rights: a.Copyright,
  141. }
  142. if a.Author != nil {
  143. feed.Author = &AtomAuthor{AtomPerson: AtomPerson{Name: a.Author.Name, Email: a.Author.Email}}
  144. }
  145. for _, e := range a.Items {
  146. feed.Entries = append(feed.Entries, newAtomEntry(e))
  147. }
  148. return feed
  149. }
  150. // FeedXml returns an XML-Ready object for an Atom object
  151. func (a *Atom) FeedXml() interface{} {
  152. return a.AtomFeed()
  153. }
  154. // FeedXml returns an XML-ready object for an AtomFeed object
  155. func (a *AtomFeed) FeedXml() interface{} {
  156. return a
  157. }