|
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') |
2 | 6 | const JSONStream = require('JSONStream') |
3 | | -const PQueue = require('p-queue') |
4 | 7 | const CredentialManager = require('../lib/credential-manager') |
5 | 8 | const Twitter = require('../lib/twitter') |
6 | | -const BatchEmitter = require('../lib/batch-emitter') |
| 9 | +const batch = require('../lib/batch-stream') |
7 | 10 |
|
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) { |
18 | 12 | let creds = new CredentialManager(name) |
19 | 13 | let [key, secret] = await creds.getKeyAndSecret('consumer') |
20 | 14 | let twitter = new Twitter(key, secret) |
21 | 15 | let [token, tokenSecret] = await creds.getKeyAndSecret('account') |
22 | 16 | 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 | + } |
49 | 41 | } |
50 | 42 |
|
51 | 43 | module.exports = lookup |
0 commit comments