scraper.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/config"
  11. "miniflux.app/http/client"
  12. "miniflux.app/logger"
  13. "miniflux.app/reader/readability"
  14. "miniflux.app/url"
  15. "github.com/PuerkitoBio/goquery"
  16. )
  17. // Fetch downloads a web page and returns relevant contents.
  18. func Fetch(websiteURL, rules, userAgent string, cookie string, allowSelfSignedCertificates, useProxy bool) (string, error) {
  19. content, err := fetchURL(websiteURL, rules, userAgent, cookie, allowSelfSignedCertificates, useProxy)
  20. if err != nil {
  21. return "", err
  22. }
  23. return followTheOnlyLink(websiteURL, content, rules, userAgent, cookie, allowSelfSignedCertificates, useProxy)
  24. }
  25. func fetchURL(websiteURL, rules, userAgent string, cookie string, allowSelfSignedCertificates, useProxy bool) (string, error) {
  26. clt := client.NewClientWithConfig(websiteURL, config.Opts)
  27. clt.WithUserAgent(userAgent)
  28. clt.WithCookie(cookie)
  29. if useProxy {
  30. clt.WithProxy()
  31. }
  32. clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
  33. response, err := clt.Get()
  34. if err != nil {
  35. return "", err
  36. }
  37. if response.HasServerFailure() {
  38. return "", errors.New("scraper: unable to download web page")
  39. }
  40. if !isAllowedContentType(response.ContentType) {
  41. return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", response.ContentType)
  42. }
  43. if err = response.EnsureUnicodeBody(); err != nil {
  44. return "", err
  45. }
  46. sameSite := url.Domain(websiteURL) == url.Domain(response.EffectiveURL)
  47. // The entry URL could redirect somewhere else.
  48. websiteURL = response.EffectiveURL
  49. if rules == "" {
  50. rules = getPredefinedScraperRules(websiteURL)
  51. }
  52. var content string
  53. if sameSite && rules != "" {
  54. logger.Debug(`[Scraper] Using rules %q for %q`, rules, websiteURL)
  55. content, err = scrapContent(response.Body, rules)
  56. } else {
  57. logger.Debug(`[Scraper] Using readability for %q`, websiteURL)
  58. content, err = readability.ExtractContent(response.Body)
  59. }
  60. if err != nil {
  61. return "", err
  62. }
  63. return content, nil
  64. }
  65. func scrapContent(page io.Reader, rules string) (string, error) {
  66. document, err := goquery.NewDocumentFromReader(page)
  67. if err != nil {
  68. return "", err
  69. }
  70. contents := ""
  71. document.Find(rules).Each(func(i int, s *goquery.Selection) {
  72. var content string
  73. content, _ = goquery.OuterHtml(s)
  74. contents += content
  75. })
  76. return contents, nil
  77. }
  78. func getPredefinedScraperRules(websiteURL string) string {
  79. urlDomain := url.Domain(websiteURL)
  80. for domain, rules := range predefinedRules {
  81. if strings.Contains(urlDomain, domain) {
  82. return rules
  83. }
  84. }
  85. return ""
  86. }
  87. func isAllowedContentType(contentType string) bool {
  88. contentType = strings.ToLower(contentType)
  89. return strings.HasPrefix(contentType, "text/html") ||
  90. strings.HasPrefix(contentType, "application/xhtml+xml")
  91. }
  92. func followTheOnlyLink(websiteURL, content string, rules, userAgent string, cookie string, allowSelfSignedCertificates, useProxy bool) (string, error) {
  93. document, err := goquery.NewDocumentFromReader(strings.NewReader(content))
  94. if err != nil {
  95. return "", err
  96. }
  97. body := document.Find("body").Nodes[0]
  98. if body.FirstChild.NextSibling != nil ||
  99. body.FirstChild.Data != "a" {
  100. return content, nil
  101. }
  102. // the body has only one child of <a>
  103. var href string
  104. for _, attr := range body.FirstChild.Attr {
  105. if attr.Key == "href" {
  106. href = attr.Val
  107. break
  108. }
  109. }
  110. if href == "" {
  111. return content, nil
  112. }
  113. href, err = url.AbsoluteURL(websiteURL, href)
  114. if err != nil {
  115. return "", err
  116. }
  117. sameSite := url.Domain(websiteURL) == url.Domain(href)
  118. if sameSite {
  119. return fetchURL(href, rules, userAgent, cookie, allowSelfSignedCertificates, useProxy)
  120. }
  121. return fetchURL(href, rules, userAgent, "", false, false)
  122. }