rss.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 // import "miniflux.app/reader/rss"
  5. import (
  6. "encoding/xml"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "miniflux.app/crypto"
  12. "miniflux.app/logger"
  13. "miniflux.app/model"
  14. "miniflux.app/reader/date"
  15. "miniflux.app/reader/media"
  16. "miniflux.app/reader/sanitizer"
  17. "miniflux.app/url"
  18. )
  19. // Specs: https://cyber.harvard.edu/rss/rss.html
  20. type rssFeed struct {
  21. XMLName xml.Name `xml:"rss"`
  22. Version string `xml:"version,attr"`
  23. Title string `xml:"channel>title"`
  24. Links []rssLink `xml:"channel>link"`
  25. Language string `xml:"channel>language"`
  26. Description string `xml:"channel>description"`
  27. PubDate string `xml:"channel>pubDate"`
  28. ManagingEditor string `xml:"channel>managingEditor"`
  29. Webmaster string `xml:"channel>webMaster"`
  30. Items []rssItem `xml:"channel>item"`
  31. PodcastFeedElement
  32. }
  33. func (r *rssFeed) Transform() *model.Feed {
  34. feed := new(model.Feed)
  35. feed.SiteURL = r.siteURL()
  36. feed.FeedURL = r.feedURL()
  37. feed.Title = strings.TrimSpace(r.Title)
  38. if feed.Title == "" {
  39. feed.Title = feed.SiteURL
  40. }
  41. for _, item := range r.Items {
  42. entry := item.Transform()
  43. if entry.Author == "" {
  44. entry.Author = r.feedAuthor()
  45. }
  46. entry.Author = sanitizer.StripTags(entry.Author)
  47. if entry.URL == "" {
  48. entry.URL = feed.SiteURL
  49. } else {
  50. entryURL, err := url.AbsoluteURL(feed.SiteURL, entry.URL)
  51. if err == nil {
  52. entry.URL = entryURL
  53. }
  54. }
  55. if entry.Title == "" {
  56. entry.Title = entry.URL
  57. }
  58. feed.Entries = append(feed.Entries, entry)
  59. }
  60. return feed
  61. }
  62. func (r *rssFeed) siteURL() string {
  63. for _, element := range r.Links {
  64. if element.XMLName.Space == "" {
  65. return strings.TrimSpace(element.Data)
  66. }
  67. }
  68. return ""
  69. }
  70. func (r *rssFeed) feedURL() string {
  71. for _, element := range r.Links {
  72. if element.XMLName.Space == "http://www.w3.org/2005/Atom" {
  73. return strings.TrimSpace(element.Href)
  74. }
  75. }
  76. return ""
  77. }
  78. func (r rssFeed) feedAuthor() string {
  79. author := r.PodcastAuthor()
  80. switch {
  81. case r.ManagingEditor != "":
  82. author = r.ManagingEditor
  83. case r.Webmaster != "":
  84. author = r.Webmaster
  85. }
  86. return strings.TrimSpace(author)
  87. }
  88. type rssLink struct {
  89. XMLName xml.Name
  90. Data string `xml:",chardata"`
  91. Href string `xml:"href,attr"`
  92. Rel string `xml:"rel,attr"`
  93. }
  94. type rssCommentLink struct {
  95. XMLName xml.Name
  96. Data string `xml:",chardata"`
  97. }
  98. type rssAuthor struct {
  99. XMLName xml.Name
  100. Data string `xml:",chardata"`
  101. Name string `xml:"name"`
  102. Email string `xml:"email"`
  103. Inner string `xml:",innerxml"`
  104. }
  105. type rssEnclosure struct {
  106. URL string `xml:"url,attr"`
  107. Type string `xml:"type,attr"`
  108. Length string `xml:"length,attr"`
  109. }
  110. func (enclosure *rssEnclosure) Size() int64 {
  111. if enclosure.Length == "" {
  112. return 0
  113. }
  114. size, _ := strconv.ParseInt(enclosure.Length, 10, 0)
  115. return size
  116. }
  117. type rssItem struct {
  118. GUID string `xml:"guid"`
  119. Title string `xml:"title"`
  120. Links []rssLink `xml:"link"`
  121. Description string `xml:"description"`
  122. PubDate string `xml:"pubDate"`
  123. Authors []rssAuthor `xml:"author"`
  124. CommentLinks []rssCommentLink `xml:"comments"`
  125. EnclosureLinks []rssEnclosure `xml:"enclosure"`
  126. DublinCoreElement
  127. FeedBurnerElement
  128. PodcastEntryElement
  129. media.Element
  130. }
  131. func (r *rssItem) Transform() *model.Entry {
  132. entry := new(model.Entry)
  133. entry.URL = r.entryURL()
  134. entry.CommentsURL = r.entryCommentsURL()
  135. entry.Date = r.entryDate()
  136. entry.Author = r.entryAuthor()
  137. entry.Hash = r.entryHash()
  138. entry.Content = r.entryContent()
  139. entry.Title = r.entryTitle()
  140. entry.Enclosures = r.entryEnclosures()
  141. return entry
  142. }
  143. func (r *rssItem) entryDate() time.Time {
  144. value := r.PubDate
  145. if r.DublinCoreDate != "" {
  146. value = r.DublinCoreDate
  147. }
  148. if value != "" {
  149. result, err := date.Parse(value)
  150. if err != nil {
  151. logger.Error("rss: %v", err)
  152. return time.Now()
  153. }
  154. return result
  155. }
  156. return time.Now()
  157. }
  158. func (r *rssItem) entryAuthor() string {
  159. author := ""
  160. for _, rssAuthor := range r.Authors {
  161. switch rssAuthor.XMLName.Space {
  162. case "http://www.itunes.com/dtds/podcast-1.0.dtd", "http://www.google.com/schemas/play-podcasts/1.0":
  163. author = rssAuthor.Data
  164. case "http://www.w3.org/2005/Atom":
  165. if rssAuthor.Name != "" {
  166. author = rssAuthor.Name
  167. } else if rssAuthor.Email != "" {
  168. author = rssAuthor.Email
  169. }
  170. default:
  171. if rssAuthor.Name != "" {
  172. author = rssAuthor.Name
  173. } else {
  174. author = rssAuthor.Inner
  175. }
  176. }
  177. }
  178. if author == "" {
  179. author = r.DublinCoreCreator
  180. }
  181. return strings.TrimSpace(author)
  182. }
  183. func (r *rssItem) entryHash() string {
  184. for _, value := range []string{r.GUID, r.entryURL()} {
  185. if value != "" {
  186. return crypto.Hash(value)
  187. }
  188. }
  189. return ""
  190. }
  191. func (r *rssItem) entryTitle() string {
  192. return strings.TrimSpace(sanitizer.StripTags(r.Title))
  193. }
  194. func (r *rssItem) entryContent() string {
  195. for _, value := range []string{r.DublinCoreContent, r.Description, r.PodcastDescription()} {
  196. if value != "" {
  197. return value
  198. }
  199. }
  200. return ""
  201. }
  202. func (r *rssItem) entryURL() string {
  203. if r.FeedBurnerLink != "" {
  204. return r.FeedBurnerLink
  205. }
  206. for _, link := range r.Links {
  207. if link.XMLName.Space == "http://www.w3.org/2005/Atom" && link.Href != "" && isValidLinkRelation(link.Rel) {
  208. return strings.TrimSpace(link.Href)
  209. }
  210. if link.Data != "" {
  211. return strings.TrimSpace(link.Data)
  212. }
  213. }
  214. return ""
  215. }
  216. func (r *rssItem) entryEnclosures() model.EnclosureList {
  217. enclosures := make(model.EnclosureList, 0)
  218. duplicates := make(map[string]bool, 0)
  219. for _, mediaThumbnail := range r.AllMediaThumbnails() {
  220. if _, found := duplicates[mediaThumbnail.URL]; !found {
  221. duplicates[mediaThumbnail.URL] = true
  222. enclosures = append(enclosures, &model.Enclosure{
  223. URL: mediaThumbnail.URL,
  224. MimeType: mediaThumbnail.MimeType(),
  225. Size: mediaThumbnail.Size(),
  226. })
  227. }
  228. }
  229. for _, enclosure := range r.EnclosureLinks {
  230. enclosureURL := enclosure.URL
  231. if r.FeedBurnerEnclosureLink != "" {
  232. filename := path.Base(r.FeedBurnerEnclosureLink)
  233. if strings.Contains(enclosureURL, filename) {
  234. enclosureURL = r.FeedBurnerEnclosureLink
  235. }
  236. }
  237. if enclosureURL == "" {
  238. continue
  239. }
  240. if _, found := duplicates[enclosureURL]; !found {
  241. duplicates[enclosureURL] = true
  242. enclosures = append(enclosures, &model.Enclosure{
  243. URL: enclosureURL,
  244. MimeType: enclosure.Type,
  245. Size: enclosure.Size(),
  246. })
  247. }
  248. }
  249. for _, mediaContent := range r.AllMediaContents() {
  250. if _, found := duplicates[mediaContent.URL]; !found {
  251. duplicates[mediaContent.URL] = true
  252. enclosures = append(enclosures, &model.Enclosure{
  253. URL: mediaContent.URL,
  254. MimeType: mediaContent.MimeType(),
  255. Size: mediaContent.Size(),
  256. })
  257. }
  258. }
  259. for _, mediaPeerLink := range r.AllMediaPeerLinks() {
  260. if _, found := duplicates[mediaPeerLink.URL]; !found {
  261. duplicates[mediaPeerLink.URL] = true
  262. enclosures = append(enclosures, &model.Enclosure{
  263. URL: mediaPeerLink.URL,
  264. MimeType: mediaPeerLink.MimeType(),
  265. Size: mediaPeerLink.Size(),
  266. })
  267. }
  268. }
  269. return enclosures
  270. }
  271. func (r *rssItem) entryCommentsURL() string {
  272. for _, commentLink := range r.CommentLinks {
  273. if commentLink.XMLName.Space == "" {
  274. commentsURL := strings.TrimSpace(commentLink.Data)
  275. // The comments URL is supposed to be absolute (some feeds publishes incorrect comments URL)
  276. // See https://cyber.harvard.edu/rss/rss.html#ltcommentsgtSubelementOfLtitemgt
  277. if url.IsAbsoluteURL(commentsURL) {
  278. return commentsURL
  279. }
  280. }
  281. }
  282. return ""
  283. }
  284. func isValidLinkRelation(rel string) bool {
  285. switch rel {
  286. case "", "alternate", "enclosure", "related", "self", "via":
  287. return true
  288. default:
  289. if strings.HasPrefix(rel, "http") {
  290. return true
  291. }
  292. return false
  293. }
  294. }