processor.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "time"
  7. "miniflux.app/config"
  8. "miniflux.app/logger"
  9. "miniflux.app/metric"
  10. "miniflux.app/model"
  11. "miniflux.app/reader/rewrite"
  12. "miniflux.app/reader/sanitizer"
  13. "miniflux.app/reader/scraper"
  14. "miniflux.app/storage"
  15. )
  16. // ProcessFeedEntries downloads original web page for entries and apply filters.
  17. func ProcessFeedEntries(store *storage.Storage, feed *model.Feed) {
  18. for _, entry := range feed.Entries {
  19. logger.Debug("[Feed #%d] Processing entry %s", feed.ID, entry.URL)
  20. if feed.Crawler {
  21. if !store.EntryURLExists(feed.ID, entry.URL) {
  22. startTime := time.Now()
  23. content, scraperErr := scraper.Fetch(entry.URL, feed.ScraperRules, feed.UserAgent)
  24. if config.Opts.HasMetricsCollector() {
  25. status := "success"
  26. if scraperErr != nil {
  27. status = "error"
  28. }
  29. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  30. }
  31. if scraperErr != nil {
  32. logger.Error(`[Filter] Unable to crawl this entry: %q => %v`, entry.URL, scraperErr)
  33. } else if content != "" {
  34. // We replace the entry content only if the scraper doesn't return any error.
  35. entry.Content = content
  36. }
  37. }
  38. }
  39. entry.Content = rewrite.Rewriter(entry.URL, entry.Content, feed.RewriteRules)
  40. // The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered.
  41. entry.Content = sanitizer.Sanitize(entry.URL, entry.Content)
  42. }
  43. }
  44. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
  45. func ProcessEntryWebPage(entry *model.Entry) error {
  46. startTime := time.Now()
  47. content, scraperErr := scraper.Fetch(entry.URL, entry.Feed.ScraperRules, entry.Feed.UserAgent)
  48. if config.Opts.HasMetricsCollector() {
  49. status := "success"
  50. if scraperErr != nil {
  51. status = "error"
  52. }
  53. metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
  54. }
  55. if scraperErr != nil {
  56. return scraperErr
  57. }
  58. content = rewrite.Rewriter(entry.URL, content, entry.Feed.RewriteRules)
  59. content = sanitizer.Sanitize(entry.URL, content)
  60. if content != "" {
  61. entry.Content = content
  62. }
  63. return nil
  64. }