atom_10.go 5.4 KB

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