-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (203 loc) · 4.68 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"strings"
"unicode"
"unicode/utf8"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"golang.org/x/net/html"
)
var config = struct {
region string
bucket string
}{}
func main() {
var ih *imageHelper
run("http://shop.nordstrom.com/c/women", ih)
run("http://shop.nordstrom.com/c/men", ih)
}
func run(url string, ih *imageHelper) {
resp, err := http.Get(url)
if err != nil {
fmt.Fprintf(os.Stderr, "noder: %v\n", err)
}
doc, err := html.Parse(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "outline: %v\n", err)
os.Exit(1)
}
if ih == nil {
ih = &imageHelper{
urlToHash: make(map[string]string, 20),
imageDownloaded: make(map[string]struct{}, 20),
}
}
node := getNodeByID(doc, "main-content")
ih.baseURL = resp.Request.URL
forEachNode(node, ih.downloadImages)
forEachNode(node, stripCommentAndSpace)
js := &jsHelper{}
forEachNode(node, js.nodeToJs)
}
func forEachNode(n *html.Node, f func(*html.Node) bool) {
var done bool
if f != nil {
done = f(n)
}
if done {
return
}
c := n.FirstChild
for c != nil {
s := c.NextSibling
forEachNode(c, f)
c = s
}
}
func getNodeByID(n *html.Node, id string) *html.Node {
var res *html.Node
filter := func(n *html.Node) bool {
for _, a := range n.Attr {
if a.Key == "id" && a.Val == id {
res = n
return true
}
}
return false
}
forEachNode(n, filter)
return res
}
func stripCommentAndSpace(n *html.Node) bool {
switch n.Type {
case html.CommentNode:
par := n.Parent
par.RemoveChild(n)
case html.TextNode:
var leading, trailing bool
r, _ := utf8.DecodeRuneInString(n.Data)
if unicode.IsSpace(r) {
leading = true
}
r, _ = utf8.DecodeLastRuneInString(n.Data)
if unicode.IsSpace(r) {
trailing = true
}
n.Data = strings.TrimSpace(n.Data)
switch {
case len(n.Data) == 0:
n.Data = " "
case leading && trailing:
n.Data = " " + n.Data + " "
case leading:
n.Data = " " + n.Data
case trailing:
n.Data += " "
}
}
return false
}
type jsHelper struct {
tileNum int
}
func (js *jsHelper) nodeToJs(n *html.Node) bool {
for _, v := range n.Attr {
switch {
case v.Key == "class" && strings.Contains(v.Val, "story-tile"):
js.tileNum++
var buf bytes.Buffer
html.Render(&buf, n)
fmt.Fprintf(os.Stdout, "var t%[1]v = document.querySelector('#main-content .story-tile:nth-of-type(%[1]v)');\nt%[1]v.innerHTML = %q;\n\n", js.tileNum, buf.String())
return true
}
}
return false
}
type imageHelper struct {
baseURL *url.URL
urlToHash map[string]string
imageDownloaded map[string]struct{}
}
func (ih *imageHelper) downloadImages(n *html.Node) bool {
if n.Type != html.ElementNode || n.Data != "img" {
return false
}
for i, a := range n.Attr {
if a.Key != "src" {
continue
}
u, err := ih.baseURL.Parse(a.Val)
if err != nil {
fmt.Fprintf(os.Stderr, "downloadImages: %v\n", err)
continue
}
name := ih.downloadImage(u.String())
n.Attr[i].Val = name
}
return false
}
func (ih *imageHelper) downloadImage(imgURL string) string {
// if we already downloaded an image from this url return the filename
if v, ok := ih.urlToHash[imgURL]; ok {
return v
}
u, err := url.Parse(imgURL)
if err != nil {
fmt.Fprintf(os.Stderr, "downloadImage: %v\n", err)
return ""
}
ext := path.Ext(u.Path)
resp, err := http.Get(imgURL)
if err != nil {
fmt.Fprintf(os.Stderr, "downloadImage: %v\n", err)
return ""
}
defer resp.Body.Close()
// create a hash of the contents
h := sha1.New()
var buf bytes.Buffer
w := io.MultiWriter(h, &buf)
io.Copy(w, resp.Body)
// save url to hash mapping
filename := fmt.Sprintf("%x%v", h.Sum(nil), ext)
url := "https://images.nordstromdata.com/cc/img/" + filename
ih.urlToHash[imgURL] = url
// if content was downloaded from a different url don't save a new file
if _, ok := ih.imageDownloaded[filename]; ok {
return filename
}
var contentType string
switch ext {
case ".jpg", ".jpeg":
contentType = "image/jpeg"
case ".png":
contentType = "image/png"
case ".gif":
contentType = "image/gif"
}
sess := session.New(&aws.Config{Region: aws.String(config.region)})
svc := s3.New(sess)
params := &s3.PutObjectInput{
Bucket: aws.String(config.bucket),
Key: aws.String("cc/img/" + filename),
Body: bytes.NewReader(buf.Bytes()),
ContentType: aws.String(contentType),
GrantRead: aws.String("Everyone"),
}
_, err = svc.PutObject(params)
if err != nil {
fmt.Fprintf(os.Stderr, "downloadImage: uploading to s3: %v\n", err)
}
ih.imageDownloaded[filename] = struct{}{}
return filename
}