atom.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/helper"
  11. "github.com/miniflux/miniflux/logger"
  12. "github.com/miniflux/miniflux/model"
  13. "github.com/miniflux/miniflux/reader/date"
  14. "github.com/miniflux/miniflux/url"
  15. )
  16. type atomFeed struct {
  17. XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
  18. ID string `xml:"id"`
  19. Title string `xml:"title"`
  20. Author atomAuthor `xml:"author"`
  21. Links []atomLink `xml:"link"`
  22. Entries []atomEntry `xml:"entry"`
  23. }
  24. type atomEntry struct {
  25. ID string `xml:"id"`
  26. Title string `xml:"title"`
  27. Updated string `xml:"updated"`
  28. Links []atomLink `xml:"link"`
  29. Summary string `xml:"summary"`
  30. Content atomContent `xml:"content"`
  31. MediaGroup atomMediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
  32. Author atomAuthor `xml:"author"`
  33. }
  34. type atomAuthor struct {
  35. Name string `xml:"name"`
  36. Email string `xml:"email"`
  37. }
  38. type atomLink struct {
  39. URL string `xml:"href,attr"`
  40. Type string `xml:"type,attr"`
  41. Rel string `xml:"rel,attr"`
  42. Length string `xml:"length,attr"`
  43. }
  44. type atomContent struct {
  45. Type string `xml:"type,attr"`
  46. Data string `xml:",chardata"`
  47. XML string `xml:",innerxml"`
  48. }
  49. type atomMediaGroup struct {
  50. Description string `xml:"http://search.yahoo.com/mrss/ description"`
  51. }
  52. func (a *atomFeed) Transform() *model.Feed {
  53. feed := new(model.Feed)
  54. feed.FeedURL = getRelationURL(a.Links, "self")
  55. feed.SiteURL = getURL(a.Links)
  56. feed.Title = strings.TrimSpace(a.Title)
  57. if feed.Title == "" {
  58. feed.Title = feed.SiteURL
  59. }
  60. for _, entry := range a.Entries {
  61. item := entry.Transform()
  62. entryURL, err := url.AbsoluteURL(feed.SiteURL, item.URL)
  63. if err == nil {
  64. item.URL = entryURL
  65. }
  66. if item.Author == "" {
  67. item.Author = getAuthor(a.Author)
  68. }
  69. if item.Title == "" {
  70. item.Title = item.URL
  71. }
  72. feed.Entries = append(feed.Entries, item)
  73. }
  74. return feed
  75. }
  76. func (a *atomEntry) Transform() *model.Entry {
  77. entry := new(model.Entry)
  78. entry.URL = getURL(a.Links)
  79. entry.Date = getDate(a)
  80. entry.Author = getAuthor(a.Author)
  81. entry.Hash = getHash(a)
  82. entry.Content = getContent(a)
  83. entry.Title = strings.TrimSpace(a.Title)
  84. entry.Enclosures = getEnclosures(a)
  85. return entry
  86. }
  87. func getURL(links []atomLink) string {
  88. for _, link := range links {
  89. if strings.ToLower(link.Rel) == "alternate" {
  90. return strings.TrimSpace(link.URL)
  91. }
  92. if link.Rel == "" && link.Type == "" {
  93. return strings.TrimSpace(link.URL)
  94. }
  95. }
  96. return ""
  97. }
  98. func getRelationURL(links []atomLink, relation string) string {
  99. for _, link := range links {
  100. if strings.ToLower(link.Rel) == relation {
  101. return strings.TrimSpace(link.URL)
  102. }
  103. }
  104. return ""
  105. }
  106. func getDate(a *atomEntry) time.Time {
  107. if a.Updated != "" {
  108. result, err := date.Parse(a.Updated)
  109. if err != nil {
  110. logger.Error("atom: %v", err)
  111. return time.Now()
  112. }
  113. return result
  114. }
  115. return time.Now()
  116. }
  117. func getContent(a *atomEntry) string {
  118. if a.Content.Type == "html" || a.Content.Type == "text" {
  119. return a.Content.Data
  120. }
  121. if a.Content.Type == "xhtml" {
  122. return a.Content.XML
  123. }
  124. if a.Summary != "" {
  125. return a.Summary
  126. }
  127. if a.MediaGroup.Description != "" {
  128. return a.MediaGroup.Description
  129. }
  130. return ""
  131. }
  132. func getHash(a *atomEntry) string {
  133. for _, value := range []string{a.ID, getURL(a.Links)} {
  134. if value != "" {
  135. return helper.Hash(value)
  136. }
  137. }
  138. return ""
  139. }
  140. func getEnclosures(a *atomEntry) model.EnclosureList {
  141. enclosures := make(model.EnclosureList, 0)
  142. for _, link := range a.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 getAuthor(author atomAuthor) string {
  151. if author.Name != "" {
  152. return strings.TrimSpace(author.Name)
  153. }
  154. if author.Email != "" {
  155. return strings.TrimSpace(author.Email)
  156. }
  157. return ""
  158. }