2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-11 04:01:38 +01:00
|
|
|
|
2023-08-10 06:15:55 +02:00
|
|
|
package scraper // import "miniflux.app/v2/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
|
|
|
|
2023-08-10 06:15:55 +02:00
|
|
|
"miniflux.app/v2/config"
|
|
|
|
"miniflux.app/v2/http/client"
|
|
|
|
"miniflux.app/v2/logger"
|
|
|
|
"miniflux.app/v2/reader/readability"
|
|
|
|
"miniflux.app/v2/url"
|
2018-08-25 06:51:50 +02:00
|
|
|
|
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.
|
2021-08-28 11:30:04 +02:00
|
|
|
func Fetch(websiteURL, rules, userAgent string, cookie string, allowSelfSignedCertificates, useProxy bool) (string, error) {
|
2020-09-27 23:29:48 +02:00
|
|
|
clt := client.NewClientWithConfig(websiteURL, config.Opts)
|
2021-02-21 22:42:49 +01:00
|
|
|
clt.WithUserAgent(userAgent)
|
2021-03-23 04:27:58 +01:00
|
|
|
clt.WithCookie(cookie)
|
2021-08-28 11:30:04 +02:00
|
|
|
if useProxy {
|
|
|
|
clt.WithProxy()
|
|
|
|
}
|
2021-02-21 22:42:49 +01:00
|
|
|
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
|
2018-09-20 03:19:24 +02:00
|
|
|
|
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.
|
2022-11-15 02:02:45 +01:00
|
|
|
sameSite := url.Domain(websiteURL) == url.Domain(response.EffectiveURL)
|
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
|
2021-12-08 09:46:33 +01:00
|
|
|
if sameSite && 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")
|
|
|
|
}
|