forked from onisuly/PanDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
305 lines (265 loc) · 6.41 KB
/
main.go
File metadata and controls
305 lines (265 loc) · 6.41 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
type param struct {
url string
size uint64
block uint64
chunk uint64
name string
bduss string
dir string
debug bool
}
type task struct {
url string
file *os.File
start uint64
end uint64
chunkSize uint64
downloadedSize *uint64
bduss string
}
const cfgFileName = "pandownloader.json"
func main() {
params := parseParams()
err := parallelDownload(params)
if err != nil {
log.Fatalln(err)
}
}
func parallelDownload(p param) error {
filename, length, err := parseHeader(p.url, p.bduss)
if err != nil {
return err
}
if p.name != "" {
filename = p.name
}
file, err := os.Create(path.Join(p.dir, filename))
if err != nil {
return err
}
defer file.Close()
fmt.Printf("file size: %s\n", formatBytes(length))
if length < p.block*p.size {
p.block = map[bool]uint64{true: length / p.size, false: 1}[length/p.size > 0]
}
fmt.Printf("download start")
startTime := time.Now()
var downloadedSize uint64 = 0
tasks := createTasks(p.url, file, length, p.block, p.chunk, p.bduss, &downloadedSize)
var wg sync.WaitGroup
for i := uint64(0); i < p.size; i++ {
wg.Add(1)
go func(tasks chan task) {
for t := range tasks {
for err := download(t); err != nil; {
if p.debug {
log.Println(err)
}
err = download(t)
}
}
wg.Done()
}(*tasks)
}
wg.Add(1)
printProgress(length, &downloadedSize, func() { wg.Done() })
wg.Wait()
fmt.Printf("\ndownload completed, time elapsed: %s, average speed: %s/s\n", time.Since(startTime),
formatBytes(length/uint64(time.Since(startTime).Seconds())))
return nil
}
func createTasks(url string, file *os.File, length uint64, block uint64, chunkSize uint64, bduss string,
downloadedSize *uint64) *chan task {
tasks := make(chan task)
split := length / block
go func() {
for i := uint64(0); i < split; i++ {
tasks <- task{
url: url,
file: file,
start: i * block,
end: (i + 1) * block,
chunkSize: chunkSize,
bduss: bduss,
downloadedSize: downloadedSize,
}
}
if length%block != 0 {
tasks <- task{
url: url,
file: file,
start: split * block,
end: length,
chunkSize: chunkSize,
bduss: bduss,
downloadedSize: downloadedSize,
}
}
close(tasks)
}()
return &tasks
}
func download(t task) error {
req, err := http.NewRequest("GET", t.url, nil)
if err != nil {
return err
}
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", t.start, t.end))
req.AddCookie(&http.Cookie{Name: "BDUSS", Value: t.bduss})
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 206 {
bytes, _ := ioutil.ReadAll(resp.Body)
return errors.New(string(bytes))
}
reader := bufio.NewReader(resp.Body)
position := t.start
part := make([]byte, t.chunkSize)
for {
count, err := reader.Read(part)
t.file.WriteAt(part[:count], int64(position))
position += uint64(count)
atomic.AddUint64(t.downloadedSize, uint64(count))
if err == io.EOF {
break
} else if err != nil {
return err
}
}
return nil
}
func parseHeader(url string, bduss string) (filename string, length uint64, err error) {
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return "", 0, err
}
req.AddCookie(&http.Cookie{Name: "BDUSS", Value: bduss})
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return "", 0, err
}
if res.StatusCode != 200 {
resp, _ := client.Get(url)
defer resp.Body.Close()
bytes, _ := ioutil.ReadAll(resp.Body)
return "", 0, errors.New(string(bytes))
}
maps := res.Header
length, err = strconv.ParseUint(maps["Content-Length"][0], 10, 64)
if err != nil {
return "", 0, err
}
if maps["Content-Disposition"] != nil {
if _, params, err := mime.ParseMediaType(maps["Content-Disposition"][0]); err == nil {
filename = params["filename"]
}
}
if filename == "" {
filename = strings.Split(path.Base(url), "?")[0]
}
return filename, length, nil
}
func parseParams() param {
var url = flag.String("url", "", "url to download, required")
var size = flag.Uint64("size", 32, "concurrent downloads size")
var block = flag.Uint64("block", 20971520, "max block size")
var chunk = flag.Uint64("chunk", 1048576, "max chunk size")
var name = flag.String("name", "", "download file name")
var bduss = flag.String("bduss", "", "BDUSS cookie")
var dir = flag.String("dir", "", "download dir")
var debug = flag.Bool("debug", false, "enable debug mode")
flag.Parse()
if len(os.Args) == 2 {
*url = os.Args[1]
}
if *url == "" {
flag.PrintDefaults()
os.Exit(1)
}
var p = param{
url: *url,
size: *size,
block: *block,
chunk: *chunk,
name: *name,
bduss: *bduss,
dir: *dir,
debug: *debug,
}
flagSet := make(map[string]bool)
flag.Visit(func(f *flag.Flag) { flagSet[f.Name] = true })
return updateParamsWithCfgFile(p, flagSet)
}
func updateParamsWithCfgFile(p param, flagSet map[string]bool) (result param) {
result = p
ex, _ := os.Executable()
confPath := path.Join(filepath.Dir(ex), cfgFileName)
if _, err := os.Stat(confPath); !os.IsNotExist(err) {
bytes, err := ioutil.ReadFile(confPath)
if err != nil {
return
}
var fileCfg cfgFile
err = json.Unmarshal(bytes, &fileCfg)
if err != nil {
return
}
if fileCfg.Size != 0 && !flagSet["size"] {
result.size = fileCfg.Size
}
if fileCfg.Block != 0 && !flagSet["block"] {
result.block = fileCfg.Block
}
if fileCfg.Chunk != 0 && !flagSet["chunk"] {
result.chunk = fileCfg.Chunk
}
if !flagSet["bduss"] {
result.bduss = fileCfg.BDUSS
}
if !flagSet["dir"] {
result.dir = fileCfg.Dir
}
}
return
}
func printProgress(length uint64, downloadedSize *uint64, done func()) {
var preDownloadedSize uint64 = 0
for range time.Tick(time.Second) {
currentDownloadedSize := *downloadedSize
oneSecondSize := currentDownloadedSize - preDownloadedSize
preDownloadedSize = currentDownloadedSize
fmt.Print("\033[2K")
fmt.Printf("\r%s / %s downloaded, speed: %s/s", formatBytes(currentDownloadedSize), formatBytes(length),
formatBytes(oneSecondSize))
if currentDownloadedSize >= length {
done()
return
}
}
}