atom_10.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. "html"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "miniflux.app/crypto"
  12. "miniflux.app/logger"
  13. "miniflux.app/model"
  14. "miniflux.app/reader/date"
  15. "miniflux.app/reader/media"
  16. "miniflux.app/reader/sanitizer"
  17. "miniflux.app/url"
  18. )
  19. // Specs:
  20. // https://tools.ietf.org/html/rfc4287
  21. // https://validator.w3.org/feed/docs/atom.html
  22. type atom10Feed struct {
  23. XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
  24. ID string `xml:"id"`
  25. Title atom10Text `xml:"title"`
  26. Authors atomAuthors `xml:"author"`
  27. Icon string `xml:"icon"`
  28. Links atomLinks `xml:"link"`
  29. Entries []atom10Entry `xml:"entry"`
  30. }
  31. func (a *atom10Feed) Transform(baseURL string) *model.Feed {
  32. var err error
  33. feed := new(model.Feed)
  34. feedURL := a.Links.firstLinkWithRelation("self")
  35. feed.FeedURL, err = url.AbsoluteURL(baseURL, feedURL)
  36. if err != nil {
  37. feed.FeedURL = feedURL
  38. }
  39. siteURL := a.Links.originalLink()
  40. feed.SiteURL, err = url.AbsoluteURL(baseURL, siteURL)
  41. if err != nil {
  42. feed.SiteURL = siteURL
  43. }
  44. feed.Title = html.UnescapeString(a.Title.String())
  45. if feed.Title == "" {
  46. feed.Title = feed.SiteURL
  47. }
  48. feed.IconURL = strings.TrimSpace(a.Icon)
  49. for _, entry := range a.Entries {
  50. item := entry.Transform()
  51. entryURL, err := url.AbsoluteURL(feed.SiteURL, item.URL)
  52. if err == nil {
  53. item.URL = entryURL
  54. }
  55. if item.Author == "" {
  56. item.Author = a.Authors.String()
  57. }
  58. if item.Title == "" {
  59. item.Title = sanitizer.TruncateHTML(item.Content, 100)
  60. }
  61. if item.Title == "" {
  62. item.Title = item.URL
  63. }
  64. feed.Entries = append(feed.Entries, item)
  65. }
  66. return feed
  67. }
  68. type atom10Entry struct {
  69. ID string `xml:"id"`
  70. Title atom10Text `xml:"title"`
  71. Published string `xml:"published"`
  72. Updated string `xml:"updated"`
  73. Links atomLinks `xml:"link"`
  74. Summary atom10Text `xml:"summary"`
  75. Content atom10Text `xml:"http://www.w3.org/2005/Atom content"`
  76. Authors atomAuthors `xml:"author"`
  77. Categories []atom10Category `xml:"category"`
  78. media.Element
  79. }
  80. func (a *atom10Entry) Transform() *model.Entry {
  81. entry := new(model.Entry)
  82. entry.URL = a.Links.originalLink()
  83. entry.Date = a.entryDate()
  84. entry.Author = a.Authors.String()
  85. entry.Hash = a.entryHash()
  86. entry.Content = a.entryContent()
  87. entry.Title = a.entryTitle()
  88. entry.Enclosures = a.entryEnclosures()
  89. entry.CommentsURL = a.entryCommentsURL()
  90. entry.Tags = a.entryCategories()
  91. return entry
  92. }
  93. func (a *atom10Entry) entryTitle() string {
  94. return html.UnescapeString(a.Title.String())
  95. }
  96. func (a *atom10Entry) entryContent() string {
  97. content := a.Content.String()
  98. if content != "" {
  99. return content
  100. }
  101. summary := a.Summary.String()
  102. if summary != "" {
  103. return summary
  104. }
  105. mediaDescription := a.FirstMediaDescription()
  106. if mediaDescription != "" {
  107. return mediaDescription
  108. }
  109. return ""
  110. }
  111. // Note: The published date represents the original creation date for YouTube feeds.
  112. // Example:
  113. // <published>2019-01-26T08:02:28+00:00</published>
  114. // <updated>2019-01-29T07:27:27+00:00</updated>
  115. func (a *atom10Entry) entryDate() time.Time {
  116. dateText := a.Published
  117. if dateText == "" {
  118. dateText = a.Updated
  119. }
  120. if dateText != "" {
  121. result, err := date.Parse(dateText)
  122. if err != nil {
  123. logger.Error("atom: %v (entry ID = %s)", err, a.ID)
  124. return time.Now()
  125. }
  126. return result
  127. }
  128. return time.Now()
  129. }
  130. func (a *atom10Entry) entryHash() string {
  131. for _, value := range []string{a.ID, a.Links.originalLink()} {
  132. if value != "" {
  133. return crypto.Hash(value)
  134. }
  135. }
  136. return ""
  137. }
  138. func (a *atom10Entry) entryEnclosures() model.EnclosureList {
  139. enclosures := make(model.EnclosureList, 0)
  140. duplicates := make(map[string]bool)
  141. for _, mediaThumbnail := range a.AllMediaThumbnails() {
  142. if _, found := duplicates[mediaThumbnail.URL]; !found {
  143. duplicates[mediaThumbnail.URL] = true
  144. enclosures = append(enclosures, &model.Enclosure{
  145. URL: mediaThumbnail.URL,
  146. MimeType: mediaThumbnail.MimeType(),
  147. Size: mediaThumbnail.Size(),
  148. })
  149. }
  150. }
  151. for _, link := range a.Links {
  152. if strings.ToLower(link.Rel) == "enclosure" {
  153. if link.URL == "" {
  154. continue
  155. }
  156. if _, found := duplicates[link.URL]; !found {
  157. duplicates[link.URL] = true
  158. length, _ := strconv.ParseInt(link.Length, 10, 0)
  159. enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
  160. }
  161. }
  162. }
  163. for _, mediaContent := range a.AllMediaContents() {
  164. if _, found := duplicates[mediaContent.URL]; !found {
  165. duplicates[mediaContent.URL] = true
  166. enclosures = append(enclosures, &model.Enclosure{
  167. URL: mediaContent.URL,
  168. MimeType: mediaContent.MimeType(),
  169. Size: mediaContent.Size(),
  170. })
  171. }
  172. }
  173. for _, mediaPeerLink := range a.AllMediaPeerLinks() {
  174. if _, found := duplicates[mediaPeerLink.URL]; !found {
  175. duplicates[mediaPeerLink.URL] = true
  176. enclosures = append(enclosures, &model.Enclosure{
  177. URL: mediaPeerLink.URL,
  178. MimeType: mediaPeerLink.MimeType(),
  179. Size: mediaPeerLink.Size(),
  180. })
  181. }
  182. }
  183. return enclosures
  184. }
  185. func (r *atom10Entry) entryCategories() []string {
  186. var categoryList []string
  187. for _, atomCategory := range r.Categories {
  188. if strings.TrimSpace(atomCategory.Label) != "" {
  189. categoryList = append(categoryList, strings.TrimSpace(atomCategory.Label))
  190. } else {
  191. categoryList = append(categoryList, strings.TrimSpace(atomCategory.Term))
  192. }
  193. }
  194. return categoryList
  195. }
  196. // See https://tools.ietf.org/html/rfc4685#section-4
  197. // If the type attribute of the atom:link is omitted, its value is assumed to be "application/atom+xml".
  198. // We accept only HTML or XHTML documents for now since the intention is to have the same behavior as RSS.
  199. func (a *atom10Entry) entryCommentsURL() string {
  200. commentsURL := a.Links.firstLinkWithRelationAndType("replies", "text/html", "application/xhtml+xml")
  201. if url.IsAbsoluteURL(commentsURL) {
  202. return commentsURL
  203. }
  204. return ""
  205. }
  206. type atom10Text struct {
  207. Type string `xml:"type,attr"`
  208. CharData string `xml:",chardata"`
  209. InnerXML string `xml:",innerxml"`
  210. XHTMLRootElement atomXHTMLRootElement `xml:"http://www.w3.org/1999/xhtml div"`
  211. }
  212. type atom10Category struct {
  213. Term string `xml:"term,attr"`
  214. Label string `xml:"label,attr"`
  215. }
  216. // Text: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.1
  217. // HTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.2
  218. // XHTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.3
  219. func (a *atom10Text) String() string {
  220. var content string
  221. switch {
  222. case a.Type == "", a.Type == "text", a.Type == "text/plain":
  223. if strings.HasPrefix(strings.TrimSpace(a.InnerXML), `<![CDATA[`) {
  224. content = html.EscapeString(a.CharData)
  225. } else {
  226. content = a.InnerXML
  227. }
  228. case a.Type == "xhtml":
  229. var root = a.XHTMLRootElement
  230. if root.XMLName.Local == "div" {
  231. content = root.InnerXML
  232. } else {
  233. content = a.InnerXML
  234. }
  235. default:
  236. content = a.CharData
  237. }
  238. return strings.TrimSpace(content)
  239. }
  240. type atomXHTMLRootElement struct {
  241. XMLName xml.Name `xml:"div"`
  242. InnerXML string `xml:",innerxml"`
  243. }