processor.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Process older entries first
  34. for _, entry := range slices.Backward(feed.Entries) {
  35. slog.Debug("Processing entry",
  36. slog.Int64("user_id", user.ID),
  37. slog.String("entry_url", entry.URL),
  38. slog.String("entry_hash", entry.Hash),
  39. slog.String("entry_title", entry.Title),
  40. slog.Int64("feed_id", feed.ID),
  41. slog.String("feed_url", feed.FeedURL),
  42. )
  43. if filter.IsBlockedEntry(feed, entry, user) || !filter.IsAllowedEntry(feed, entry, user) {
  44. continue
  45. }
  46. parsedInputUrl, _ := url.Parse(entry.URL)
  47. if cleanedURL, err := urlcleaner.RemoveTrackingParameters(parsedFeedURL, parsedSiteURL, parsedInputUrl); err == nil {
  48. entry.URL = cleanedURL
  49. }
  50. webpageBaseURL := ""
  51. entry.URL = rewrite.RewriteEntryURL(feed, entry)
  52. entryIsNew := store.IsNewEntry(feed.ID, entry.Hash)
  53. if feed.Crawler && (entryIsNew || forceRefresh) {
  54. slog.Debug("Scraping entry",
  55. slog.Int64("user_id", user.ID),
  56. slog.String("entry_url", entry.URL),
  57. slog.String("entry_hash", entry.Hash),
  58. slog.String("entry_title", entry.Title),
  59. slog.Int64("feed_id", feed.ID),
  60. slog.String("feed_url", feed.FeedURL),
  61. slog.Bool("entry_is_new", entryIsNew),
  62. slog.Bool("force_refresh", forceRefresh),
  63. )
  64. startTime := time.Now()
  65. requestBuilder := fetcher.NewRequestBuilder()
  66. requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
  67. requestBuilder.WithCookie(feed.Cookie)
  68. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  69. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  70. requestBuilder.WithCustomFeedProxyURL(feed.ProxyURL)
  71. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  72. requestBuilder.UseCustomApplicationProxyURL(feed.FetchViaProxy)
  73. requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
  74. requestBuilder.DisableHTTP2(feed.DisableHTTP2)
  75. scrapedPageBaseURL, extractedContent, scraperErr := scraper.ScrapeWebsite(
  76. requestBuilder,
  77. entry.URL,
  78. feed.ScraperRules,
  79. )
  80. if scrapedPageBaseURL != "" {
  81. webpageBaseURL = scrapedPageBaseURL
  82. }
  83. if config.Opts.HasMetricsCollector() {
  84. status := "success"
  85. if scraperErr != nil {
  86. status = "error"
  87. }
  88. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  89. }
  90. if scraperErr != nil {
  91. slog.Warn("Unable to scrape entry",
  92. slog.Int64("user_id", user.ID),
  93. slog.String("entry_url", entry.URL),
  94. slog.Int64("feed_id", feed.ID),
  95. slog.String("feed_url", feed.FeedURL),
  96. slog.Any("error", scraperErr),
  97. )
  98. } else if extractedContent != "" {
  99. // We replace the entry content only if the scraper doesn't return any error.
  100. entry.Content = minifyContent(extractedContent)
  101. }
  102. }
  103. rewrite.ApplyContentRewriteRules(entry, feed.RewriteRules)
  104. if webpageBaseURL == "" {
  105. webpageBaseURL = entry.URL
  106. }
  107. // The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered out.
  108. entry.Content = sanitizer.SanitizeHTML(webpageBaseURL, entry.Content, &sanitizer.SanitizerOptions{OpenLinksInNewTab: user.OpenExternalLinksInNewTab})
  109. updateEntryReadingTime(store, feed, entry, entryIsNew, user)
  110. filteredEntries = append(filteredEntries, entry)
  111. }
  112. if user.ShowReadingTime && shouldFetchYouTubeWatchTimeInBulk() {
  113. fetchYouTubeWatchTimeInBulk(filteredEntries)
  114. }
  115. feed.Entries = filteredEntries
  116. }
  117. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
  118. func ProcessEntryWebPage(feed *model.Feed, entry *model.Entry, user *model.User) error {
  119. startTime := time.Now()
  120. entry.URL = rewrite.RewriteEntryURL(feed, entry)
  121. requestBuilder := fetcher.NewRequestBuilder()
  122. requestBuilder.WithUserAgent(feed.UserAgent, config.Opts.HTTPClientUserAgent())
  123. requestBuilder.WithCookie(feed.Cookie)
  124. requestBuilder.WithTimeout(config.Opts.HTTPClientTimeout())
  125. requestBuilder.WithProxyRotator(proxyrotator.ProxyRotatorInstance)
  126. requestBuilder.WithCustomFeedProxyURL(feed.ProxyURL)
  127. requestBuilder.WithCustomApplicationProxyURL(config.Opts.HTTPClientProxyURL())
  128. requestBuilder.UseCustomApplicationProxyURL(feed.FetchViaProxy)
  129. requestBuilder.IgnoreTLSErrors(feed.AllowSelfSignedCertificates)
  130. requestBuilder.DisableHTTP2(feed.DisableHTTP2)
  131. webpageBaseURL, extractedContent, scraperErr := scraper.ScrapeWebsite(
  132. requestBuilder,
  133. entry.URL,
  134. feed.ScraperRules,
  135. )
  136. if config.Opts.HasMetricsCollector() {
  137. status := "success"
  138. if scraperErr != nil {
  139. status = "error"
  140. }
  141. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  142. }
  143. if scraperErr != nil {
  144. return scraperErr
  145. }
  146. if extractedContent != "" {
  147. entry.Content = minifyContent(extractedContent)
  148. if user.ShowReadingTime {
  149. entry.ReadingTime = readingtime.EstimateReadingTime(entry.Content, user.DefaultReadingSpeed, user.CJKReadingSpeed)
  150. }
  151. }
  152. rewrite.ApplyContentRewriteRules(entry, entry.Feed.RewriteRules)
  153. entry.Content = sanitizer.SanitizeHTML(webpageBaseURL, entry.Content, &sanitizer.SanitizerOptions{OpenLinksInNewTab: user.OpenExternalLinksInNewTab})
  154. return nil
  155. }