/
file.go
181 lines (149 loc) · 3.54 KB
/
file.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
package nzbfile
import (
"encoding/xml"
"io"
"log"
"os"
"path"
"regexp"
"strings"
"sync"
"sync/atomic"
"github.com/cheggaaa/pb"
)
type Nzb struct {
File []File `xml:"file"`
JobName string
fileRe *regexp.Regexp
progress *pb.ProgressBar
DoneCount int32
FailedCount int32
TotalCount int32
wg *sync.WaitGroup
}
type File struct {
Poster string `xml:"poster,attr"`
Date string `xml:"date,attr"`
Subject string `xml:"subject,attr"`
Group []string `xml:"groups>group"`
Segment []Segment `xml:"segments>segment"`
}
type Segment struct {
Bytes int64 `xml:"bytes,attr"`
Number int `xml:"number,attr"`
MessageID string `xml:",chardata"`
}
type SegmentRequest struct {
MessageID string
Bytes int64
filename string
workGroup *sync.WaitGroup
nzb *Nzb
excludedHosts map[string]bool
workQueue chan *SegmentRequest
failed bool
done bool
}
type WriterAtCloser interface {
io.WriterAt
io.Closer
}
func (n *Nzb) Done(failed bool) {
n.wg.Done()
atomic.AddInt32(&n.DoneCount, 1)
if failed {
atomic.AddInt32(&n.FailedCount, 1)
}
}
func (s *SegmentRequest) BuildWriter(outDir string, filename string) (WriterAtCloser, error) {
dirName := path.Join(outDir, s.nzb.JobName)
err := os.MkdirAll(dirName, 0700)
if err != nil {
return nil, err
}
outName := path.Join(outDir, s.nzb.JobName, filename)
f, err := os.OpenFile(outName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
return nil, err
}
return f, nil
}
func (s *SegmentRequest) Done(fail bool) {
if !s.done {
s.done = true
if fail {
s.failed = true
}
s.nzb.Complete(s.Bytes)
s.nzb.Done(fail)
}
}
func (s *SegmentRequest) FailServer(server string, numServers int) {
log.Printf("Failing server %s for message %s\n", server, s.MessageID)
if s.excludedHosts == nil {
s.excludedHosts = make(map[string]bool)
}
s.excludedHosts[server] = true
if len(s.excludedHosts) >= numServers {
s.Done(true)
log.Println("Failing Article", s.MessageID)
return
}
s.Queue()
}
func (s *SegmentRequest) Queue() {
if s.done || s.failed {
return
}
s.workQueue <- s
}
func (s *SegmentRequest) RequeueIfFailed(server string) bool {
if val, ok := s.excludedHosts[server]; ok && val {
log.Printf("Already failed at %s on %s\n", s.MessageID, server)
s.Queue()
return true
}
return false
}
func NewNZB(nzbfile []byte, jobName string, wg *sync.WaitGroup) (*Nzb, error) {
nzb := Nzb{JobName: jobName, fileRe: regexp.MustCompile(`"(.*)"`), wg: wg}
err := xml.Unmarshal(nzbfile, &nzb)
if err != nil {
return nil, err
}
return &nzb, nil
}
func (n *Nzb) extractFileName(subject string) string {
submatches := n.fileRe.FindSubmatch([]byte(subject))
if submatches == nil {
return ""
}
return string(submatches[0])
}
func (n *Nzb) EnqueueFetches(workQueue chan *SegmentRequest, fetchPar2 bool) {
var size int64
n.FailedCount = 0
n.DoneCount = 0
for _, file := range n.File {
filename := n.extractFileName(file.Subject)
isPar2 := strings.Contains(strings.ToLower(filename), "par2")
if fetchPar2 != isPar2 {
log.Println("Skipping", filename)
continue
}
for _, seg := range file.Segment {
size += seg.Bytes
n.wg.Add(1)
seg := SegmentRequest{MessageID: seg.MessageID, filename: filename, nzb: n, workQueue: workQueue, Bytes: seg.Bytes}
go seg.Queue()
}
}
bar := pb.New64(size).Prefix(n.JobName + " ")
bar.ShowSpeed = true
bar.SetUnits(pb.U_BYTES)
bar.Start()
n.progress = bar
}
func (n *Nzb) Complete(bytes int64) {
n.progress.Add64(bytes)
}