/
pounce.go
198 lines (171 loc) · 4.25 KB
/
pounce.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
// pounce.go
package main
import (
"bufio"
"errors"
"fmt"
"github.com/codegangsta/cli"
"github.com/guelfey/go.dbus"
"io"
"net/http"
"os"
"strings"
"time"
)
var (
transactions map[string]Transaction = make(map[string]Transaction)
)
type Transaction struct {
Url string
Destination string
}
func notify(msg string) {
conn, err := dbus.SessionBus()
if err != nil {
fmt.Printf("Unable to connect to dbus session bus: %v\n", err)
}
obj := conn.Object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
call := obj.Call("org.freedesktop.Notifications.Notify", 0, "", uint32(0),
"", "pounce", msg, []string{}, map[string]dbus.Variant{}, int32(5000))
if call.Err != nil {
fmt.Printf("Error while trying to send notification: %v\n", call.Err)
}
}
func nameGenerator(url, destination string) string {
var name string
urlSplit := strings.Split(url, "/")
if urlSplit[len(urlSplit)-1] != "" {
name = urlSplit[len(urlSplit)-1]
} else {
name = urlSplit[len(urlSplit)-2]
}
destination = strings.Trim(destination, "/")
return fmt.Sprintf("%v/%v", destination, name)
}
func download(url, output string, multi bool, response chan<- *http.Response) {
fmt.Printf("Retreiving from %v\n", url)
if multi {
output = nameGenerator(url, output)
}
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Unable to get resource from '%v'. Error: %v\n", url, err)
}
go func() {
transaction := Transaction{Url: resp.Request.URL.String(), Destination: output}
transactions[resp.Request.URL.String()] = transaction
}()
response <- resp
}
func readFile(filename string) []string {
var urls []string
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Problem opening input file!")
panic(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf("Problem reading input file!")
panic(err)
}
return urls
}
func create(filename string) *os.File {
file, err := os.Create(filename)
if err != nil {
fmt.Printf("Unable to create file!")
panic(err)
}
return file
}
func save(file io.Writer, resp *http.Response) int64 {
complete, err := io.Copy(file, resp.Body)
if err != nil {
fmt.Printf("Unable to write file, error: %v\n", err)
}
return complete
}
func main() {
app := cli.NewApp()
app.Name = "gopounce"
app.Version = "0.0.2"
app.Usage = "A very simple file downloader in the vein of wget."
app.Authors = []cli.Author{cli.Author{
Name: "Brian Tomlinson",
Email: "darthlukan@gmail.com",
}}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "url, u",
Value: "",
Usage: "The input URL",
},
cli.StringFlag{
Name: "file, f",
Value: "",
Usage: "The input file",
},
cli.StringFlag{
Name: "outfile, o",
Value: "",
Usage: "The output file",
},
cli.StringFlag{
Name: "dir, d",
Value: "",
Usage: "The output directory",
},
}
app.Action = func(c *cli.Context) {
counter := 0
url := c.String("url")
inFile := c.String("file")
outFile := c.String("outfile")
outDir := c.String("dir")
if url == "" && inFile == "" {
fmt.Println(errors.New("Missing input arguments, please see 'pounce --help'\n"))
return
} else if outFile == "" && outDir == "" {
fmt.Println(errors.New("Missing output arguments, please see 'pounce --help'\n"))
return
}
startTime := time.Now()
respChan := make(chan *http.Response)
if inFile != "" && outDir != "" {
urls := readFile(inFile)
for _, url := range urls {
go download(url, outDir, true, respChan)
counter += 1
}
}
if url != "" && outFile != "" {
go download(url, outFile, false, respChan)
counter += 1
}
for counter > 0 {
select {
case r := <-respChan:
u := fmt.Sprintf("%v", r.Request.URL.String())
if transaction, ok := transactions[u]; ok == true {
defer r.Body.Close()
f := create(transaction.Destination)
defer f.Close()
bytesWritten := save(f, r)
endTime := time.Now()
msg := fmt.Sprintf("Downloaded %vkb file '%v' in %v\n",
bytesWritten/1024, f.Name(), endTime.Sub(startTime))
go notify(msg)
fmt.Printf(msg)
} else {
fmt.Printf("%v: %v.\n", "Problem processing transaction", r.Body)
}
counter--
}
}
}
app.Run(os.Args)
}