-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
293 lines (252 loc) · 5.92 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
var hostUrl url.URL
var maxThreads int64
var threads int64 = 0
var writeUrlOnly bool
var urlFile *os.File
var urlFileMtx sync.Mutex
var wg sync.WaitGroup
func getDownloadPath(entryPath string) (string, error) {
parts := []string{"downloads"}
for _, str := range strings.Split(entryPath, "/") {
str, err := url.PathUnescape(str)
if err != nil {
return "", err
}
str = strings.TrimSpace(str)
if len(str) > 0 {
parts = append(parts, str)
}
}
return strings.Join(parts, "/"), nil
}
func getDownloadUrl(entryHost url.URL, entryPath string) string {
entryHost.Path = entryPath
return entryHost.String()
}
func GetFileSize(name string) (int64, error) {
stat, err := os.Stat(name)
if err == nil {
return stat.Size(), nil
}
return 0, err
}
func writeUrl(fileUrl string) {
urlFileMtx.Lock()
defer urlFileMtx.Unlock()
urlFile.WriteString(fileUrl + "\n")
}
func downloadUrl(entryPath string, downloadSize int64, modifiedTime uint64) {
fileName, err := getDownloadPath(entryPath)
if err != nil {
return
}
folder := filepath.Dir(fileName)
if os.MkdirAll(folder, os.ModePerm) != nil {
return
}
nThreads := atomic.LoadInt64(&threads)
// Verify file integrity
fileSize, err := GetFileSize(fileName)
if err == nil {
if fileSize == downloadSize {
fmt.Printf("[%03d] Intact # %s\n", nThreads, fileName)
return
}
fmt.Printf("[%03d] Damaged # %s\n", nThreads, fileName)
os.Remove(fileName)
}
entryUrl := getDownloadUrl(hostUrl, entryPath)
fmt.Printf("[%03d] Downloading # %s\n", nThreads, entryUrl)
resp, err := http.Get(entryUrl)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("[%03d] Saving # %s\n", nThreads, fileName)
file, err := os.Create(fileName)
if err != nil {
fmt.Printf("%s: %s\n", fileName, err.Error())
os.Remove(fileName)
return
}
defer file.Close()
var buffer = make([]byte, 4096)
for {
n, err := resp.Body.Read(buffer)
if err != nil {
if err == io.EOF {
return
} else {
fmt.Printf("[%03d] ERROR # %s: %s\n", nThreads, fileName, err.Error())
os.Remove(fileName)
return
}
}
if n != 4096 {
_, err = file.Write(buffer[:n])
} else {
_, err = file.Write(buffer)
}
if err != nil {
fmt.Printf("[%03d] ERROR # %s: %s\n", nThreads, fileName, err.Error())
os.Remove(fileName)
return
}
}
}
func saveContent(fileUrl string, downloadSize int64, modifiedTime uint64) {
writeUrl(fileUrl)
if !writeUrlOnly {
downloadUrl(fileUrl, downloadSize, modifiedTime)
}
}
type FileEntry struct {
Path string `json:"href"`
Time uint64 `json:"time"`
Size int64 `json:"size"`
}
func getFileIndex(entryPath string) ([]FileEntry, error) {
request := struct {
Action string `json:"action"`
Items struct {
Path string `json:"href"`
Depth int `json:"what"`
} `json:"items"`
}{}
request.Action = "get"
request.Items.Path = entryPath
request.Items.Depth = 1
request_data, err := json.Marshal(request)
if err != nil {
return nil, err
}
resp, err := http.Post(hostUrl.String(), "application/json", bytes.NewBuffer(request_data))
if err != nil {
fmt.Printf("Error: %v\n", err)
return nil, err
}
defer resp.Body.Close()
fileIndex := struct {
Items []FileEntry `json:"items"`
}{}
err = json.NewDecoder(resp.Body).Decode(&fileIndex)
if err != nil {
return nil, err
}
return fileIndex.Items, nil
}
func crawlDirectory(entryPath string) {
fileEntries, err := getFileIndex(entryPath)
if err != nil {
return
}
for _, entry := range fileEntries {
if entryPath == entry.Path || !strings.HasPrefix(entry.Path, entryPath) {
continue
}
if strings.HasSuffix(entry.Path, "/") {
crawlDirectoryAsync(entry.Path)
} else {
saveContentAsync(entry.Path, entry.Size, entry.Time)
}
}
}
func saveContentAsync(entryPath string, fileSize int64, fileTime uint64) {
f_async := func() {
wg.Add(1)
defer wg.Done()
saveContent(entryPath, fileSize, fileTime)
atomic.AddInt64(&threads, -1)
}
nThreads := atomic.AddInt64(&threads, 1)
if nThreads <= maxThreads {
go f_async()
} else {
atomic.AddInt64(&threads, -1)
saveContent(entryPath, fileSize, fileTime)
}
}
func crawlDirectoryAsync(entryPath string) {
f_async := func() {
wg.Add(1)
defer wg.Done()
crawlDirectory(entryPath)
atomic.AddInt64(&threads, -1)
}
nThreads := atomic.AddInt64(&threads, 1)
if nThreads <= maxThreads {
go f_async()
} else {
atomic.AddInt64(&threads, -1)
crawlDirectory(entryPath)
}
}
func printUsage() {
println("Usage: h5ai-dl.exe [1] [2] [3]")
println(" 1: A url for a h5ai hsoted website")
println(" 2: Only save urls, and dont download files? (1, t, T, TRUE, true, True / 0, f, F, FALSE, false, False)")
println(" 3: [OPTIONAL] Amount of threads to use, defaults to CPU core count")
}
func main() {
if len(os.Args) < 3 {
printUsage()
return
}
requestUrl, err := url.Parse(os.Args[1])
if err != nil {
printUsage()
println("Error: 1st argument: " + err.Error())
return
}
if requestUrl.Scheme != "http" && requestUrl.Scheme != "https" {
printUsage()
println("Error: 1st argument is not a http or https url!")
return
}
hostUrl = *requestUrl
hostUrl.Path = ""
requestPath := requestUrl.Path
writeUrlOnly, err = strconv.ParseBool(os.Args[2])
if err != nil {
printUsage()
println("Error: 2nd argument: " + err.Error())
return
}
if len(os.Args) > 3 {
maxThreads, err = strconv.ParseInt(os.Args[3], 10, 64)
if err != nil {
printUsage()
println("Error: 3rd argument: " + err.Error())
return
}
} else {
maxThreads = int64(runtime.NumCPU())
}
urlFile, err = os.Create("urls.txt")
if err != nil {
println("Error: Failed to create \"urls.txt\"")
return
}
defer urlFile.Close()
crawlDirectory(requestPath)
time.Sleep(time.Second)
wg.Wait()
println("Done!")
}