atom.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/miniflux/miniflux/crypto"
  11. "github.com/miniflux/miniflux/logger"
  12. "github.com/miniflux/miniflux/model"
  13. "github.com/miniflux/miniflux/reader/date"
  14. "github.com/miniflux/miniflux/reader/sanitizer"
  15. "github.com/miniflux/miniflux/url"
  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 atomAuthor `xml:"author"`
  22. Links []atomLink `xml:"link"`
  23. Entries []atomEntry `xml:"entry"`
  24. }
  25. type atomEntry struct {
  26. ID string `xml:"id"`
  27. Title atomContent `xml:"title"`
  28. Updated string `xml:"updated"`
  29. Links []atomLink `xml:"link"`
  30. Summary string `xml:"summary"`
  31. Content atomContent `xml:"content"`
  32. MediaGroup atomMediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
  33. Author atomAuthor `xml:"author"`
  34. }
  35. type atomAuthor struct {
  36. Name string `xml:"name"`
  37. Email string `xml:"email"`
  38. }
  39. type atomLink 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 atomContent struct {
  46. Type string `xml:"type,attr"`
  47. Data string `xml:",chardata"`
  48. XML string `xml:",innerxml"`
  49. }
  50. type atomMediaGroup struct {
  51. Description string `xml:"http://search.yahoo.com/mrss/ description"`
  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. if a.Updated != "" {
  109. result, err := date.Parse(a.Updated)
  110. if err != nil {
  111. logger.Error("atom: %v", err)
  112. return time.Now()
  113. }
  114. return result
  115. }
  116. return time.Now()
  117. }
  118. func getContent(a *atomEntry) string {
  119. if a.Content.Type == "html" || a.Content.Type == "text" {
  120. return a.Content.Data
  121. }
  122. if a.Content.Type == "xhtml" {
  123. return a.Content.XML
  124. }
  125. if a.Summary != "" {
  126. return a.Summary
  127. }
  128. if a.MediaGroup.Description != "" {
  129. return a.MediaGroup.Description
  130. }
  131. return ""
  132. }
  133. func getTitle(a *atomEntry) string {
  134. title := ""
  135. if a.Title.Type == "xhtml" {
  136. title = a.Title.XML
  137. } else {
  138. title = a.Title.Data
  139. }
  140. return strings.TrimSpace(sanitizer.StripTags(title))
  141. }
  142. func getHash(a *atomEntry) string {
  143. for _, value := range []string{a.ID, getURL(a.Links)} {
  144. if value != "" {
  145. return crypto.Hash(value)
  146. }
  147. }
  148. return ""
  149. }
  150. func getEnclosures(a *atomEntry) model.EnclosureList {
  151. enclosures := make(model.EnclosureList, 0)
  152. for _, link := range a.Links {
  153. if strings.ToLower(link.Rel) == "enclosure" {
  154. length, _ := strconv.ParseInt(link.Length, 10, 0)
  155. enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
  156. }
  157. }
  158. return enclosures
  159. }
  160. func getAuthor(author atomAuthor) string {
  161. if author.Name != "" {
  162. return strings.TrimSpace(author.Name)
  163. }
  164. if author.Email != "" {
  165. return strings.TrimSpace(author.Email)
  166. }
  167. return ""
  168. }