-
Notifications
You must be signed in to change notification settings - Fork 3
/
chrome.go
187 lines (145 loc) · 4.62 KB
/
chrome.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
// not used file atm
package main
import (
"context"
"crypto/md5"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"time"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/chromedp"
"github.com/google/uuid"
)
type Chrome struct {
headless bool
}
func NewChrome(headless bool) *Chrome {
return &Chrome{headless: headless}
}
func (c *Chrome) chromeOpts() []func(*chromedp.ExecAllocator) {
opts := []func(*chromedp.ExecAllocator){
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.Flag("disable-background-networking", true),
chromedp.Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
chromedp.Flag("disable-background-timer-throttling", true),
chromedp.Flag("disable-backgrounding-occluded-windows", true),
chromedp.Flag("disable-breakpad", true),
chromedp.Flag("disable-client-side-phishing-detection", true),
chromedp.Flag("disable-default-apps", true),
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("disable-extensions", true),
chromedp.Flag("disable-features", "site-per-process,TranslateUI,BlinkGenPropertyTrees"),
chromedp.Flag("disable-hang-monitor", true),
chromedp.Flag("disable-ipc-flooding-protection", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("force-color-profile", "srgb"),
chromedp.Flag("metrics-recording-only", true),
chromedp.Flag("safebrowsing-disable-auto-update", true),
chromedp.Flag("enable-automation", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("use-mock-keychain", true),
chromedp.DisableGPU,
}
if c.headless {
opts = append(opts,
chromedp.Headless,
)
}
return opts
}
// func (c *Chrome) GetDownlaodHrefUsingChrome(url string) (string, error) {
// var href string
// var err error
// for i := 0; i < 10; i++ {
// log.Printf("Navigating to %s", url)
// ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
// defer cancel()
// opts := c.chromeOpts()
// alloCtx, cancel := chromedp.NewExecAllocator(ctx, opts...)
// defer cancel()
// logOpts := chromedp.WithErrorf(log.Printf)
// taskCtx, cancel := chromedp.NewContext(alloCtx, logOpts)
// defer cancel()
// err = chromedp.Run(taskCtx,
// page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorDeny),
// chromedp.Navigate(url),
// )
// if err != nil {
// log.Printf("Could not navigate to %s: %s", url, err)
// continue
// }
// time.Sleep(time.Second * 15)
// log.Println("Trying to query for href")
// err = chromedp.Run(taskCtx,
// chromedp.Evaluate(`$('a').map((i, el) => $(el).attr('href')).toArray().find(h => h.match(/\/wow\/addons\/.+\/download\/\d+\/file/))`, &href),
// )
// log.Printf(`Evaluate result is "%s": "%v"`, href, err)
// if err != nil {
// continue
// }
// if href != "" {
// break
// }
// }
// if err != nil {
// return "", fmt.Errorf("Could get download url from %s: %s", url, err)
// }
// return href, nil
// }
func (c *Chrome) DownloadFileUsingChrome(url, tmp string) (string, string, error) {
folderName := uuid.New().String()
folder := fmt.Sprintf("%s/%s", tmp, folderName)
os.MkdirAll(folder, os.ModePerm)
log.Printf("Navigating to %s", url)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
opts := c.chromeOpts()
alloCtx, cancel := chromedp.NewExecAllocator(ctx, opts...)
defer cancel()
logOpts := chromedp.WithErrorf(log.Printf)
taskCtx, cancel := chromedp.NewContext(alloCtx, logOpts)
defer cancel()
log.Printf("Setting download path to %s", folder)
err := chromedp.Run(taskCtx,
page.SetDownloadBehavior(page.SetDownloadBehaviorBehaviorAllow).WithDownloadPath(folder),
chromedp.Navigate(url),
)
time.Sleep(time.Second * 20)
if err != nil {
log.Printf(`Chrome session exit error that will be ignored: "%v"`, err)
}
files, err := filepath.Glob(fmt.Sprintf("%s/*", folder))
if err != nil {
return "", "", fmt.Errorf("Could not find files in %s: %s", folder, err)
}
if len(files) == 0 {
return "", "", fmt.Errorf("Folder %s is empty", folder)
}
var file string
reg := regexp.MustCompile(`\.zip$`)
for _, f := range files {
if reg.MatchString(f) {
file = f
break
}
}
mdbuf := md5.New()
f, err := os.Open(file)
if err != nil {
return "", "", fmt.Errorf("Could not open file %s: %s", file, err)
}
_, err = io.Copy(mdbuf, f)
if err != nil {
return "", "", fmt.Errorf("Could not read file %s to calculate checksum: %s", file, err)
}
sum := fmt.Sprintf("%x", mdbuf.Sum(nil))
return file, sum, nil
}