atom.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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
  5. import (
  6. "encoding/xml"
  7. "github.com/miniflux/miniflux2/helper"
  8. "github.com/miniflux/miniflux2/model"
  9. "github.com/miniflux/miniflux2/reader/feed/date"
  10. "github.com/miniflux/miniflux2/reader/processor"
  11. "github.com/miniflux/miniflux2/reader/sanitizer"
  12. "log"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. type AtomFeed struct {
  18. XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
  19. ID string `xml:"id"`
  20. Title string `xml:"title"`
  21. Author Author `xml:"author"`
  22. Links []Link `xml:"link"`
  23. Entries []AtomEntry `xml:"entry"`
  24. }
  25. type AtomEntry struct {
  26. ID string `xml:"id"`
  27. Title string `xml:"title"`
  28. Updated string `xml:"updated"`
  29. Links []Link `xml:"link"`
  30. Summary string `xml:"summary"`
  31. Content Content `xml:"content"`
  32. MediaGroup MediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
  33. Author Author `xml:"author"`
  34. }
  35. type Author struct {
  36. Name string `xml:"name"`
  37. Email string `xml:"email"`
  38. }
  39. type Link struct {
  40. Url string `xml:"href,attr"`
  41. Type string `xml:"type,attr"`
  42. Rel string `xml:"rel,attr"`
  43. Length string `xml:"length,attr"`
  44. }
  45. type Content struct {
  46. Type string `xml:"type,attr"`
  47. Data string `xml:",chardata"`
  48. Xml string `xml:",innerxml"`
  49. }
  50. type MediaGroup struct {
  51. Description string `xml:"http://search.yahoo.com/mrss/ description"`
  52. }
  53. func (a *AtomFeed) getSiteURL() string {
  54. for _, link := range a.Links {
  55. if strings.ToLower(link.Rel) == "alternate" {
  56. return link.Url
  57. }
  58. if link.Rel == "" && link.Type == "" {
  59. return link.Url
  60. }
  61. }
  62. return ""
  63. }
  64. func (a *AtomFeed) getFeedURL() string {
  65. for _, link := range a.Links {
  66. if strings.ToLower(link.Rel) == "self" {
  67. return link.Url
  68. }
  69. }
  70. return ""
  71. }
  72. func (a *AtomFeed) Transform() *model.Feed {
  73. feed := new(model.Feed)
  74. feed.FeedURL = a.getFeedURL()
  75. feed.SiteURL = a.getSiteURL()
  76. feed.Title = sanitizer.StripTags(a.Title)
  77. if feed.Title == "" {
  78. feed.Title = feed.SiteURL
  79. }
  80. for _, entry := range a.Entries {
  81. item := entry.Transform()
  82. if item.Author == "" {
  83. item.Author = a.GetAuthor()
  84. }
  85. feed.Entries = append(feed.Entries, item)
  86. }
  87. return feed
  88. }
  89. func (a *AtomFeed) GetAuthor() string {
  90. return getAuthor(a.Author)
  91. }
  92. func (e *AtomEntry) GetDate() time.Time {
  93. if e.Updated != "" {
  94. result, err := date.Parse(e.Updated)
  95. if err != nil {
  96. log.Println(err)
  97. return time.Now()
  98. }
  99. return result
  100. }
  101. return time.Now()
  102. }
  103. func (e *AtomEntry) GetURL() string {
  104. for _, link := range e.Links {
  105. if strings.ToLower(link.Rel) == "alternate" {
  106. return link.Url
  107. }
  108. if link.Rel == "" && link.Type == "" {
  109. return link.Url
  110. }
  111. }
  112. return ""
  113. }
  114. func (e *AtomEntry) GetAuthor() string {
  115. return getAuthor(e.Author)
  116. }
  117. func (e *AtomEntry) GetHash() string {
  118. for _, value := range []string{e.ID, e.GetURL()} {
  119. if value != "" {
  120. return helper.Hash(value)
  121. }
  122. }
  123. return ""
  124. }
  125. func (e *AtomEntry) GetContent() string {
  126. if e.Content.Type == "html" || e.Content.Type == "text" {
  127. return e.Content.Data
  128. }
  129. if e.Content.Type == "xhtml" {
  130. return e.Content.Xml
  131. }
  132. if e.Summary != "" {
  133. return e.Summary
  134. }
  135. if e.MediaGroup.Description != "" {
  136. return e.MediaGroup.Description
  137. }
  138. return ""
  139. }
  140. func (e *AtomEntry) GetEnclosures() model.EnclosureList {
  141. enclosures := make(model.EnclosureList, 0)
  142. for _, link := range e.Links {
  143. if strings.ToLower(link.Rel) == "enclosure" {
  144. length, _ := strconv.Atoi(link.Length)
  145. enclosures = append(enclosures, &model.Enclosure{URL: link.Url, MimeType: link.Type, Size: length})
  146. }
  147. }
  148. return enclosures
  149. }
  150. func (e *AtomEntry) Transform() *model.Entry {
  151. entry := new(model.Entry)
  152. entry.URL = e.GetURL()
  153. entry.Date = e.GetDate()
  154. entry.Author = sanitizer.StripTags(e.GetAuthor())
  155. entry.Hash = e.GetHash()
  156. entry.Content = processor.ItemContentProcessor(entry.URL, e.GetContent())
  157. entry.Title = sanitizer.StripTags(strings.Trim(e.Title, " \n\t"))
  158. entry.Enclosures = e.GetEnclosures()
  159. if entry.Title == "" {
  160. entry.Title = entry.URL
  161. }
  162. return entry
  163. }
  164. func getAuthor(author Author) string {
  165. if author.Name != "" {
  166. return author.Name
  167. }
  168. if author.Email != "" {
  169. return author.Email
  170. }
  171. return ""
  172. }