processor.go 4.0 KB

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