-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
82 lines (65 loc) · 1.44 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
/*
Copyright © 2022 edivangalindo
*/
package main
import (
"bufio"
"flag"
"fmt"
"net/url"
"os"
"strings"
"github.com/cavaliergopher/grab/v3"
)
func main() {
threads := flag.Int("t", 50, "Number of threads to utilise.")
// Check for stdin input
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {
fmt.Fprintln(os.Stderr, "No urls detected. Hint: cat urls.txt | dwlr")
os.Exit(1)
}
results := make(chan string, *threads)
go func() {
// Read urls from stdin
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
url := s.Text()
hostname, filename, err := extract(url)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// Download the url
resp, err := grab.Get("./dwlr/"+hostname+"/"+filename, url)
if err != nil {
fmt.Println(err)
}
printResult("Downloaded: "+resp.Filename, results)
}
close(results)
}()
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
for res := range results {
fmt.Fprintln(w, res)
}
}
func printResult(url string, results chan string) {
result := url
if result != "" {
results <- result
}
}
// Extract the hostname and path from a url
func extract(urlString string) (string, string, error) {
u, err := url.Parse(urlString)
if err != nil {
return "", "", err
}
// Get last resource asked of url
// Example: https://www.google.com/test.js => test.js
f := strings.Split(u.String(), "/")
l := f[len(f)-1]
return u.Hostname(), l, nil
}