-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (173 loc) · 4 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
package main
import (
"bufio"
"context"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
"golang.org/x/net/html"
)
func main() {
// get HTML from stdin
rawPage, err := getInputFromPipe()
if err != nil {
log.Fatalf("can't get page: %v", err)
}
// parse HTML page into searchable tree
doc, err := html.Parse(strings.NewReader(rawPage))
if err != nil {
log.Fatalf("can't parse page: %v", err)
}
// search for a links with valid exts
links := make(map[string]bool)
findLinks(doc, links)
// make a Bar
wg := &sync.WaitGroup{}
progress := mpb.New(
mpb.WithWidth(60),
mpb.WithRefreshRate(180*time.Millisecond),
mpb.WithWaitGroup(wg),
)
mainBar := progress.Add(int64(len(links)),
mpb.NewBarFiller(mpb.BarStyle().Lbound("╢").Filler("▌").Tip("▌").Padding("░").Rbound("╟")),
mpb.PrependDecorators(
decor.Name("Total:", decor.WCSyncSpaceR),
decor.CountersNoUnit("%d / %d", decor.WCSyncSpaceR),
decor.Elapsed(decor.ET_STYLE_GO, decor.WCSyncWidthR),
),
mpb.AppendDecorators(
decor.OnComplete(
decor.Name("downloading...", decor.WCSyncSpaceR), " done!",
),
),
)
// make chan from links list
linksChan := make(chan string)
go func() {
for k := range links {
linksChan <- k
}
close(linksChan)
}()
// spawn workers
ctx := context.Background()
for i := 0; i < 5; i++ {
wg.Add(1)
go func(ctx context.Context, wg *sync.WaitGroup, ls <-chan string) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case l, ok := <-linksChan:
if !ok {
return
}
download(l, progress, mainBar)
}
}
}(ctx, wg, linksChan)
}
// wait until end
progress.Wait()
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func download(link string, p *mpb.Progress, mBar *mpb.Bar) {
fileNameParts := strings.Split(link, "/")
fileName := fileNameParts[len(fileNameParts)-1]
downloadToPath, _ := os.Getwd()
filePath := filepath.Join(downloadToPath, "sucker_downloads", fileName)
if fileExists(filePath) {
mBar.Increment()
return
}
resp, err := http.Get(link)
if err != nil {
log.Printf("[ERROR] can't request %s : %s", link, err)
}
bar, proxyReader := func(resp *http.Response, link string) (*mpb.Bar, io.ReadCloser) {
b := p.AddBar(
resp.ContentLength,
mpb.BarFillerClearOnComplete(),
mpb.BarOptOn(mpb.BarRemoveOnComplete(), func() bool { return true }), // del bar
mpb.PrependDecorators(
decor.Name(link, decor.WCSyncSpaceR),
decor.CountersKibiByte("% .2f / % .2f"),
),
mpb.AppendDecorators(
decor.EwmaETA(decor.ET_STYLE_GO, 90),
decor.Name(" ] "),
decor.EwmaSpeed(decor.UnitKiB, "% .2f", 60),
),
)
pR := b.ProxyReader(resp.Body)
return b, pR
}(resp, link)
_ = os.MkdirAll(filepath.Join(downloadToPath, "sucker_downloads"), os.ModePerm)
file, _ := os.Create(filePath)
_, err = io.Copy(file, proxyReader)
if err != nil {
bar.Abort(true)
}
mBar.Increment()
_ = proxyReader.Close()
_ = file.Close()
}
func findLinks(n *html.Node, acc map[string]bool) {
if n.Type == html.ElementNode && n.Data == "a" {
for _, a := range n.Attr {
if a.Key == "href" && isValidExt(a.Val) {
// TODO: hide the domain
acc["_a"+a.Val] = true
}
}
}
for ch := n.FirstChild; ch != nil; ch = ch.NextSibling {
findLinks(ch, acc)
}
}
func isValidExt(l string) bool {
exts := []string{"webm", "mp4"}
for _, ext := range exts {
if strings.Contains(l, ext) {
return true
}
}
return false
}
func getInputFromPipe() (string, error) {
nBytes, nChunks := int64(0), int64(0)
r := bufio.NewReader(os.Stdin)
buf := make([]byte, 0, 128*1024)
var res string
for {
n, err := r.Read(buf[:cap(buf)])
buf = buf[:n]
if n == 0 {
if err == nil {
continue
}
if err == io.EOF {
break
}
return "", err
}
nChunks++
nBytes += int64(len(buf))
res += string(buf)
if err != nil && err != io.EOF {
return "", nil
}
}
return res, nil
}