atom.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. "log"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/miniflux/miniflux2/helper"
  12. "github.com/miniflux/miniflux2/model"
  13. "github.com/miniflux/miniflux2/reader/date"
  14. "github.com/miniflux/miniflux2/reader/processor"
  15. "github.com/miniflux/miniflux2/reader/sanitizer"
  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 string `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 = sanitizer.StripTags(a.Title)
  58. if feed.Title == "" {
  59. feed.Title = feed.SiteURL
  60. }
  61. for _, entry := range a.Entries {
  62. item := entry.Transform()
  63. if item.Author == "" {
  64. item.Author = getAuthor(a.Author)
  65. }
  66. feed.Entries = append(feed.Entries, item)
  67. }
  68. return feed
  69. }
  70. func (a *atomEntry) Transform() *model.Entry {
  71. entry := new(model.Entry)
  72. entry.URL = getURL(a.Links)
  73. entry.Date = getDate(a)
  74. entry.Author = sanitizer.StripTags(getAuthor(a.Author))
  75. entry.Hash = getHash(a)
  76. entry.Content = processor.ItemContentProcessor(entry.URL, getContent(a))
  77. entry.Title = sanitizer.StripTags(strings.Trim(a.Title, " \n\t"))
  78. entry.Enclosures = getEnclosures(a)
  79. if entry.Title == "" {
  80. entry.Title = entry.URL
  81. }
  82. return entry
  83. }
  84. func getURL(links []atomLink) string {
  85. for _, link := range links {
  86. if strings.ToLower(link.Rel) == "alternate" {
  87. return link.URL
  88. }
  89. if link.Rel == "" && link.Type == "" {
  90. return link.URL
  91. }
  92. }
  93. return ""
  94. }
  95. func getRelationURL(links []atomLink, relation string) string {
  96. for _, link := range links {
  97. if strings.ToLower(link.Rel) == relation {
  98. return link.URL
  99. }
  100. }
  101. return ""
  102. }
  103. func getDate(a *atomEntry) time.Time {
  104. if a.Updated != "" {
  105. result, err := date.Parse(a.Updated)
  106. if err != nil {
  107. log.Println(err)
  108. return time.Now()
  109. }
  110. return result
  111. }
  112. return time.Now()
  113. }
  114. func getContent(a *atomEntry) string {
  115. if a.Content.Type == "html" || a.Content.Type == "text" {
  116. return a.Content.Data
  117. }
  118. if a.Content.Type == "xhtml" {
  119. return a.Content.XML
  120. }
  121. if a.Summary != "" {
  122. return a.Summary
  123. }
  124. if a.MediaGroup.Description != "" {
  125. return a.MediaGroup.Description
  126. }
  127. return ""
  128. }
  129. func getHash(a *atomEntry) string {
  130. for _, value := range []string{a.ID, getURL(a.Links)} {
  131. if value != "" {
  132. return helper.Hash(value)
  133. }
  134. }
  135. return ""
  136. }
  137. func getEnclosures(a *atomEntry) model.EnclosureList {
  138. enclosures := make(model.EnclosureList, 0)
  139. for _, link := range a.Links {
  140. if strings.ToLower(link.Rel) == "enclosure" {
  141. length, _ := strconv.Atoi(link.Length)
  142. enclosures = append(enclosures, &model.Enclosure{URL: link.URL, MimeType: link.Type, Size: length})
  143. }
  144. }
  145. return enclosures
  146. }
  147. func getAuthor(author atomAuthor) string {
  148. if author.Name != "" {
  149. return author.Name
  150. }
  151. if author.Email != "" {
  152. return author.Email
  153. }
  154. return ""
  155. }