-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
186 lines (150 loc) · 3.69 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
package main
import (
"fmt"
"net/http"
"encoding/json"
"io/ioutil"
"os"
"io"
"strconv"
)
type Item struct {
Name string `json:"name"`
Type string `json:"type"`
Mtime string `json:"mtime"`
Size uint64 `json:"size,omitempty"`
}
func main() {
fmt.Println("----------------------------------------------------")
fmt.Println("------------| (Re)HLDS Installer v0.4 |-------------")
fmt.Println("----------------------------------------------------")
routes := make(map[int]string)
depth := 0
routes[depth] = "http://dl.rehlds.ru"
do(routes, Item{Name:"",Type:"",Mtime: ""}, depth)
}
func do(routes map[int]string, item Item, depth int) {
if isFile(item) {
err := load(routes, item, depth)
if err == nil {
if depth <= 1 {
do(routes, Item{Name:"",Type:"",Mtime: ""}, 0)
} else {
do(routes, Item{Name:"",Type:"",Mtime: ""}, depth - 1)
}
}
} else {
list(routes, item, depth)
}
}
func getUrl(routes map[int]string, item Item, depth int) string {
if len(item.Name) > 0 {
routes[depth] = routes[depth - 1] + "/" + item.Name
return routes[depth]
}
return routes[depth]
}
func isFile(item Item) bool {
return item.Type == "file"
}
func list(routes map[int]string, item Item, depth int) {
res, err := http.Get(getUrl(routes, item, depth))
if err != nil {
panic(err)
}
var data []Item
body, _ := ioutil.ReadAll(res.Body)
err = json.Unmarshal([]byte(body), &data)
if err != nil {
panic(err)
}
fmt.Println("Choose item (enter number):")
fmt.Printf("--------------------------------\n")
for i, item := range data{
if isFile(item) {
fmt.Printf("%-3d | %-110s | %-9s |\n", i + 1, item.Name, ByteCountDecimal(item.Size))
} else {
fmt.Printf("%-3d | %-110s | --- |\n", i + 1, item.Name)
}
}
fmt.Printf("--------------------------------\n")
if depth <= 0 {
fmt.Printf("0: Exit\n")
} else {
fmt.Printf("0: Back\n")
}
var point string
fmt.Scanf("%s\n", &point)
key := 0
if len(point) > 0 {
num, err := strconv.Atoi(point)
if err != nil {
fmt.Printf("\n===============\nInvalid key\n===============\n\n")
do(routes, item, depth)
}
key = num
}
if key == 0 {
if depth <= 0 {
os.Exit(0)
} else {
do(routes, Item{Name:"",Type:"",Mtime: ""}, depth - 1)
}
}
if key <= len(data) {
do(routes, data[key - 1], depth + 1)
} else {
fmt.Printf("\n===============\nItem with key %d not found\n===============\n\n", key)
}
do(routes, item, depth)
}
type WriteCounter struct {
All uint64
Total uint64
}
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
func (wc WriteCounter) PrintProgress() {
//fmt.Printf("\r%s", strings.Repeat(" ", 35))
fmt.Printf("\rDownloading... %d%% (%d/%d) complete", (100 * wc.Total / wc.All), wc.Total, wc.All)
}
func load(routes map[int]string, item Item, depth int) error {
out, err := os.Create(item.Name + ".tmp")
if err != nil {
return err
}
// Get the data
resp, err := http.Get(getUrl(routes, item, depth))
if err != nil {
out.Close()
return err
}
defer resp.Body.Close()
counter := &WriteCounter{All: item.Size}
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
out.Close()
return err
}
fmt.Print("\n")
out.Close()
if err = os.Rename(item.Name + ".tmp", item.Name); err != nil {
return err
}
return nil
}
func ByteCountDecimal(b uint64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}