-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (98 loc) · 2.38 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"path"
"strings"
"time"
"wget/pkg"
)
var (
B bool
O string
P string
I string
RateLimit string
Mirror bool
Reject string
Exclude string
)
func init() {
flag.BoolVar(&B, "B", false, "Output in wget-log")
flag.StringVar(&O, "O", "", "Specify download filename")
flag.StringVar(&P, "P", "./", "Specify download directory")
flag.StringVar(&I, "i", "", "Download multiple files")
flag.StringVar(&RateLimit, "rate-limit", "1000M", "The rate limit in k = KB/s or M = MB/s")
flag.BoolVar(&Mirror, "mirror", false, "Mirror the whole site")
flag.StringVar(&Reject, "reject", "", "Reject files")
flag.StringVar(&Reject, "R", "", "Reject files")
flag.StringVar(&Exclude, "exclude", "", "Exclude directory")
flag.StringVar(&Exclude, "X", "", "Exclude directory")
}
func main() {
flag.Parse()
url := flag.Arg(0)
rate, err := pkg.GetRateLimit(RateLimit)
if err != nil {
fmt.Println(err)
return
}
if Mirror {
pkg.Mirror(url, Exclude, Reject)
} else if I != "" && (O == "") {
urls, err := pkg.ReadDownloadFile(I)
if err != nil {
fmt.Println(err)
return
}
filePath := P
if strings.Contains(P, "~") {
usr, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
return
}
filePath = path.Join(usr, P[1:])
}
pkg.DownloadMultipleFiles(filePath, urls, rate)
} else {
fileName := path.Base(url) // extract the file name from the url
if O != "" && I == "" {
fileName = O
}
client := &http.Client{}
res, err := http.NewRequest("GET", url, nil)
if err != nil {
return
}
res.Header.Set("User-Agent", "golang-wget-project")
response, err := client.Do(res)
if err != nil {
return
}
defer response.Body.Close()
download := &pkg.Download{Response: response, StartTime: time.Now(), ContentLength: float64(response.ContentLength), BarWidth: pkg.GetTerminalLength(), Url: url, Path: "./" + fileName}
if P != "./" {
download.Path = P + "/" + fileName
}
download.PrintOrLogBefore(B)
resp, err := download.DownloadFile(response, rate)
if err != nil {
fmt.Println(err)
return
}
filePath := P
if strings.Contains(P, "~") {
usr, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
return
}
filePath = path.Join(usr, P[1:])
}
pkg.SaveBytesToFile(filePath, fileName, resp)
download.PrintOrLogAfter(B)
}
}