/
scraper.go
136 lines (115 loc) · 2.61 KB
/
scraper.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package lib
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"golang.org/x/net/html"
)
// Scrape downloads the content of the given URL and returns it as a string
func Scrape(URL string, depth int) (string, []string) {
response, err := http.Get(URL)
allLinks := []string{}
if err != nil {
if depth == 1 {
log.Fatal(err)
}
return "", allLinks
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
if depth == 1 {
log.Fatal(bodyString)
}
return "", allLinks
}
bodyReader := response.Body
var buf bytes.Buffer
tee := io.TeeReader(bodyReader, &buf)
bodyBytes, err := ioutil.ReadAll(tee)
if depth == 1 {
allLinks = findLinkedAsset(&buf)
}
if err != nil {
log.Fatal(err)
}
bodyString := string(bodyBytes)
if len(bodyString) == 0 {
log.Fatal("The received body is empty")
}
return bodyString, allLinks
}
// ReplaceLinks replaces the links by their hash in the content
func ReplaceLinks(content string, links []string, hashes []string) string {
gatewayURL := "https://gateway.ipfs.io/ipfs/"
for key, link := range links {
fmt.Println("Replace ", link)
replacer := strings.NewReplacer(link, gatewayURL+hashes[key])
content = replacer.Replace(content)
}
return content
}
func getTokenLink(token html.Token) (linkValue string, err error) {
if token.Data == "link" {
for _, a := range token.Attr {
if a.Key == "href" {
return a.Val, nil
}
}
}
if token.Data == "img" || token.Data == "script" {
for _, a := range token.Attr {
if a.Key == "src" {
return a.Val, nil
}
}
}
return "", errors.New("No value found in the link")
}
func findLinkedAsset(reader io.Reader) []string {
z := html.NewTokenizer(reader)
linksArray := []string{}
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
// End of doc
return linksArray
case html.StartTagToken:
t := z.Token()
linksArray = appendTokenLink(t, linksArray)
case html.SelfClosingTagToken:
t := z.Token()
linksArray = appendTokenLink(t, linksArray)
}
}
}
func appendTokenLink(token html.Token, linksArray []string) []string {
linkValue, err := getTokenLink(token)
if err == nil {
// Link found
linksArray = append(linksArray, linkValue)
}
return linksArray
}
// PrependBaseURL add the base URL if the scheme is empty
func PrependBaseURL(link string, baseURL string) string {
u, err := url.Parse(link)
if err != nil {
log.Fatal(err)
}
if u.Scheme == "" {
return baseURL + link
}
return link
}