-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
105 lines (94 loc) · 2.36 KB
/
cli.js
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
#!/usr/bin/env node
'use strict'
var Transform = require('readable-stream/transform')
var concat = require('concat-stream')
var split = require('split')
var ndjson = require('ndjson')
var fs = require('fs')
var pump = require('pump')
var prompt = require('inquirer').prompt
var createApiStream = require('./lib/api-stream')
var argv = require('minimist')(process.argv.slice(2), {
alias: {
o: 'output-file',
n: 'num-results',
s: 'soundcloud-client-id'
}
})
var inputFile = argv._[0]
var outputFile = argv.o
var numResults = argv.n || 5
var SC_CLIENT_ID = argv.scid || process.env.SC_CLIENT_ID
if (!inputFile) {
console.error('Must provide input file. STDIN is reserved for answering questions')
process.exit(1)
}
if (!outputFile) {
console.error('Must provide output file. STDOUT is reserved for answering questions')
process.exit(1)
}
if (!SC_CLIENT_ID) {
console.error('Must provide Soundcloud Client ID')
process.exit(2)
}
var pendingQueries = []
pump(
fs.createReadStream(inputFile),
split(),
Transform({
objectMode: true,
transform (query, enc, cb) {
pendingQueries.push(query.toString('utf8'))
this.push({q: query.toString('utf8')})
cb()
}
}),
createApiStream('https://api.soundcloud.com/tracks?client_id=' + SC_CLIENT_ID + '{&q}'),
concatResponse(pendingQueries),
listOptions(),
ndjson.serialize(),
fs.createWriteStream(outputFile)
)
function concatResponse (queryQueue) {
return Transform({
objectMode: true,
transform (req, enc, cb) {
var self = this
req.pipe(concat(function (body) {
var currentQuery = pendingQueries.shift()
try {
var json = JSON.parse(body)
json.query = currentQuery
self.push(json)
cb()
} catch (ex) {
cb(ex)
}
}))
.on('error', cb)
}
})
}
function listOptions () {
return Transform({
objectMode: true,
highWaterMark: 1,
transform (results, enc, cb) {
var self = this
prompt([{
type: 'list',
name: 'q',
message: results.query,
choices: results.slice(0, numResults).map(function (value) {
return {
name: value.title,
value
}
}).concat({name: 'Skip', value: null})
}], function (answers) {
if (answers.q) self.push(answers.q)
cb()
})
}
})
}