atom_10.go 7.2 KB

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