processor.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. "miniflux.app/logger"
  7. "miniflux.app/model"
  8. "miniflux.app/reader/rewrite"
  9. "miniflux.app/reader/sanitizer"
  10. "miniflux.app/reader/scraper"
  11. "miniflux.app/storage"
  12. )
  13. // ProcessFeedEntries downloads original web page for entries and apply filters.
  14. func ProcessFeedEntries(store *storage.Storage, feed *model.Feed) {
  15. for _, entry := range feed.Entries {
  16. if feed.Crawler {
  17. if !store.EntryURLExists(feed.UserID, entry.URL) {
  18. content, err := scraper.Fetch(entry.URL, feed.ScraperRules, feed.UserAgent)
  19. if err != nil {
  20. logger.Error(`[Filter] Unable to crawl this entry: %q => %v`, entry.URL, err)
  21. } else if content != "" {
  22. // We replace the entry content only if the scraper doesn't return any error.
  23. entry.Content = content
  24. }
  25. }
  26. }
  27. entry.Content = rewrite.Rewriter(entry.URL, entry.Content, feed.RewriteRules)
  28. // The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered.
  29. entry.Content = sanitizer.Sanitize(entry.URL, entry.Content)
  30. }
  31. }
  32. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
  33. func ProcessEntryWebPage(entry *model.Entry) error {
  34. content, err := scraper.Fetch(entry.URL, entry.Feed.ScraperRules, entry.Feed.UserAgent)
  35. if err != nil {
  36. return err
  37. }
  38. content = rewrite.Rewriter(entry.URL, content, entry.Feed.RewriteRules)
  39. content = sanitizer.Sanitize(entry.URL, content)
  40. if content != "" {
  41. entry.Content = content
  42. }
  43. return nil
  44. }