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
|
|
|
|
2020-09-27 23:29:48 +02:00
|
|
|
"miniflux.app/config"
|
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-10-14 20:46:41 +02:00
|
|
|
// Fetch downloads a web page and returns relevant contents.
|
2018-09-20 03:19:24 +02:00
|
|
|
func Fetch(websiteURL, rules, userAgent string) (string, error) {
|
2020-09-27 23:29:48 +02:00
|
|
|
clt := client.NewClientWithConfig(websiteURL, config.Opts)
|
2018-09-20 03:19:24 +02:00
|
|
|
if userAgent != "" {
|
|
|
|
clt.WithUserAgent(userAgent)
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:51:07 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-09-28 01:01:06 +02:00
|
|
|
if !isAllowedContentType(response.ContentType) {
|
2018-01-03 03:32:01 +01:00
|
|
|
return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", response.ContentType)
|
2017-12-11 04:01:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-14 20:46:41 +02:00
|
|
|
if err = response.EnsureUnicodeBody(); err != nil {
|
2017-12-11 04:01:38 +01:00
|
|
|
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 != "" {
|
2018-10-14 20:46:41 +02:00
|
|
|
logger.Debug(`[Scraper] Using rules %q for %q`, rules, websiteURL)
|
|
|
|
content, err = scrapContent(response.Body, rules)
|
2017-12-11 05:51:04 +01:00
|
|
|
} else {
|
2018-12-03 05:51:06 +01:00
|
|
|
logger.Debug(`[Scraper] Using readability for %q`, websiteURL)
|
2018-10-14 20:46:41 +02:00
|
|
|
content, err = readability.ExtractContent(response.Body)
|
2017-12-11 05:51:04 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2019-12-22 06:18:31 +01:00
|
|
|
content, _ = goquery.OuterHtml(s)
|
2017-12-11 05:51:04 +01:00
|
|
|
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 ""
|
|
|
|
}
|
2018-11-03 21:44:13 +01:00
|
|
|
|
2020-09-28 01:01:06 +02:00
|
|
|
func isAllowedContentType(contentType string) bool {
|
2018-11-03 21:44:13 +01:00
|
|
|
contentType = strings.ToLower(contentType)
|
|
|
|
return strings.HasPrefix(contentType, "text/html") ||
|
|
|
|
strings.HasPrefix(contentType, "application/xhtml+xml")
|
|
|
|
}
|