1
0
Fork 0
miniflux/internal/reader/scraper/scraper.go

106 lines
2.6 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package scraper // import "miniflux.app/v2/internal/reader/scraper"
import (
"errors"
"fmt"
2017-12-10 23:51:04 -05:00
"io"
"strings"
"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/http/client"
"miniflux.app/v2/internal/logger"
"miniflux.app/v2/internal/reader/readability"
"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"
)
// 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) {
clt := client.NewClientWithConfig(websiteURL, config.Opts)
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()
}
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
2018-04-28 13:51:07 -04:00
response, err := clt.Get()
if err != nil {
return "", err
}
if response.HasServerFailure() {
return "", errors.New("scraper: unable to download web page")
}
2020-09-27 19:01:06 -04:00
if !isAllowedContentType(response.ContentType) {
return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", response.ContentType)
}
if err = response.EnsureUnicodeBody(); err != nil {
return "", err
}
// The entry URL could redirect somewhere else.
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)
}
var content string
if sameSite && rules != "" {
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)
content, err = readability.ExtractContent(response.Body)
2017-12-10 23:51:04 -05:00
}
if err != nil {
return "", err
}
return content, nil
}
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
content, _ = goquery.OuterHtml(s)
2017-12-10 23:51:04 -05:00
contents += content
})
return contents, nil
}
func getPredefinedScraperRules(websiteURL string) string {
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 ""
}
2020-09-27 19:01:06 -04:00
func isAllowedContentType(contentType string) bool {
contentType = strings.ToLower(contentType)
return strings.HasPrefix(contentType, "text/html") ||
strings.HasPrefix(contentType, "application/xhtml+xml")
}