miniflux/reader/scraper/scraper.go

93 lines
2 KiB
Go
Raw Normal View History

// 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.
package scraper
import (
"errors"
2017-12-11 05:51:04 +01:00
"io"
"strings"
2017-12-11 05:51:04 +01:00
"github.com/PuerkitoBio/goquery"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/http"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-12-13 06:48:13 +01:00
"github.com/miniflux/miniflux/reader/readability"
"github.com/miniflux/miniflux/url"
)
// Fetch download a web page a returns relevant contents.
2017-12-11 05:51:04 +01:00
func Fetch(websiteURL, rules string) (string, error) {
client := http.NewClient(websiteURL)
response, err := client.Get()
if err != nil {
return "", err
}
if response.HasServerFailure() {
return "", errors.New("unable to download web page")
}
page, err := response.NormalizeBodyEncoding()
if err != nil {
return "", err
}
2017-12-14 06:30:40 +01:00
// The entry URL could be a redirect somewhere else.
websiteURL = response.EffectiveURL
2017-12-11 05:51:04 +01:00
if rules == "" {
rules = getPredefinedScraperRules(websiteURL)
}
var content string
2017-12-11 05:51:04 +01:00
if rules != "" {
2017-12-16 03:55:57 +01:00
logger.Debug(`[Scraper] Using rules "%s" for "%s"`, rules, websiteURL)
2017-12-11 05:51:04 +01:00
content, err = scrapContent(page, rules)
} else {
2017-12-16 03:55:57 +01:00
logger.Debug(`[Scraper] Using readability for "%s"`, websiteURL)
2017-12-11 05:51:04 +01:00
content, err = readability.ExtractContent(page)
}
if err != nil {
return "", err
}
return content, nil
}
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
// For some inline elements, we get the parent.
if s.Is("img") {
content, _ = s.Parent().Html()
} else {
content, _ = s.Html()
}
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 ""
}