scraper.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2017 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 scraper // import "miniflux.app/reader/scraper"
  5. import (
  6. "errors"
  7. "fmt"
  8. "io"
  9. "strings"
  10. "miniflux.app/http/client"
  11. "miniflux.app/logger"
  12. "miniflux.app/reader/readability"
  13. "miniflux.app/url"
  14. "github.com/PuerkitoBio/goquery"
  15. )
  16. // Fetch downloads a web page and returns relevant contents.
  17. func Fetch(websiteURL, rules, userAgent string) (string, error) {
  18. clt := client.New(websiteURL)
  19. if userAgent != "" {
  20. clt.WithUserAgent(userAgent)
  21. }
  22. response, err := clt.Get()
  23. if err != nil {
  24. return "", err
  25. }
  26. if response.HasServerFailure() {
  27. return "", errors.New("scraper: unable to download web page")
  28. }
  29. if !isWhitelistedContentType(response.ContentType) {
  30. return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", response.ContentType)
  31. }
  32. if err = response.EnsureUnicodeBody(); err != nil {
  33. return "", err
  34. }
  35. // The entry URL could redirect somewhere else.
  36. websiteURL = response.EffectiveURL
  37. if rules == "" {
  38. rules = getPredefinedScraperRules(websiteURL)
  39. }
  40. var content string
  41. if rules != "" {
  42. logger.Debug(`[Scraper] Using rules %q for %q`, rules, websiteURL)
  43. content, err = scrapContent(response.Body, rules)
  44. } else {
  45. logger.Debug(`[Scraper] Using readability for %q`, websiteURL)
  46. content, err = readability.ExtractContent(response.Body)
  47. }
  48. if err != nil {
  49. return "", err
  50. }
  51. return content, nil
  52. }
  53. func scrapContent(page io.Reader, rules string) (string, error) {
  54. document, err := goquery.NewDocumentFromReader(page)
  55. if err != nil {
  56. return "", err
  57. }
  58. contents := ""
  59. document.Find(rules).Each(func(i int, s *goquery.Selection) {
  60. var content string
  61. content, _ = goquery.OuterHtml(s)
  62. contents += content
  63. })
  64. return contents, nil
  65. }
  66. func getPredefinedScraperRules(websiteURL string) string {
  67. urlDomain := url.Domain(websiteURL)
  68. for domain, rules := range predefinedRules {
  69. if strings.Contains(urlDomain, domain) {
  70. return rules
  71. }
  72. }
  73. return ""
  74. }
  75. func isWhitelistedContentType(contentType string) bool {
  76. contentType = strings.ToLower(contentType)
  77. return strings.HasPrefix(contentType, "text/html") ||
  78. strings.HasPrefix(contentType, "application/xhtml+xml")
  79. }