/
worker.go
292 lines (249 loc) · 7.03 KB
/
worker.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
package main
import(
"exec"
"fmt"
"flag"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"strconv"
)
var binary = flag.String("downloader", "youtube-dl", "Path to youtube-dl")
var fileNameFormat = flag.String("name_format", "%(stitle)s.%(ext)s", "File name format to pass to youtube-dl")
var outputFormat = flag.String("output_format", "mp3", "Output format to write.")
type DownloadState int
const (
kStarting DownloadState = 1
kDownloadingMetadata = 2
kDownloadingVideo = 3
kExtractingAudio = 4
kTagging = 5
kComplete = 6
)
type DownloadRequest struct {
id int
url string
basePath string
statusChannel chan StatusMessage
}
func (d *DownloadRequest) WriteError(step string, e os.Error) {
fmt.Printf("Error (%s): %v -> %v\n", step, e, d.statusChannel)
var msg StatusMessage
msg.requestId = d.id
msg.step = step
msg.error = e
d.statusChannel <- msg
}
func MakeCommand(request *DownloadRequest, tempFile string) *exec.Cmd {
bin, e := filepath.Abs(*binary)
if e != nil {
return nil
}
cmd := exec.Command(bin, "-o", tempFile, "-c", "-k", "--write-info-json", "--extract-audio", "--audio-format=mp3", "--audio-quality=192k", request.url)
cmd.Dir = request.basePath
return cmd
}
type StatusMessage struct {
requestId int
state DownloadState
link string
title string
bytesTransferred, totalBytes, secondsRemaining uint64
error os.Error
step string
}
func (s StatusMessage) IsError() bool {
return s.error != nil
}
func (s StatusMessage) IsLast() bool {
return s.IsError() || s.state == kComplete
}
func SizeStringToInt(s string) (uint64, os.Error) {
base, err := strconv.Atof64(s[0:len(s) - 1])
if err != nil {
return 0, err
}
switch strings.ToLower(s[len(s) - 1:len(s)])[0] {
case 'k':
return uint64(base * 1024), nil
case 'm':
return uint64(base * 1024 * 1024), nil
case 'g':
return uint64(base * 1024 * 1024 * 1024), nil
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return uint64(base * 10) + uint64((uint8(s[len(s) - 1]) - '0')), nil
}
return 0, os.NewError("Unknown trailing char in size string!")
}
func NewStatusMessage(s string) *StatusMessage {
var msg StatusMessage
progressRe := regexp.MustCompile("\\[download\\][ \t]+([0-9.]+)%[ \t]+of[ \t]+([0-9\\.mkgb]+) at[ \t]+([0-9.gkmb\\-]+)/s[ \t]+eta[ \t]+([0-9\\-]+):([0-9\\-]+)")
if strings.HasPrefix(s, "[info]") && strings.Index(s, ":") != -1 {
msg.state = kDownloadingMetadata
} else if match := progressRe.FindStringSubmatch(strings.ToLower(s)); match != nil {
msg.state = kDownloadingVideo
progress, e := strconv.Atof32(match[1])
if e != nil {
return nil
}
msg.totalBytes, e = SizeStringToInt(match[2])
if e != nil {
return nil
} else {
minsRemaining, e := strconv.Atoi(match[4])
if e == nil {
secsRemaining, e := strconv.Atoi(match[5])
if e != nil {
msg.secondsRemaining = uint64(minsRemaining * 60 + secsRemaining)
}
}
msg.bytesTransferred =
uint64(float32(msg.totalBytes) * (progress / 100))
}
} else if strings.HasPrefix(s, "[ffmpeg]") && strings.Index(s, ":") != -1 {
msg.state = kExtractingAudio
}
return &msg
}
type StdoutProcessor struct {
readAhead string
in *io.ReadCloser
data []byte
savedError os.Error
}
func NewStdoutProcessor(in *io.ReadCloser) *StdoutProcessor {
return &StdoutProcessor{"", in, make([]byte, 256), nil}
}
func HasNewLineCR(s string) bool {
return strings.IndexAny(s, "\r\n") != -1
}
func ExtractFirstToken(s string) (string, string) {
n := strings.IndexAny(s, "\r\n")
return s[0:n], s[n + 1:]
}
func (p *StdoutProcessor) ParseOneStatusMessage() (*StatusMessage, os.Error) {
if HasNewLineCR(p.readAhead) {
chunk, rest := ExtractFirstToken(p.readAhead)
p.readAhead = rest
return NewStatusMessage(chunk), nil
} else {
// Need to read more data from in. But first check savedError.
if p.savedError != nil {
return nil, p.savedError
}
n, e := (*(p.in)).Read(p.data)
if n > 0 {
p.readAhead = p.readAhead + string(p.data[0:n])
}
if HasNewLineCR(p.readAhead) {
p.savedError = e
chunk, rest := ExtractFirstToken(p.readAhead)
p.readAhead = rest
return NewStatusMessage(chunk), nil
}
if e != nil {
return nil, e
}
return nil, os.NewError("No more data")
}
return nil, os.NewError("should not get here")
}
func ProcessRequest(request DownloadRequest) {
var tempFile string = MakeTempFileName(request.url, ".flv")
var tempFileSansExtension string
{
pathDotIndex := strings.LastIndex(tempFile, ".")
tempFileSansExtension = tempFile[0:pathDotIndex]
}
var cmd *exec.Cmd = MakeCommand(&request, tempFile)
if cmd == nil {
request.WriteError("forming the download command", nil)
return
}
var proc *StdoutProcessor
if pipe, e := cmd.StdoutPipe(); e != nil {
request.WriteError("setting youtube-dl pipe", e)
return
} else {
proc = NewStdoutProcessor(&pipe)
}
if e := cmd.Start(); e != nil {
request.WriteError("running youtube-dl", e)
return
}
var state DownloadState = kStarting
var data map[string] interface{}
var e os.Error
for {
status, e := proc.ParseOneStatusMessage()
if e != nil {
if state < kExtractingAudio {
request.WriteError("downloading the video", e)
return
} else {
break
}
} else if status == nil {
continue
}
if status.state != kDownloadingVideo && (status.state == 0 || status.state == state) {
continue
}
fmt.Printf("Proc SM: %v\n", status)
if status.state >= kDownloadingVideo && state != status.state {
if data, e = ReadJson(tempFile + ".info.json"); e != nil {
request.WriteError("reading video JSON", e)
return
}
status.title = data["title"].(string)
}
state = status.state
status.requestId = request.id
request.statusChannel <- *status
}
var finalFileBaseName = data["title"].(string) + "." + *outputFormat
var finalFile = request.basePath + "/" + finalFileBaseName
if _, e := CopyFile(finalFile,
tempFileSansExtension + "." + *outputFormat); e != nil {
request.WriteError("tagging", e)
return
}
request.statusChannel <- StatusMessage{request.id, kTagging, "", "", 0, 0, 0, nil, ""}
var c = exec.Command("lltag", "--yes", "-G", "--rename=%a/%t", finalFileBaseName)
c.Dir = request.basePath
out, ioError := c.StdoutPipe()
if ioError != nil {
request.WriteError("starting the tagger", ioError)
return
}
var in io.WriteCloser
in, ioError = c.StdinPipe()
if ioError != nil {
request.WriteError("starting the tagger", ioError)
return
}
var link string
if e = c.Start(); e != nil {
request.WriteError("tagging", e)
return
} else {
in.Write([]byte(strings.Repeat("y\n", 20)))
outData, e := ioutil.ReadAll(out)
if e != nil {
request.WriteError("reading the tagger output", e)
return
}
match := regexp.MustCompile("New filename is '([^\n]*)'\n").FindStringSubmatch(string(outData))
fmt.Printf("out data: %s\n\nmatch: %v\n", string(outData), match)
if match == nil {
request.WriteError("finding the output file", os.NewError("fail"))
return
}
link = "/files/" + match[1]
}
fmt.Printf("Link: %s\n", link)
request.statusChannel <- StatusMessage{request.id, kComplete, link, "", 0, 0, 0, nil, ""}
}