rss.go 8.5 KB

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