processor.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 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 processor
  5. import (
  6. "regexp"
  7. "time"
  8. "miniflux.app/config"
  9. "miniflux.app/logger"
  10. "miniflux.app/metric"
  11. "miniflux.app/model"
  12. "miniflux.app/reader/rewrite"
  13. "miniflux.app/reader/sanitizer"
  14. "miniflux.app/reader/scraper"
  15. "miniflux.app/storage"
  16. )
  17. // ProcessFeedEntries downloads original web page for entries and apply filters.
  18. func ProcessFeedEntries(store *storage.Storage, feed *model.Feed) {
  19. var filteredEntries model.Entries
  20. for _, entry := range feed.Entries {
  21. logger.Debug("[Processor] Processing entry %q from feed %q", entry.URL, feed.FeedURL)
  22. if isBlockedEntry(feed, entry) || !isAllowedEntry(feed, entry) {
  23. continue
  24. }
  25. if feed.Crawler {
  26. if !store.EntryURLExists(feed.ID, entry.URL) {
  27. logger.Debug("[Processor] Crawling entry %q from feed %q", entry.URL, feed.FeedURL)
  28. startTime := time.Now()
  29. content, scraperErr := scraper.Fetch(entry.URL, feed.ScraperRules, feed.UserAgent)
  30. if config.Opts.HasMetricsCollector() {
  31. status := "success"
  32. if scraperErr != nil {
  33. status = "error"
  34. }
  35. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  36. }
  37. if scraperErr != nil {
  38. logger.Error(`[Processor] Unable to crawl this entry: %q => %v`, entry.URL, scraperErr)
  39. } else if content != "" {
  40. // We replace the entry content only if the scraper doesn't return any error.
  41. entry.Content = content
  42. }
  43. }
  44. }
  45. entry.Content = rewrite.Rewriter(entry.URL, entry.Content, feed.RewriteRules)
  46. // The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered.
  47. entry.Content = sanitizer.Sanitize(entry.URL, entry.Content)
  48. filteredEntries = append(filteredEntries, entry)
  49. }
  50. feed.Entries = filteredEntries
  51. }
  52. func isBlockedEntry(feed *model.Feed, entry *model.Entry) bool {
  53. if feed.BlocklistRules != "" {
  54. match, _ := regexp.MatchString(feed.BlocklistRules, entry.Title)
  55. if match {
  56. logger.Debug("[Processor] Blocking entry %q from feed %q based on rule %q", entry.Title, feed.FeedURL, feed.BlocklistRules)
  57. return true
  58. }
  59. }
  60. return false
  61. }
  62. func isAllowedEntry(feed *model.Feed, entry *model.Entry) bool {
  63. if feed.KeeplistRules != "" {
  64. match, _ := regexp.MatchString(feed.KeeplistRules, entry.Title)
  65. if match {
  66. logger.Debug("[Processor] Allow entry %q from feed %q based on rule %q", entry.Title, feed.FeedURL, feed.KeeplistRules)
  67. return true
  68. }
  69. return false
  70. }
  71. return true
  72. }
  73. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
  74. func ProcessEntryWebPage(entry *model.Entry) error {
  75. startTime := time.Now()
  76. content, scraperErr := scraper.Fetch(entry.URL, entry.Feed.ScraperRules, entry.Feed.UserAgent)
  77. if config.Opts.HasMetricsCollector() {
  78. status := "success"
  79. if scraperErr != nil {
  80. status = "error"
  81. }
  82. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  83. }
  84. if scraperErr != nil {
  85. return scraperErr
  86. }
  87. content = rewrite.Rewriter(entry.URL, content, entry.Feed.RewriteRules)
  88. content = sanitizer.Sanitize(entry.URL, content)
  89. if content != "" {
  90. entry.Content = content
  91. }
  92. return nil
  93. }