2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-10 22:01:38 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package scraper // import "miniflux.app/v2/internal/reader/scraper"
|
2017-12-10 22:01:38 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-01-02 21:32:01 -05:00
|
|
|
"fmt"
|
2017-12-10 23:51:04 -05:00
|
|
|
"io"
|
|
|
|
"strings"
|
2017-12-10 22:01:38 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
|
|
|
"miniflux.app/v2/internal/http/client"
|
|
|
|
"miniflux.app/v2/internal/logger"
|
|
|
|
"miniflux.app/v2/internal/reader/readability"
|
2023-08-13 22:09:01 -04:00
|
|
|
"miniflux.app/v2/internal/urllib"
|
2018-08-25 00:51:50 -04:00
|
|
|
|
2017-12-10 23:51:04 -05:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2017-12-10 22:01:38 -05:00
|
|
|
)
|
|
|
|
|
2018-10-14 14:46:41 -04:00
|
|
|
// Fetch downloads a web page and returns relevant contents.
|
2021-08-28 05:30:04 -04:00
|
|
|
func Fetch(websiteURL, rules, userAgent string, cookie string, allowSelfSignedCertificates, useProxy bool) (string, error) {
|
2020-09-27 17:29:48 -04:00
|
|
|
clt := client.NewClientWithConfig(websiteURL, config.Opts)
|
2021-02-21 16:42:49 -05:00
|
|
|
clt.WithUserAgent(userAgent)
|
2021-03-22 23:27:58 -04:00
|
|
|
clt.WithCookie(cookie)
|
2021-08-28 05:30:04 -04:00
|
|
|
if useProxy {
|
|
|
|
clt.WithProxy()
|
|
|
|
}
|
2021-02-21 16:42:49 -05:00
|
|
|
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
|
2018-09-19 21:19:24 -04:00
|
|
|
|
2018-04-28 13:51:07 -04:00
|
|
|
response, err := clt.Get()
|
2017-12-10 22:01:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
2018-01-02 21:32:01 -05:00
|
|
|
return "", errors.New("scraper: unable to download web page")
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:01:06 -04:00
|
|
|
if !isAllowedContentType(response.ContentType) {
|
2018-01-02 21:32:01 -05:00
|
|
|
return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", response.ContentType)
|
2017-12-10 22:01:38 -05:00
|
|
|
}
|
|
|
|
|
2018-10-14 14:46:41 -04:00
|
|
|
if err = response.EnsureUnicodeBody(); err != nil {
|
2017-12-10 22:01:38 -05:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:32:01 -05:00
|
|
|
// The entry URL could redirect somewhere else.
|
2023-08-13 22:09:01 -04:00
|
|
|
sameSite := urllib.Domain(websiteURL) == urllib.Domain(response.EffectiveURL)
|
2017-12-14 00:30:40 -05:00
|
|
|
websiteURL = response.EffectiveURL
|
|
|
|
|
2017-12-10 23:51:04 -05:00
|
|
|
if rules == "" {
|
|
|
|
rules = getPredefinedScraperRules(websiteURL)
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:19:36 -05:00
|
|
|
var content string
|
2021-12-08 03:46:33 -05:00
|
|
|
if sameSite && rules != "" {
|
2018-10-14 14:46:41 -04:00
|
|
|
logger.Debug(`[Scraper] Using rules %q for %q`, rules, websiteURL)
|
|
|
|
content, err = scrapContent(response.Body, rules)
|
2017-12-10 23:51:04 -05:00
|
|
|
} else {
|
2018-12-02 23:51:06 -05:00
|
|
|
logger.Debug(`[Scraper] Using readability for %q`, websiteURL)
|
2018-10-14 14:46:41 -04:00
|
|
|
content, err = readability.ExtractContent(response.Body)
|
2017-12-10 23:51:04 -05:00
|
|
|
}
|
|
|
|
|
2017-12-10 22:01:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:19:36 -05:00
|
|
|
return content, nil
|
2017-12-10 22:01:38 -05:00
|
|
|
}
|
2017-12-10 23:51:04 -05: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 00:18:31 -05:00
|
|
|
content, _ = goquery.OuterHtml(s)
|
2017-12-10 23:51:04 -05:00
|
|
|
contents += content
|
|
|
|
})
|
|
|
|
|
|
|
|
return contents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPredefinedScraperRules(websiteURL string) string {
|
2023-08-13 22:09:01 -04:00
|
|
|
urlDomain := urllib.Domain(websiteURL)
|
2017-12-10 23:51:04 -05:00
|
|
|
|
|
|
|
for domain, rules := range predefinedRules {
|
|
|
|
if strings.Contains(urlDomain, domain) {
|
|
|
|
return rules
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
2018-11-03 16:44:13 -04:00
|
|
|
|
2020-09-27 19:01:06 -04:00
|
|
|
func isAllowedContentType(contentType string) bool {
|
2018-11-03 16:44:13 -04:00
|
|
|
contentType = strings.ToLower(contentType)
|
|
|
|
return strings.HasPrefix(contentType, "text/html") ||
|
|
|
|
strings.HasPrefix(contentType, "application/xhtml+xml")
|
|
|
|
}
|