atom_10.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2019 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package atom // import "miniflux.app/reader/atom"
  5. import (
  6. "encoding/xml"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "miniflux.app/crypto"
  11. "miniflux.app/logger"
  12. "miniflux.app/model"
  13. "miniflux.app/reader/date"
  14. "miniflux.app/reader/media"
  15. "miniflux.app/url"
  16. )
  17. // Specs:
  18. // https://tools.ietf.org/html/rfc4287
  19. // https://validator.w3.org/feed/docs/atom.html
  20. type atom10Feed struct {
  21. XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
  22. ID string `xml:"id"`
  23. Title atom10Text `xml:"title"`
  24. Author atomPerson `xml:"author"`
  25. Links atomLinks `xml:"link"`
  26. Entries []atom10Entry `xml:"entry"`
  27. }
  28. func (a *atom10Feed) Transform(baseURL string) *model.Feed {
  29. var err error
  30. feed := new(model.Feed)
  31. feedURL := a.Links.firstLinkWithRelation("self")
  32. feed.FeedURL, err = url.AbsoluteURL(baseURL, feedURL)
  33. if err != nil {
  34. feed.FeedURL = feedURL
  35. }
  36. siteURL := a.Links.originalLink()
  37. feed.SiteURL, err = url.AbsoluteURL(baseURL, siteURL)
  38. if err != nil {
  39. feed.SiteURL = siteURL
  40. }
  41. feed.Title = a.Title.String()
  42. if feed.Title == "" {
  43. feed.Title = feed.SiteURL
  44. }
  45. for _, entry := range a.Entries {
  46. item := entry.Transform()
  47. entryURL, err := url.AbsoluteURL(feed.SiteURL, item.URL)
  48. if err == nil {
  49. item.URL = entryURL
  50. }
  51. if item.Author == "" {
  52. item.Author = a.Author.String()
  53. }
  54. if item.Title == "" {
  55. item.Title = item.URL
  56. }
  57. feed.Entries = append(feed.Entries, item)
  58. }
  59. return feed
  60. }
  61. type atom10Entry struct {
  62. ID string `xml:"id"`
  63. Title atom10Text `xml:"title"`
  64. Published string `xml:"published"`
  65. Updated string `xml:"updated"`
  66. Links atomLinks `xml:"link"`
  67. Summary atom10Text `xml:"summary"`
  68. Content atom10Text `xml:"http://www.w3.org/2005/Atom content"`
  69. Author atomPerson `xml:"author"`
  70. media.Element
  71. }
  72. func (a *atom10Entry) Transform() *model.Entry {
  73. entry := new(model.Entry)
  74. entry.URL = a.Links.originalLink()
  75. entry.Date = a.entryDate()
  76. entry.Author = a.Author.String()
  77. entry.Hash = a.entryHash()
  78. entry.Content = a.entryContent()
  79. entry.Title = a.entryTitle()
  80. entry.Enclosures = a.entryEnclosures()
  81. entry.CommentsURL = a.entryCommentsURL()
  82. return entry
  83. }
  84. func (a *atom10Entry) entryTitle() string {
  85. return a.Title.String()
  86. }
  87. func (a *atom10Entry) entryContent() string {
  88. content := a.Content.String()
  89. if content != "" {
  90. return content
  91. }
  92. summary := a.Summary.String()
  93. if summary != "" {
  94. return summary
  95. }
  96. mediaDescription := a.FirstMediaDescription()
  97. if mediaDescription != "" {
  98. return mediaDescription
  99. }
  100. return ""
  101. }
  102. // Note: The published date represents the original creation date for YouTube feeds.
  103. // Example:
  104. // <published>2019-01-26T08:02:28+00:00</published>
  105. // <updated>2019-01-29T07:27:27+00:00</updated>
  106. func (a *atom10Entry) entryDate() time.Time {
  107. dateText := a.Published
  108. if dateText == "" {
  109. dateText = a.Updated
  110. }
  111. if dateText != "" {
  112. result, err := date.Parse(dateText)
  113. if err != nil {
  114. logger.Error("atom: %v (entry ID = %s)", err, a.ID)
  115. return time.Now()
  116. }
  117. return result
  118. }
  119. return time.Now()
  120. }
  121. func (a *atom10Entry) entryHash() string {
  122. for _, value := range []string{a.ID, a.Links.originalLink()} {
  123. if value != "" {
  124. return crypto.Hash(value)
  125. }
  126. }
  127. return ""
  128. }
  129. func (a *atom10Entry) entryEnclosures() model.EnclosureList {
  130. enclosures := make(model.EnclosureList, 0)
  131. duplicates := make(map[string]bool, 0)
  132. for _, mediaThumbnail := range a.AllMediaThumbnails() {
  133. if _, found := duplicates[mediaThumbnail.URL]; !found {
  134. duplicates[mediaThumbnail.URL] = true
  135. enclosures = append(enclosures, &model.Enclosure{
  136. URL: mediaThumbnail.URL,
  137. MimeType: mediaThumbnail.MimeType(),
  138. Size: mediaThumbnail.Size(),
  139. })
  140. }
  141. }
  142. for _, link := range a.Links {
  143. if strings.ToLower(link.Rel) == "enclosure" {
  144. if link.URL == "" {
  145. continue
  146. }
  147. if _, found := duplicates[link.URL]; !found {
  148. duplicates[link.URL] = true
  149. length, _ := strconv.ParseInt(link.Length, 10, 0)
  150. enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
  151. }
  152. }
  153. }
  154. for _, mediaContent := range a.AllMediaContents() {
  155. if _, found := duplicates[mediaContent.URL]; !found {
  156. duplicates[mediaContent.URL] = true
  157. enclosures = append(enclosures, &model.Enclosure{
  158. URL: mediaContent.URL,
  159. MimeType: mediaContent.MimeType(),
  160. Size: mediaContent.Size(),
  161. })
  162. }
  163. }
  164. for _, mediaPeerLink := range a.AllMediaPeerLinks() {
  165. if _, found := duplicates[mediaPeerLink.URL]; !found {
  166. duplicates[mediaPeerLink.URL] = true
  167. enclosures = append(enclosures, &model.Enclosure{
  168. URL: mediaPeerLink.URL,
  169. MimeType: mediaPeerLink.MimeType(),
  170. Size: mediaPeerLink.Size(),
  171. })
  172. }
  173. }
  174. return enclosures
  175. }
  176. // See https://tools.ietf.org/html/rfc4685#section-4
  177. // If the type attribute of the atom:link is omitted, its value is assumed to be "application/atom+xml".
  178. // We accept only HTML or XHTML documents for now since the intention is to have the same behavior as RSS.
  179. func (a *atom10Entry) entryCommentsURL() string {
  180. commentsURL := a.Links.firstLinkWithRelationAndType("replies", "text/html", "application/xhtml+xml")
  181. if url.IsAbsoluteURL(commentsURL) {
  182. return commentsURL
  183. }
  184. return ""
  185. }
  186. type atom10Text struct {
  187. Type string `xml:"type,attr"`
  188. Data string `xml:",chardata"`
  189. XML string `xml:",innerxml"`
  190. }
  191. func (a *atom10Text) String() string {
  192. content := ""
  193. switch {
  194. case a.Type == "xhtml":
  195. content = a.XML
  196. default:
  197. content = a.Data
  198. }
  199. return strings.TrimSpace(content)
  200. }