entry_scraper.go 1.2 KB

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