-
Notifications
You must be signed in to change notification settings - Fork 2
/
gonzbee.go
310 lines (279 loc) · 6.23 KB
/
gonzbee.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//Copyright 2012, Daniel Morsing
//For licensing information, See the LICENSE file
package main
import (
"bytes"
"errors"
_ "expvar"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
_ "net/http/pprof"
"net/textproto"
"os"
"path/filepath"
"regexp"
"sync"
"github.com/DanielMorsing/gonzbee/nntp"
"github.com/DanielMorsing/gonzbee/nzb"
"github.com/DanielMorsing/gonzbee/par2"
"github.com/DanielMorsing/gonzbee/yenc"
)
var (
rm = flag.Bool("rm", false, "Remove the nzb file after downloading")
saveDir = flag.String("d", "", "Save to this directory")
par = flag.Bool("par", false, "only download par2 files")
profAddr = flag.String("prof", "", "address to open profiling server on")
)
var extStrip = regexp.MustCompile(`\.nzb$`)
var existErr = errors.New("file exists")
func main() {
flag.Parse()
if flag.NArg() == 0 {
fmt.Fprintln(os.Stderr, "No NZB files given")
os.Exit(1)
}
if *profAddr != "" {
laddr, err := net.Listen("tcp", *profAddr)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
go func() {
log.Fatalln(http.Serve(laddr, nil))
}()
}
for _, path := range flag.Args() {
file, err := os.Open(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
nzb, err := nzb.Parse(file)
file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
err = downloadNzb(nzb, extStrip.ReplaceAllString(path, ""))
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
if *rm {
err = os.Remove(path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
filewg.Wait()
}
// download all the files contained in an nzb,
func downloadNzb(nzbFile *nzb.Nzb, dir string) error {
if *saveDir != "" {
dir = *saveDir
}
err := os.Mkdir(dir, os.ModePerm)
// if the directory already exist, assume that it's an old download that was canceled
// and restarted.
if err != nil && !os.IsExist(err) {
return err
}
parfiles := filterPars(nzbFile)
// first download the parfiles
for file, _ := range parfiles {
err = downloadFile(dir, file)
if err == existErr {
continue
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
if *par {
for _, pfiles := range parfiles {
for _, f := range pfiles {
err := downloadFile(dir, f.file)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
return nil
}
// download the rest of the files.
for _, file := range nzbFile.File {
err = downloadFile(dir, file)
if err == existErr {
continue
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
// create a list of files downloaded
var paths []string
for _, file := range nzbFile.File {
filename := file.Subject.Filename()
path := filepath.Join(dir, filename)
paths = append(paths, path)
}
filewg.Wait()
for fp, set := range parfiles {
var n int
paths, n, err = verifyPar(fp, dir, paths)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
files := selectPars(set, n)
for _, file := range files {
err = downloadFile(dir, file)
if err == existErr {
continue
} else if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
return nil
}
func verifyPar(fp *nzb.File, dir string, paths []string) ([]string, int, error) {
filename := fp.Subject.Filename()
path := filepath.Join(dir, filename)
f, err := os.Open(path)
if err != nil {
return paths, 0, err
}
defer f.Close()
fset := par2.NewFileset(f)
if !fset.CanVerify() {
return paths, 0, nil
}
pathSet := make(map[string]bool)
for _, s := range paths {
pathSet[s] = true
}
matches, blockNeeded := fset.Verify(paths)
for _, fm := range matches {
if pathSet[fm.Path] {
delete(pathSet, fm.Path)
par2path := filepath.Join(dir, fm.File.Name)
if par2path != fm.Path {
err := os.Rename(fm.Path, par2path)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
}
retPaths := make([]string, 0, len(pathSet))
for s := range pathSet {
retPaths = append(retPaths, s)
}
return retPaths, blockNeeded, nil
}
// download a single file contained in an nzb.
func downloadFile(dir string, nzbfile *nzb.File) error {
file, err := newFile(dir, nzbfile)
if err != nil {
return err
}
for _, f := range nzbfile.Segments {
c, err := getConn()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
go decodeMsg(c, file, nzbfile.Groups, f.MsgId)
}
return nil
}
// decodes an nntp message and writes it to a section of the file.
func decodeMsg(c *nntp.Conn, f *file, groups []string, MsgId string) {
var err error
defer f.Done()
rc, err := c.GetMessage(MsgId)
if err != nil {
fmt.Fprintln(os.Stderr, "nntp error getting", MsgId, ":", err)
if _, ok := err.(*textproto.Error); ok {
putConn(c)
} else {
putBroken(c)
}
return
}
putConn(c)
yread, err := yenc.NewPart(bytes.NewBuffer(rc))
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
wr := f.WriterAt(yread.Begin)
_, err = io.Copy(wr, yread)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
// waitgroup that keeps track if there are any files being downloaded.
var filewg sync.WaitGroup
type file struct {
name string
path string
file *os.File
partsLeft int
mu sync.Mutex
}
func newFile(dirname string, nzbfile *nzb.File) (*file, error) {
filename := nzbfile.Subject.Filename()
if filename == "" {
return nil, errors.New("bad subject")
}
path := filepath.Join(dirname, filename)
if _, err := os.Stat(path); err == nil {
return nil, existErr
}
temppath := path + ".gonztemp"
f, err := os.Create(temppath)
if err != nil {
return nil, err
}
ret := &file{
name: filename,
path: path,
partsLeft: len(nzbfile.Segments),
file: f,
}
filewg.Add(1)
return ret, nil
}
func (f *file) WriterAt(offset int64) io.Writer {
return &fileWriter{
f: f.file,
offset: offset,
}
}
func (f *file) Done() {
f.mu.Lock()
defer f.mu.Unlock()
f.partsLeft--
if f.partsLeft != 0 {
return
}
fmt.Printf("Done downloading file %q\n", f.name)
os.Rename(f.file.Name(), f.path)
f.file.Close()
filewg.Done()
}
// filewriter allows for multiple goroutines to write concurrently to
// non-overlapping sections of a file
type fileWriter struct {
f *os.File
offset int64
}
func (f *fileWriter) Write(b []byte) (int, error) {
n, err := f.f.WriteAt(b, f.offset)
f.offset += int64(n)
return n, err
}