rss.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 rssTitle struct {
  106. XMLName xml.Name
  107. Data string `xml:",chardata"`
  108. Inner string `xml:",innerxml"`
  109. }
  110. type rssEnclosure struct {
  111. URL string `xml:"url,attr"`
  112. Type string `xml:"type,attr"`
  113. Length string `xml:"length,attr"`
  114. }
  115. func (enclosure *rssEnclosure) Size() int64 {
  116. if enclosure.Length == "" {
  117. return 0
  118. }
  119. size, _ := strconv.ParseInt(enclosure.Length, 10, 0)
  120. return size
  121. }
  122. type rssItem struct {
  123. GUID string `xml:"guid"`
  124. Title []rssTitle `xml:"title"`
  125. Links []rssLink `xml:"link"`
  126. Description string `xml:"description"`
  127. PubDate string `xml:"pubDate"`
  128. Authors []rssAuthor `xml:"author"`
  129. CommentLinks []rssCommentLink `xml:"comments"`
  130. EnclosureLinks []rssEnclosure `xml:"enclosure"`
  131. DublinCoreElement
  132. FeedBurnerElement
  133. PodcastEntryElement
  134. media.Element
  135. }
  136. func (r *rssItem) Transform() *model.Entry {
  137. entry := new(model.Entry)
  138. entry.URL = r.entryURL()
  139. entry.CommentsURL = r.entryCommentsURL()
  140. entry.Date = r.entryDate()
  141. entry.Author = r.entryAuthor()
  142. entry.Hash = r.entryHash()
  143. entry.Content = r.entryContent()
  144. entry.Title = r.entryTitle()
  145. entry.Enclosures = r.entryEnclosures()
  146. return entry
  147. }
  148. func (r *rssItem) entryDate() time.Time {
  149. value := r.PubDate
  150. if r.DublinCoreDate != "" {
  151. value = r.DublinCoreDate
  152. }
  153. if value != "" {
  154. result, err := date.Parse(value)
  155. if err != nil {
  156. logger.Error("rss: %v", err)
  157. return time.Now()
  158. }
  159. return result
  160. }
  161. return time.Now()
  162. }
  163. func (r *rssItem) entryAuthor() string {
  164. author := ""
  165. for _, rssAuthor := range r.Authors {
  166. switch rssAuthor.XMLName.Space {
  167. case "http://www.itunes.com/dtds/podcast-1.0.dtd", "http://www.google.com/schemas/play-podcasts/1.0":
  168. author = rssAuthor.Data
  169. case "http://www.w3.org/2005/Atom":
  170. if rssAuthor.Name != "" {
  171. author = rssAuthor.Name
  172. } else if rssAuthor.Email != "" {
  173. author = rssAuthor.Email
  174. }
  175. default:
  176. if rssAuthor.Name != "" {
  177. author = rssAuthor.Name
  178. } else {
  179. author = rssAuthor.Inner
  180. }
  181. }
  182. }
  183. if author == "" {
  184. author = r.DublinCoreCreator
  185. }
  186. return strings.TrimSpace(author)
  187. }
  188. func (r *rssItem) entryHash() string {
  189. for _, value := range []string{r.GUID, r.entryURL()} {
  190. if value != "" {
  191. return crypto.Hash(value)
  192. }
  193. }
  194. return ""
  195. }
  196. func (r *rssItem) entryTitle() string {
  197. var title string
  198. for _, rssTitle := range r.Title {
  199. switch rssTitle.XMLName.Space {
  200. case "http://search.yahoo.com/mrss/":
  201. // Ignore title in media namespace
  202. case "http://purl.org/dc/elements/1.1/":
  203. title = rssTitle.Data
  204. default:
  205. title = rssTitle.Data
  206. }
  207. if title != "" {
  208. break
  209. }
  210. }
  211. return strings.TrimSpace(sanitizer.StripTags(title))
  212. }
  213. func (r *rssItem) entryContent() string {
  214. for _, value := range []string{r.DublinCoreContent, r.Description, r.PodcastDescription()} {
  215. if value != "" {
  216. return value
  217. }
  218. }
  219. return ""
  220. }
  221. func (r *rssItem) entryURL() string {
  222. if r.FeedBurnerLink != "" {
  223. return r.FeedBurnerLink
  224. }
  225. for _, link := range r.Links {
  226. if link.XMLName.Space == "http://www.w3.org/2005/Atom" && link.Href != "" && isValidLinkRelation(link.Rel) {
  227. return strings.TrimSpace(link.Href)
  228. }
  229. if link.Data != "" {
  230. return strings.TrimSpace(link.Data)
  231. }
  232. }
  233. return ""
  234. }
  235. func (r *rssItem) entryEnclosures() model.EnclosureList {
  236. enclosures := make(model.EnclosureList, 0)
  237. duplicates := make(map[string]bool, 0)
  238. for _, mediaThumbnail := range r.AllMediaThumbnails() {
  239. if _, found := duplicates[mediaThumbnail.URL]; !found {
  240. duplicates[mediaThumbnail.URL] = true
  241. enclosures = append(enclosures, &model.Enclosure{
  242. URL: mediaThumbnail.URL,
  243. MimeType: mediaThumbnail.MimeType(),
  244. Size: mediaThumbnail.Size(),
  245. })
  246. }
  247. }
  248. for _, enclosure := range r.EnclosureLinks {
  249. enclosureURL := enclosure.URL
  250. if r.FeedBurnerEnclosureLink != "" {
  251. filename := path.Base(r.FeedBurnerEnclosureLink)
  252. if strings.Contains(enclosureURL, filename) {
  253. enclosureURL = r.FeedBurnerEnclosureLink
  254. }
  255. }
  256. if enclosureURL == "" {
  257. continue
  258. }
  259. if _, found := duplicates[enclosureURL]; !found {
  260. duplicates[enclosureURL] = true
  261. enclosures = append(enclosures, &model.Enclosure{
  262. URL: enclosureURL,
  263. MimeType: enclosure.Type,
  264. Size: enclosure.Size(),
  265. })
  266. }
  267. }
  268. for _, mediaContent := range r.AllMediaContents() {
  269. if _, found := duplicates[mediaContent.URL]; !found {
  270. duplicates[mediaContent.URL] = true
  271. enclosures = append(enclosures, &model.Enclosure{
  272. URL: mediaContent.URL,
  273. MimeType: mediaContent.MimeType(),
  274. Size: mediaContent.Size(),
  275. })
  276. }
  277. }
  278. for _, mediaPeerLink := range r.AllMediaPeerLinks() {
  279. if _, found := duplicates[mediaPeerLink.URL]; !found {
  280. duplicates[mediaPeerLink.URL] = true
  281. enclosures = append(enclosures, &model.Enclosure{
  282. URL: mediaPeerLink.URL,
  283. MimeType: mediaPeerLink.MimeType(),
  284. Size: mediaPeerLink.Size(),
  285. })
  286. }
  287. }
  288. return enclosures
  289. }
  290. func (r *rssItem) entryCommentsURL() string {
  291. for _, commentLink := range r.CommentLinks {
  292. if commentLink.XMLName.Space == "" {
  293. commentsURL := strings.TrimSpace(commentLink.Data)
  294. // The comments URL is supposed to be absolute (some feeds publishes incorrect comments URL)
  295. // See https://cyber.harvard.edu/rss/rss.html#ltcommentsgtSubelementOfLtitemgt
  296. if url.IsAbsoluteURL(commentsURL) {
  297. return commentsURL
  298. }
  299. }
  300. }
  301. return ""
  302. }
  303. func isValidLinkRelation(rel string) bool {
  304. switch rel {
  305. case "", "alternate", "enclosure", "related", "self", "via":
  306. return true
  307. default:
  308. if strings.HasPrefix(rel, "http") {
  309. return true
  310. }
  311. return false
  312. }
  313. }