processor.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package processor // import "miniflux.app/v2/internal/reader/processor"
  4. import (
  5. "log/slog"
  6. "net/url"
  7. "slices"
  8. "time"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/metric"
  11. "miniflux.app/v2/internal/model"
  12. "miniflux.app/v2/internal/proxyrotator"
  13. "miniflux.app/v2/internal/reader/fetcher"
  14. "miniflux.app/v2/internal/reader/filter"
  15. "miniflux.app/v2/internal/reader/readingtime"
  16. "miniflux.app/v2/internal/reader/rewrite"
  17. "miniflux.app/v2/internal/reader/sanitizer"
  18. "miniflux.app/v2/internal/reader/scraper"
  19. "miniflux.app/v2/internal/reader/urlcleaner"
  20. "miniflux.app/v2/internal/storage"
  21. )
  22. // ProcessFeedEntries downloads original web page for entries and apply filters.
  23. func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, userID int64, forceRefresh bool) {
  24. var filteredEntries model.Entries
  25. user, storeErr := store.UserByID(userID)
  26. if storeErr != nil {
  27. slog.Error("Database error", slog.Any("error", storeErr))
  28. return
  29. }
  30. // The errors are handled in RemoveTrackingParameters.
  31. parsedFeedURL, _ := url.Parse(feed.FeedURL)
  32. parsedSiteURL, _ := url.Parse(feed.SiteURL)
  33. blockRules := filter.ParseRules(user.BlockFilterEntryRules, feed.BlockFilterEntryRules)
  34. allowRules := filter.ParseRules(user.KeepFilterEntryRules, feed.KeepFilterEntryRules)
  35. slog.Debug("Filter rules",
  36. slog.String("user_block_filter_rules", user.BlockFilterEntryRules),
  37. slog.String("feed_block_filter_rules", feed.BlockFilterEntryRules),
  38. slog.String("user_keep_filter_rules", user.KeepFilterEntryRules),
  39. slog.String("feed_keep_filter_rules", feed.KeepFilterEntryRules),
  40. slog.Any("block_rules", blockRules),
  41. slog.Any("allow_rules", allowRules),
  42. slog.Int64("user_id", user.ID),
  43. slog.Int64("feed_id", feed.ID),
  44. )
  45. requestBuilder := fetcher.NewRequestBuilder()
  46. requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
  47. requestBuilder.WithCookie(feed.Cookie)
  48. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  49. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  50. requestBuilder.WithCustomFeedProxyURL(feed.ProxyURL)
  51. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  52. requestBuilder.UseCustomApplicationProxyURL(feed.FetchViaProxy)
  53. requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
  54. requestBuilder.DisableHTTP2(feed.DisableHTTP2)
  55. // Processing older entries first ensures that their creation timestamp is lower than newer entries.
  56. for _, entry := range slices.Backward(feed.Entries) {
  57. slog.Debug("Processing entry",
  58. slog.Int64("user_id", user.ID),
  59. slog.String("entry_url", entry.URL),
  60. slog.String("entry_hash", entry.Hash),
  61. slog.String("entry_title", entry.Title),
  62. slog.Int64("feed_id", feed.ID),
  63. slog.String("feed_url", feed.FeedURL),
  64. )
  65. if filter.IsBlockedEntry(blockRules, allowRules, feed, entry) {
  66. slog.Debug("Entry is blocked by filter rules",
  67. slog.Int64("user_id", user.ID),
  68. slog.String("entry_url", entry.URL),
  69. slog.String("entry_hash", entry.Hash),
  70. slog.String("entry_title", entry.Title),
  71. slog.Int64("feed_id", feed.ID),
  72. slog.String("feed_url", feed.FeedURL),
  73. )
  74. continue
  75. }
  76. parsedInputUrl, _ := url.Parse(entry.URL)
  77. if cleanedURL, err := urlcleaner.RemoveTrackingParameters(parsedFeedURL, parsedSiteURL, parsedInputUrl); err == nil {
  78. entry.URL = cleanedURL
  79. }
  80. webpageBaseURL := ""
  81. entry.URL = rewrite.RewriteEntryURL(feed, entry)
  82. entryIsNew := store.IsNewEntry(feed.ID, entry.Hash)
  83. if feed.Crawler && (entryIsNew || forceRefresh) {
  84. slog.Debug("Scraping entry",
  85. slog.Int64("user_id", user.ID),
  86. slog.String("entry_url", entry.URL),
  87. slog.String("entry_hash", entry.Hash),
  88. slog.String("entry_title", entry.Title),
  89. slog.Int64("feed_id", feed.ID),
  90. slog.String("feed_url", feed.FeedURL),
  91. slog.Bool("entry_is_new", entryIsNew),
  92. slog.Bool("force_refresh", forceRefresh),
  93. )
  94. startTime := time.Now()
  95. scrapedPageBaseURL, extractedContent, scraperErr := scraper.ScrapeWebsite(
  96. requestBuilder,
  97. entry.URL,
  98. feed.ScraperRules,
  99. )
  100. if scrapedPageBaseURL != "" {
  101. webpageBaseURL = scrapedPageBaseURL
  102. }
  103. if config.Opts.HasMetricsCollector() {
  104. status := "success"
  105. if scraperErr != nil {
  106. status = "error"
  107. }
  108. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  109. }
  110. if scraperErr != nil {
  111. slog.Warn("Unable to scrape entry",
  112. slog.Int64("user_id", user.ID),
  113. slog.String("entry_url", entry.URL),
  114. slog.Int64("feed_id", feed.ID),
  115. slog.String("feed_url", feed.FeedURL),
  116. slog.Any("error", scraperErr),
  117. )
  118. } else if extractedContent != "" {
  119. // We replace the entry content only if the scraper doesn't return any error.
  120. entry.Content = minifyContent(extractedContent)
  121. }
  122. }
  123. rewrite.ApplyContentRewriteRules(entry, feed.RewriteRules)
  124. if webpageBaseURL == "" {
  125. webpageBaseURL = entry.URL
  126. }
  127. // The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered out.
  128. entry.Content = sanitizer.SanitizeHTML(webpageBaseURL, entry.Content, &sanitizer.SanitizerOptions{OpenLinksInNewTab: user.OpenExternalLinksInNewTab})
  129. updateEntryReadingTime(store, feed, entry, entryIsNew, user)
  130. filteredEntries = append(filteredEntries, entry)
  131. }
  132. if user.ShowReadingTime && shouldFetchYouTubeWatchTimeInBulk() {
  133. fetchYouTubeWatchTimeInBulk(filteredEntries)
  134. }
  135. feed.Entries = filteredEntries
  136. }
  137. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
  138. func ProcessEntryWebPage(feed *model.Feed, entry *model.Entry, user *model.User) error {
  139. startTime := time.Now()
  140. entry.URL = rewrite.RewriteEntryURL(feed, entry)
  141. requestBuilder := fetcher.NewRequestBuilder()
  142. requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
  143. requestBuilder.WithCookie(feed.Cookie)
  144. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  145. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  146. requestBuilder.WithCustomFeedProxyURL(feed.ProxyURL)
  147. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  148. requestBuilder.UseCustomApplicationProxyURL(feed.FetchViaProxy)
  149. requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
  150. requestBuilder.DisableHTTP2(feed.DisableHTTP2)
  151. webpageBaseURL, extractedContent, scraperErr := scraper.ScrapeWebsite(
  152. requestBuilder,
  153. entry.URL,
  154. feed.ScraperRules,
  155. )
  156. if config.Opts.HasMetricsCollector() {
  157. status := "success"
  158. if scraperErr != nil {
  159. status = "error"
  160. }
  161. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  162. }
  163. if scraperErr != nil {
  164. return scraperErr
  165. }
  166. if extractedContent != "" {
  167. entry.Content = minifyContent(extractedContent)
  168. if user.ShowReadingTime {
  169. entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
  170. }
  171. }
  172. rewrite.ApplyContentRewriteRules(entry, entry.Feed.RewriteRules)
  173. entry.Content = sanitizer.SanitizeHTML(webpageBaseURL, entry.Content, &sanitizer.SanitizerOptions{OpenLinksInNewTab: user.OpenExternalLinksInNewTab})
  174. return nil
  175. }