Skip to content

Commit e810e44

Browse files
authored
Merge pull request #1 from pofallon/develop
Implement lookup with streams
2 parents 7da7a6c + 4d3e57e commit e810e44

5 files changed

Lines changed: 810 additions & 756 deletions

File tree

commands/lookup.js

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,43 @@
1-
const readline = require('readline')
1+
const through2 = require('through2')
2+
const ps = require('promise-streams')
3+
const split = require('split2')
4+
const parallel = require('parallel-transform')
5+
const from = require('from2-array')
26
const JSONStream = require('JSONStream')
3-
const PQueue = require('p-queue')
47
const CredentialManager = require('../lib/credential-manager')
58
const Twitter = require('../lib/twitter')
6-
const BatchEmitter = require('../lib/batch-emitter')
9+
const batch = require('../lib/batch-stream')
710

8-
const lookup = {
9-
async users (name, users, streams) {
10-
await doLookup(name, '1.1/users/lookup.json?screen_name=', users, streams)
11-
},
12-
async statuses (name, ids, streams) {
13-
await doLookup(name, '1.1/statuses/lookup.json?id=', ids, streams)
14-
}
15-
}
16-
17-
const doLookup = async function (name, api, items, streams = process) {
11+
const doLookup = async function (api, name, items, inout = process) {
1812
let creds = new CredentialManager(name)
1913
let [key, secret] = await creds.getKeyAndSecret('consumer')
2014
let twitter = new Twitter(key, secret)
2115
let [token, tokenSecret] = await creds.getKeyAndSecret('account')
2216
twitter.setToken(token, tokenSecret)
23-
let queue = new PQueue({concurrency: 2})
24-
let jsonStream = JSONStream.stringify()
25-
jsonStream.pipe(streams.stdout)
26-
await new Promise((resolve, reject) => {
27-
let batch = new BatchEmitter(100)
28-
batch.on('data', (data) => {
29-
queue.add(() => twitter.get(`${api}${data.join(',')}`))
30-
.then((results) => {
31-
results.forEach((result) => { jsonStream.write(result) })
32-
}).catch(reject)
33-
})
34-
batch.on('end', () => {
35-
queue.onIdle().then(() => {
36-
jsonStream.end()
37-
resolve()
38-
})
39-
})
40-
if (items) {
41-
items.split(',').forEach((item) => { batch.add(item) })
42-
batch.done()
43-
} else {
44-
readline.createInterface({input: streams.stdin})
45-
.on('line', (line) => { batch.add(line) })
46-
.on('close', () => { batch.done() })
47-
}
48-
})
17+
return ps.pipeline(
18+
items ? from.obj(items.split(',')) : inout.stdin.pipe(split()),
19+
batch(100),
20+
parallel(2, function (data, next) {
21+
twitter.get(`${api}${data.join(',')}`)
22+
.then((results) => next(null, results))
23+
.catch(next)
24+
}),
25+
through2.obj(function (chunk, enc, next) {
26+
chunk.forEach((c) => this.push(c))
27+
next()
28+
}),
29+
JSONStream.stringify(),
30+
inout.stdout
31+
)
32+
}
33+
34+
const lookup = {
35+
async users (...args) {
36+
await doLookup('1.1/users/lookup.json?screen_name=', ...args)
37+
},
38+
async statuses (...args) {
39+
await doLookup('1.1/statuses/lookup.json?id=', ...args)
40+
}
4941
}
5042

5143
module.exports = lookup

lib/batch-emitter.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/batch-stream.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const through2 = require('through2')
2+
3+
const batchStream = (batchSize = 10) => {
4+
let batch = []
5+
return through2.obj(
6+
(chunk, enc, next) => {
7+
batch.push(chunk)
8+
if (batch.length === batchSize) {
9+
let data = batch
10+
batch = []
11+
next(null, data)
12+
} else {
13+
next()
14+
}
15+
},
16+
(next) => {
17+
if (batch.length > 0) {
18+
next(null, batch)
19+
} else {
20+
next()
21+
}
22+
}
23+
)
24+
}
25+
26+
module.exports = batchStream

0 commit comments

Comments
 (0)