atom.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2017 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. type atomFeed struct {
  20. XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
  21. ID string `xml:"id"`
  22. Title string `xml:"title"`
  23. Author atomAuthor `xml:"author"`
  24. Links []atomLink `xml:"link"`
  25. Entries []atomEntry `xml:"entry"`
  26. }
  27. type atomEntry struct {
  28. ID string `xml:"id"`
  29. Title atomContent `xml:"title"`
  30. Published string `xml:"published"`
  31. Updated string `xml:"updated"`
  32. Links []atomLink `xml:"link"`
  33. Summary atomContent `xml:"summary"`
  34. Content atomContent `xml:"http://www.w3.org/2005/Atom content"`
  35. Author atomAuthor `xml:"author"`
  36. media.Element
  37. }
  38. type atomAuthor struct {
  39. Name string `xml:"name"`
  40. Email string `xml:"email"`
  41. }
  42. type atomLink struct {
  43. URL string `xml:"href,attr"`
  44. Type string `xml:"type,attr"`
  45. Rel string `xml:"rel,attr"`
  46. Length string `xml:"length,attr"`
  47. }
  48. type atomContent struct {
  49. Type string `xml:"type,attr"`
  50. Data string `xml:",chardata"`
  51. XML string `xml:",innerxml"`
  52. }
  53. func (a *atomFeed) Transform() *model.Feed {
  54. feed := new(model.Feed)
  55. feed.FeedURL = getRelationURL(a.Links, "self")
  56. feed.SiteURL = getURL(a.Links)
  57. feed.Title = strings.TrimSpace(a.Title)
  58. if feed.Title == "" {
  59. feed.Title = feed.SiteURL
  60. }
  61. for _, entry := range a.Entries {
  62. item := entry.Transform()
  63. entryURL, err := url.AbsoluteURL(feed.SiteURL, item.URL)
  64. if err == nil {
  65. item.URL = entryURL
  66. }
  67. if item.Author == "" {
  68. item.Author = getAuthor(a.Author)
  69. }
  70. if item.Title == "" {
  71. item.Title = item.URL
  72. }
  73. feed.Entries = append(feed.Entries, item)
  74. }
  75. return feed
  76. }
  77. func (a *atomEntry) Transform() *model.Entry {
  78. entry := new(model.Entry)
  79. entry.URL = getURL(a.Links)
  80. entry.Date = getDate(a)
  81. entry.Author = getAuthor(a.Author)
  82. entry.Hash = getHash(a)
  83. entry.Content = getContent(a)
  84. entry.Title = getTitle(a)
  85. entry.Enclosures = getEnclosures(a)
  86. return entry
  87. }
  88. func getURL(links []atomLink) string {
  89. for _, link := range links {
  90. if strings.ToLower(link.Rel) == "alternate" {
  91. return strings.TrimSpace(link.URL)
  92. }
  93. if link.Rel == "" && link.Type == "" {
  94. return strings.TrimSpace(link.URL)
  95. }
  96. }
  97. return ""
  98. }
  99. func getRelationURL(links []atomLink, relation string) string {
  100. for _, link := range links {
  101. if strings.ToLower(link.Rel) == relation {
  102. return strings.TrimSpace(link.URL)
  103. }
  104. }
  105. return ""
  106. }
  107. func getDate(a *atomEntry) time.Time {
  108. // Note: The published date represents the original creation date for YouTube feeds.
  109. // Example:
  110. // <published>2019-01-26T08:02:28+00:00</published>
  111. // <updated>2019-01-29T07:27:27+00:00</updated>
  112. dateText := a.Published
  113. if dateText == "" {
  114. dateText = a.Updated
  115. }
  116. if dateText != "" {
  117. result, err := date.Parse(dateText)
  118. if err != nil {
  119. logger.Error("atom: %v", err)
  120. return time.Now()
  121. }
  122. return result
  123. }
  124. return time.Now()
  125. }
  126. func atomContentToString(c atomContent) string {
  127. if c.Type == "xhtml" {
  128. return c.XML
  129. }
  130. if c.Type == "html" {
  131. return c.Data
  132. }
  133. if c.Type == "text" || c.Type == "" {
  134. return html.EscapeString(c.Data)
  135. }
  136. return ""
  137. }
  138. func getContent(a *atomEntry) string {
  139. r := atomContentToString(a.Content)
  140. if r != "" {
  141. return r
  142. }
  143. r = atomContentToString(a.Summary)
  144. if r != "" {
  145. return r
  146. }
  147. mediaDescription := a.FirstMediaDescription()
  148. if mediaDescription != "" {
  149. return mediaDescription
  150. }
  151. return ""
  152. }
  153. func getTitle(a *atomEntry) string {
  154. title := atomContentToString(a.Title)
  155. return strings.TrimSpace(sanitizer.StripTags(title))
  156. }
  157. func getHash(a *atomEntry) string {
  158. for _, value := range []string{a.ID, getURL(a.Links)} {
  159. if value != "" {
  160. return crypto.Hash(value)
  161. }
  162. }
  163. return ""
  164. }
  165. func getEnclosures(a *atomEntry) model.EnclosureList {
  166. enclosures := make(model.EnclosureList, 0)
  167. duplicates := make(map[string]bool, 0)
  168. for _, mediaThumbnail := range a.AllMediaThumbnails() {
  169. if _, found := duplicates[mediaThumbnail.URL]; !found {
  170. duplicates[mediaThumbnail.URL] = true
  171. enclosures = append(enclosures, &model.Enclosure{
  172. URL: mediaThumbnail.URL,
  173. MimeType: mediaThumbnail.MimeType(),
  174. Size: mediaThumbnail.Size(),
  175. })
  176. }
  177. }
  178. for _, link := range a.Links {
  179. if strings.ToLower(link.Rel) == "enclosure" {
  180. if _, found := duplicates[link.URL]; !found {
  181. duplicates[link.URL] = true
  182. length, _ := strconv.ParseInt(link.Length, 10, 0)
  183. enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
  184. }
  185. }
  186. }
  187. for _, mediaContent := range a.AllMediaContents() {
  188. if _, found := duplicates[mediaContent.URL]; !found {
  189. duplicates[mediaContent.URL] = true
  190. enclosures = append(enclosures, &model.Enclosure{
  191. URL: mediaContent.URL,
  192. MimeType: mediaContent.MimeType(),
  193. Size: mediaContent.Size(),
  194. })
  195. }
  196. }
  197. for _, mediaPeerLink := range a.AllMediaPeerLinks() {
  198. if _, found := duplicates[mediaPeerLink.URL]; !found {
  199. duplicates[mediaPeerLink.URL] = true
  200. enclosures = append(enclosures, &model.Enclosure{
  201. URL: mediaPeerLink.URL,
  202. MimeType: mediaPeerLink.MimeType(),
  203. Size: mediaPeerLink.Size(),
  204. })
  205. }
  206. }
  207. return enclosures
  208. }
  209. func getAuthor(author atomAuthor) string {
  210. if author.Name != "" {
  211. return strings.TrimSpace(author.Name)
  212. }
  213. if author.Email != "" {
  214. return strings.TrimSpace(author.Email)
  215. }
  216. return ""
  217. }