4
0

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. // Processing older entries first ensures that their creation timestamp is lower than newer entries.
  46. for _, entry := range slices.Backward(feed.Entries) {
  47. slog.Debug("Processing entry",
  48. slog.Int64("user_id", user.ID),
  49. slog.String("entry_url", entry.URL),
  50. slog.String("entry_hash", entry.Hash),
  51. slog.String("entry_title", entry.Title),
  52. slog.Int64("feed_id", feed.ID),
  53. slog.String("feed_url", feed.FeedURL),
  54. )
  55. if filter.IsBlockedEntry(blockRules, allowRules, feed, entry) {
  56. slog.Debug("Entry is blocked by filter rules",
  57. slog.Int64("user_id", user.ID),
  58. slog.String("entry_url", entry.URL),
  59. slog.String("entry_hash", entry.Hash),
  60. slog.String("entry_title", entry.Title),
  61. slog.Int64("feed_id", feed.ID),
  62. slog.String("feed_url", feed.FeedURL),
  63. )
  64. continue
  65. }
  66. parsedInputUrl, _ := url.Parse(entry.URL)
  67. if cleanedURL, err := urlcleaner.RemoveTrackingParameters(parsedFeedURL, parsedSiteURL, parsedInputUrl); err == nil {
  68. entry.URL = cleanedURL
  69. }
  70. webpageBaseURL := ""
  71. entry.URL = rewrite.RewriteEntryURL(feed, entry)
  72. entryIsNew := store.IsNewEntry(feed.ID, entry.Hash)
  73. if feed.Crawler && (entryIsNew || forceRefresh) {
  74. slog.Debug("Scraping entry",
  75. slog.Int64("user_id", user.ID),
  76. slog.String("entry_url", entry.URL),
  77. slog.String("entry_hash", entry.Hash),
  78. slog.String("entry_title", entry.Title),
  79. slog.Int64("feed_id", feed.ID),
  80. slog.String("feed_url", feed.FeedURL),
  81. slog.Bool("entry_is_new", entryIsNew),
  82. slog.Bool("force_refresh", forceRefresh),
  83. )
  84. startTime := time.Now()
  85. requestBuilder := fetcher.NewRequestBuilder()
  86. requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
  87. requestBuilder.WithCookie(feed.Cookie)
  88. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  89. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  90. requestBuilder.WithCustomFeedProxyURL(feed.ProxyURL)
  91. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  92. requestBuilder.UseCustomApplicationProxyURL(feed.FetchViaProxy)
  93. requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
  94. requestBuilder.DisableHTTP2(feed.DisableHTTP2)
  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. }