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 (
|
2018-01-02 21:32:01 -05:00
|
|
|
"fmt"
|
2017-12-10 23:51:04 -05:00
|
|
|
"io"
|
2023-09-24 19:32:09 -04:00
|
|
|
"log/slog"
|
2017-12-10 23:51:04 -05:00
|
|
|
"strings"
|
2017-12-10 22:01:38 -05:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
"miniflux.app/v2/internal/config"
|
2023-12-01 19:27:18 -05:00
|
|
|
"miniflux.app/v2/internal/reader/encoding"
|
2023-10-21 22:50:29 -04:00
|
|
|
"miniflux.app/v2/internal/reader/fetcher"
|
2023-08-10 22:46:45 -04:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
func ScrapeWebsite(requestBuilder *fetcher.RequestBuilder, websiteURL, rules string) (string, error) {
|
|
|
|
responseHandler := fetcher.NewResponseHandler(requestBuilder.ExecuteRequest(websiteURL))
|
|
|
|
defer responseHandler.Close()
|
2017-12-10 22:01:38 -05:00
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
if localizedError := responseHandler.LocalizedError(); localizedError != nil {
|
|
|
|
slog.Warn("Unable to scrape website", slog.String("website_url", websiteURL), slog.Any("error", localizedError.Error()))
|
|
|
|
return "", localizedError.Error()
|
2018-01-02 21:32:01 -05:00
|
|
|
}
|
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
if !isAllowedContentType(responseHandler.ContentType()) {
|
|
|
|
return "", fmt.Errorf("scraper: this resource is not a HTML document (%s)", responseHandler.ContentType())
|
2017-12-10 22:01:38 -05:00
|
|
|
}
|
|
|
|
|
2018-01-02 21:32:01 -05:00
|
|
|
// The entry URL could redirect somewhere else.
|
2023-10-21 22:50:29 -04:00
|
|
|
sameSite := urllib.Domain(websiteURL) == urllib.Domain(responseHandler.EffectiveURL())
|
|
|
|
websiteURL = responseHandler.EffectiveURL()
|
2017-12-14 00:30:40 -05:00
|
|
|
|
2017-12-10 23:51:04 -05:00
|
|
|
if rules == "" {
|
|
|
|
rules = getPredefinedScraperRules(websiteURL)
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:19:36 -05:00
|
|
|
var content string
|
2023-10-21 22:50:29 -04:00
|
|
|
var err error
|
|
|
|
|
2023-12-01 19:27:18 -05:00
|
|
|
htmlDocumentReader, err := encoding.CharsetReaderFromContentType(
|
|
|
|
responseHandler.ContentType(),
|
|
|
|
responseHandler.Body(config.Opts.HTTPClientMaxBodySize()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("scraper: unable to read HTML document: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-12-08 03:46:33 -05:00
|
|
|
if sameSite && rules != "" {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Extracting content with custom rules",
|
|
|
|
"url", websiteURL,
|
|
|
|
"rules", rules,
|
|
|
|
)
|
2023-12-01 19:27:18 -05:00
|
|
|
content, err = findContentUsingCustomRules(htmlDocumentReader, rules)
|
2017-12-10 23:51:04 -05:00
|
|
|
} else {
|
2023-09-24 19:32:09 -04:00
|
|
|
slog.Debug("Extracting content with readability",
|
|
|
|
"url", websiteURL,
|
|
|
|
)
|
2023-12-01 19:27:18 -05:00
|
|
|
content, err = readability.ExtractContent(htmlDocumentReader)
|
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
|
|
|
|
2023-10-21 22:50:29 -04:00
|
|
|
func findContentUsingCustomRules(page io.Reader, rules string) (string, error) {
|
2017-12-10 23:51:04 -05:00
|
|
|
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")
|
|
|
|
}
|