rss.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/date"
  15. "github.com/miniflux/miniflux2/reader/processor"
  16. )
  17. type rssFeed struct {
  18. XMLName xml.Name `xml:"rss"`
  19. Version string `xml:"version,attr"`
  20. Title string `xml:"channel>title"`
  21. Links []rssLink `xml:"channel>link"`
  22. Language string `xml:"channel>language"`
  23. Description string `xml:"channel>description"`
  24. PubDate string `xml:"channel>pubDate"`
  25. ItunesAuthor string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>author"`
  26. Items []rssItem `xml:"channel>item"`
  27. }
  28. type rssLink struct {
  29. XMLName xml.Name
  30. Data string `xml:",chardata"`
  31. Href string `xml:"href,attr"`
  32. Rel string `xml:"rel,attr"`
  33. }
  34. type rssItem struct {
  35. GUID string `xml:"guid"`
  36. Title string `xml:"title"`
  37. Links []rssLink `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 _, element := range r.Links {
  60. if element.XMLName.Space == "" {
  61. return strings.TrimSpace(element.Data)
  62. }
  63. }
  64. return ""
  65. }
  66. func (r *rssFeed) GetFeedURL() string {
  67. for _, element := range r.Links {
  68. if element.XMLName.Space == "http://www.w3.org/2005/Atom" {
  69. return strings.TrimSpace(element.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 = strings.TrimSpace(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 = strings.TrimSpace(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 (r *rssItem) GetDate() time.Time {
  96. value := r.PubDate
  97. if r.Date != "" {
  98. value = r.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 (r *rssItem) GetAuthor() string {
  111. for _, element := range r.Authors {
  112. if element.Name != "" {
  113. return element.Name
  114. }
  115. if element.Data != "" {
  116. return element.Data
  117. }
  118. }
  119. return r.Creator
  120. }
  121. func (r *rssItem) GetHash() string {
  122. for _, value := range []string{r.GUID, r.GetURL()} {
  123. if value != "" {
  124. return helper.Hash(value)
  125. }
  126. }
  127. return ""
  128. }
  129. func (r *rssItem) GetContent() string {
  130. if r.Content != "" {
  131. return r.Content
  132. }
  133. return r.Description
  134. }
  135. func (r *rssItem) GetURL() string {
  136. if r.OriginalLink != "" {
  137. return r.OriginalLink
  138. }
  139. for _, link := range r.Links {
  140. if link.XMLName.Space == "http://www.w3.org/2005/Atom" && link.Href != "" && isValidLinkRelation(link.Rel) {
  141. return strings.TrimSpace(link.Href)
  142. }
  143. if link.Data != "" {
  144. return strings.TrimSpace(link.Data)
  145. }
  146. }
  147. return ""
  148. }
  149. func (r *rssItem) GetEnclosures() model.EnclosureList {
  150. enclosures := make(model.EnclosureList, 0)
  151. for _, enclosure := range r.Enclosures {
  152. length, _ := strconv.Atoi(enclosure.Length)
  153. enclosureURL := enclosure.URL
  154. if r.OrigEnclosureLink != "" {
  155. filename := path.Base(r.OrigEnclosureLink)
  156. if strings.Contains(enclosureURL, filename) {
  157. enclosureURL = r.OrigEnclosureLink
  158. }
  159. }
  160. enclosures = append(enclosures, &model.Enclosure{
  161. URL: enclosureURL,
  162. MimeType: enclosure.Type,
  163. Size: length,
  164. })
  165. }
  166. return enclosures
  167. }
  168. func (r *rssItem) Transform() *model.Entry {
  169. entry := new(model.Entry)
  170. entry.URL = r.GetURL()
  171. entry.Date = r.GetDate()
  172. entry.Author = r.GetAuthor()
  173. entry.Hash = r.GetHash()
  174. entry.Content = processor.ItemContentProcessor(entry.URL, r.GetContent())
  175. entry.Title = strings.TrimSpace(r.Title)
  176. entry.Enclosures = r.GetEnclosures()
  177. if entry.Title == "" {
  178. entry.Title = entry.URL
  179. }
  180. return entry
  181. }
  182. func isValidLinkRelation(rel string) bool {
  183. switch rel {
  184. case "", "alternate", "enclosure", "related", "self", "via":
  185. return true
  186. default:
  187. if strings.HasPrefix(rel, "http") {
  188. return true
  189. }
  190. return false
  191. }
  192. }