processor.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. logger.Debug("[Feed #%d] Processing entry %s", feed.ID, entry.URL)
  17. if feed.Crawler {
  18. if !store.EntryURLExists(feed.ID, entry.URL) {
  19. content, err := scraper.Fetch(entry.URL, feed.ScraperRules, feed.UserAgent)
  20. if err != nil {
  21. logger.Error(`[Filter] Unable to crawl this entry: %q => %v`, entry.URL, err)
  22. } else if content != "" {
  23. // We replace the entry content only if the scraper doesn't return any error.
  24. entry.Content = content
  25. }
  26. }
  27. }
  28. entry.Content = rewrite.Rewriter(entry.URL, entry.Content, feed.RewriteRules)
  29. // The sanitizer should always run at the end of the process to make sure unsafe HTML is filtered.
  30. entry.Content = sanitizer.Sanitize(entry.URL, entry.Content)
  31. }
  32. }
  33. // ProcessEntryWebPage downloads the entry web page and apply rewrite rules.
  34. func ProcessEntryWebPage(entry *model.Entry) error {
  35. content, err := scraper.Fetch(entry.URL, entry.Feed.ScraperRules, entry.Feed.UserAgent)
  36. if err != nil {
  37. return err
  38. }
  39. content = rewrite.Rewriter(entry.URL, content, entry.Feed.RewriteRules)
  40. content = sanitizer.Sanitize(entry.URL, content)
  41. if content != "" {
  42. entry.Content = content
  43. }
  44. return nil
  45. }