entry_scraper.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 ui // import "miniflux.app/ui"
  5. import (
  6. "net/http"
  7. "miniflux.app/http/request"
  8. "miniflux.app/http/response/json"
  9. "miniflux.app/model"
  10. "miniflux.app/reader/sanitizer"
  11. "miniflux.app/reader/scraper"
  12. )
  13. // FetchContent downloads the original HTML page and returns relevant contents.
  14. func (c *Controller) FetchContent(w http.ResponseWriter, r *http.Request) {
  15. entryID := request.RouteInt64Param(r, "entryID")
  16. builder := c.store.NewEntryQueryBuilder(request.UserID(r))
  17. builder.WithEntryID(entryID)
  18. builder.WithoutStatus(model.EntryStatusRemoved)
  19. entry, err := builder.GetEntry()
  20. if err != nil {
  21. json.ServerError(w, r, err)
  22. return
  23. }
  24. if entry == nil {
  25. json.NotFound(w, r)
  26. return
  27. }
  28. content, err := scraper.Fetch(entry.URL, entry.Feed.ScraperRules, entry.Feed.UserAgent)
  29. if err != nil {
  30. json.ServerError(w, r, err)
  31. return
  32. }
  33. entry.Content = sanitizer.Sanitize(entry.URL, content)
  34. c.store.UpdateEntryContent(entry)
  35. json.OK(w, r, map[string]string{"content": entry.Content})
  36. }