aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader/scraper/scraper.go
blob: 6c51862189cb86bd1afeae875634f3d544781858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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"

	"github.com/miniflux/miniflux2/http"
	"github.com/miniflux/miniflux2/reader/readability"
	"github.com/miniflux/miniflux2/reader/sanitizer"
)

// Fetch download a web page a returns relevant contents.
func Fetch(websiteURL 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
	}

	content, err := readability.ExtractContent(page)
	if err != nil {
		return "", err
	}

	return sanitizer.Sanitize(websiteURL, content), nil
}