2017-12-11 04:01:38 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package scraper // import "miniflux.app/reader/scraper"
|
2017-12-11 04:01:38 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-01-03 03:32:01 +01:00
|
|
|
"fmt"
|
2017-12-11 05:51:04 +01:00
|
|
|
"io"
|
|
|
|
"strings"
|
2017-12-11 04:01:38 +01:00
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/http/client"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/reader/readability"
|
|
|
|
"miniflux.app/url"
|
|
|
|
|
2017-12-11 05:51:04 +01:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-12-11 04:01:38 +01:00
|
|
|
)
|
|
|
|
|
2018-01-03 03:32:01 +01:00
|
|
|
// Fetch downloads a web page a returns relevant contents.
|
2017-12-11 05:51:04 +01:00
|
|
|
func Fetch(websiteURL, rules string) (string, error) {
|
2018-04-28 19:51:07 +02:00
|
|
|
clt := client.New(websiteURL)
|
|
|
|
response, err := clt.Get()
|
2017-12-11 04:01:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
2018-01-03 03:32:01 +01:00
|
|
|
return "", errors.New("scraper: unable to download web page")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(response.ContentType, "text/html") {
|
|
|
|
return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", response.ContentType)
|
2017-12-11 04:01:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
page, err := response.NormalizeBodyEncoding()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-01-03 03:32:01 +01:00
|
|
|
// The entry URL could redirect somewhere else.
|
2017-12-14 06:30:40 +01:00
|
|
|
websiteURL = response.EffectiveURL
|
|
|
|
|
2017-12-11 05:51:04 +01:00
|
|
|
if rules == "" {
|
|
|
|
rules = getPredefinedScraperRules(websiteURL)
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:19:36 +01:00
|
|
|
var content string
|
2017-12-11 05:51:04 +01:00
|
|
|
if rules != "" {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Debug(`[Scraper] Using rules "%s" for "%s"`, rules, websiteURL)
|
2017-12-11 05:51:04 +01:00
|
|
|
content, err = scrapContent(page, rules)
|
|
|
|
} else {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Debug(`[Scraper] Using readability for "%s"`, websiteURL)
|
2017-12-11 05:51:04 +01:00
|
|
|
content, err = readability.ExtractContent(page)
|
|
|
|
}
|
|
|
|
|
2017-12-11 04:01:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-12-13 04:19:36 +01:00
|
|
|
return content, nil
|
2017-12-11 04:01:38 +01:00
|
|
|
}
|
2017-12-11 05:51:04 +01:00
|
|
|
|
|
|
|
func scrapContent(page io.Reader, rules string) (string, error) {
|
|
|
|
document, err := goquery.NewDocumentFromReader(page)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
contents := ""
|
|
|
|
document.Find(rules).Each(func(i int, s *goquery.Selection) {
|
|
|
|
var content string
|
|
|
|
|
|
|
|
// For some inline elements, we get the parent.
|
2018-04-26 22:51:07 +02:00
|
|
|
if s.Is("img") || s.Is("iframe") {
|
2017-12-11 05:51:04 +01:00
|
|
|
content, _ = s.Parent().Html()
|
|
|
|
} else {
|
|
|
|
content, _ = s.Html()
|
|
|
|
}
|
|
|
|
|
|
|
|
contents += content
|
|
|
|
})
|
|
|
|
|
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPredefinedScraperRules(websiteURL string) string {
|
|
|
|
urlDomain := url.Domain(websiteURL)
|
|
|
|
|
|
|
|
for domain, rules := range predefinedRules {
|
|
|
|
if strings.Contains(urlDomain, domain) {
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|