rss.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 rss
  5. import (
  6. "encoding/xml"
  7. "log"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/miniflux/miniflux2/helper"
  13. "github.com/miniflux/miniflux2/model"
  14. "github.com/miniflux/miniflux2/reader/feed/date"
  15. "github.com/miniflux/miniflux2/reader/processor"
  16. "github.com/miniflux/miniflux2/reader/sanitizer"
  17. )
  18. type rssFeed struct {
  19. XMLName xml.Name `xml:"rss"`
  20. Version string `xml:"version,attr"`
  21. Title string `xml:"channel>title"`
  22. Links []rssLink `xml:"channel>link"`
  23. Language string `xml:"channel>language"`
  24. Description string `xml:"channel>description"`
  25. PubDate string `xml:"channel>pubDate"`
  26. ItunesAuthor string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>author"`
  27. Items []rssItem `xml:"channel>item"`
  28. }
  29. type rssLink struct {
  30. XMLName xml.Name
  31. Data string `xml:",chardata"`
  32. Href string `xml:"href,attr"`
  33. }
  34. type rssItem struct {
  35. GUID string `xml:"guid"`
  36. Title string `xml:"title"`
  37. Link string `xml:"link"`
  38. OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"`
  39. Description string `xml:"description"`
  40. Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
  41. PubDate string `xml:"pubDate"`
  42. Date string `xml:"http://purl.org/dc/elements/1.1/ date"`
  43. Authors []rssAuthor `xml:"author"`
  44. Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
  45. Enclosures []rssEnclosure `xml:"enclosure"`
  46. OrigEnclosureLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origEnclosureLink"`
  47. }
  48. type rssAuthor struct {
  49. XMLName xml.Name
  50. Data string `xml:",chardata"`
  51. Name string `xml:"name"`
  52. }
  53. type rssEnclosure struct {
  54. URL string `xml:"url,attr"`
  55. Type string `xml:"type,attr"`
  56. Length string `xml:"length,attr"`
  57. }
  58. func (r *rssFeed) GetSiteURL() string {
  59. for _, elem := range r.Links {
  60. if elem.XMLName.Space == "" {
  61. return elem.Data
  62. }
  63. }
  64. return ""
  65. }
  66. func (r *rssFeed) GetFeedURL() string {
  67. for _, elem := range r.Links {
  68. if elem.XMLName.Space == "http://www.w3.org/2005/Atom" {
  69. return elem.Href
  70. }
  71. }
  72. return ""
  73. }
  74. func (r *rssFeed) Transform() *model.Feed {
  75. feed := new(model.Feed)
  76. feed.SiteURL = r.GetSiteURL()
  77. feed.FeedURL = r.GetFeedURL()
  78. feed.Title = sanitizer.StripTags(r.Title)
  79. if feed.Title == "" {
  80. feed.Title = feed.SiteURL
  81. }
  82. for _, item := range r.Items {
  83. entry := item.Transform()
  84. if entry.Author == "" && r.ItunesAuthor != "" {
  85. entry.Author = r.ItunesAuthor
  86. }
  87. entry.Author = sanitizer.StripTags(entry.Author)
  88. if entry.URL == "" {
  89. entry.URL = feed.SiteURL
  90. }
  91. feed.Entries = append(feed.Entries, entry)
  92. }
  93. return feed
  94. }
  95. func (i *rssItem) GetDate() time.Time {
  96. value := i.PubDate
  97. if i.Date != "" {
  98. value = i.Date
  99. }
  100. if value != "" {
  101. result, err := date.Parse(value)
  102. if err != nil {
  103. log.Println(err)
  104. return time.Now()
  105. }
  106. return result
  107. }
  108. return time.Now()
  109. }
  110. func (i *rssItem) GetAuthor() string {
  111. for _, element := range i.Authors {
  112. if element.Name != "" {
  113. return element.Name
  114. }
  115. if element.Data != "" {
  116. return element.Data
  117. }
  118. }
  119. return i.Creator
  120. }
  121. func (i *rssItem) GetHash() string {
  122. for _, value := range []string{i.GUID, i.Link} {
  123. if value != "" {
  124. return helper.Hash(value)
  125. }
  126. }
  127. return ""
  128. }
  129. func (i *rssItem) GetContent() string {
  130. if i.Content != "" {
  131. return i.Content
  132. }
  133. return i.Description
  134. }
  135. func (i *rssItem) GetURL() string {
  136. if i.OriginalLink != "" {
  137. return i.OriginalLink
  138. }
  139. return i.Link
  140. }
  141. func (i *rssItem) GetEnclosures() model.EnclosureList {
  142. enclosures := make(model.EnclosureList, 0)
  143. for _, enclosure := range i.Enclosures {
  144. length, _ := strconv.Atoi(enclosure.Length)
  145. enclosureURL := enclosure.URL
  146. if i.OrigEnclosureLink != "" {
  147. filename := path.Base(i.OrigEnclosureLink)
  148. if strings.Contains(enclosureURL, filename) {
  149. enclosureURL = i.OrigEnclosureLink
  150. }
  151. }
  152. enclosures = append(enclosures, &model.Enclosure{
  153. URL: enclosureURL,
  154. MimeType: enclosure.Type,
  155. Size: length,
  156. })
  157. }
  158. return enclosures
  159. }
  160. func (i *rssItem) Transform() *model.Entry {
  161. entry := new(model.Entry)
  162. entry.URL = i.GetURL()
  163. entry.Date = i.GetDate()
  164. entry.Author = i.GetAuthor()
  165. entry.Hash = i.GetHash()
  166. entry.Content = processor.ItemContentProcessor(entry.URL, i.GetContent())
  167. entry.Title = sanitizer.StripTags(strings.Trim(i.Title, " \n\t"))
  168. entry.Enclosures = i.GetEnclosures()
  169. if entry.Title == "" {
  170. entry.Title = entry.URL
  171. }
  172. return entry
  173. }