scraper.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package scraper // import "miniflux.app/v2/internal/reader/scraper"
  4. import (
  5. "fmt"
  6. "io"
  7. "log/slog"
  8. "strings"
  9. "miniflux.app/v2/internal/config"
  10. "miniflux.app/v2/internal/reader/encoding"
  11. "miniflux.app/v2/internal/reader/fetcher"
  12. "miniflux.app/v2/internal/reader/readability"
  13. "miniflux.app/v2/internal/urllib"
  14. "github.com/PuerkitoBio/goquery"
  15. )
  16. func ScrapeWebsite(requestBuilder *fetcher.RequestBuilder, websiteURL, rules string) (string, error) {
  17. responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
  18. defer responseHandler.Close()
  19. if localizedError := responseHandler.LocalizedError(); localizedError != nil {
  20. slog.Warn("Unable to scrape website", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
  21. return "", localizedError.Error()
  22. }
  23. if !isAllowedContentType(responseHandler.ContentType()) {
  24. return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", responseHandler.ContentType())
  25. }
  26. // The entry URL could redirect somewhere else.
  27. sameSite := urllib.Domain(websiteURL) == urllib.Domain(responseHandler.EffectiveURL())
  28. websiteURL = responseHandler.EffectiveURL()
  29. if rules == "" {
  30. rules = getPredefinedScraperRules(websiteURL)
  31. }
  32. var content string
  33. var err error
  34. htmlDocumentReader, err := encoding.CharsetReaderFromContentType(
  35. responseHandler.ContentType(),
  36. responseHandler.Body(config.Opts.HTTPClientMaxBodySize()),
  37. )
  38. if err != nil {
  39. return "", fmt.Errorf("scraper: unable to read HTML document: %v", err)
  40. }
  41. if sameSite && rules != "" {
  42. slog.Debug("Extracting content with custom rules",
  43. "url", websiteURL,
  44. "rules", rules,
  45. )
  46. content, err = findContentUsingCustomRules(htmlDocumentReader, rules)
  47. } else {
  48. slog.Debug("Extracting content with readability",
  49. "url", websiteURL,
  50. )
  51. content, err = readability.ExtractContent(htmlDocumentReader)
  52. }
  53. if err != nil {
  54. return "", err
  55. }
  56. return content, nil
  57. }
  58. func findContentUsingCustomRules(page io.Reader, rules string) (string, error) {
  59. document, err := goquery.NewDocumentFromReader(page)
  60. if err != nil {
  61. return "", err
  62. }
  63. contents := ""
  64. document.Find(rules).Each(func(i int, s *goquery.Selection) {
  65. var content string
  66. content, _ = goquery.OuterHtml(s)
  67. contents += content
  68. })
  69. return contents, nil
  70. }
  71. func getPredefinedScraperRules(websiteURL string) string {
  72. urlDomain := urllib.Domain(websiteURL)
  73. for domain, rules := range predefinedRules {
  74. if strings.Contains(urlDomain, domain) {
  75. return rules
  76. }
  77. }
  78. return ""
  79. }
  80. func isAllowedContentType(contentType string) bool {
  81. contentType = strings.ToLower(contentType)
  82. return strings.HasPrefix(contentType, "text/html") ||
  83. strings.HasPrefix(contentType, "application/xhtml+xml")
  84. }