scraper.go 2.1 KB

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