-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
69 lines (63 loc) · 1.55 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
/**
* TESS search script
*
* Usage:
*
* > node cli.js TERM [--loglevel OPTIONAL_LOG_LEVEL]
*
* e.g.
*
* > node cli.js infowars
*
*/
// jshint node:true
'use strict';
const log = require('loglevel');
const search = require('./search');
const args = require('yargs')
.option('log', {
alias: 'l',
default: 'info',
describe: 'Log level (error, info, debug)',
type: 'string'
})
.option('images', {
alias: 'i',
default: true,
describe: 'Whether to retrieve images and include their filenames in output',
type: 'boolean'
})
.option('term', {
alias: 't',
describe: 'TESS free-form search term -- e.g. `"Infowars"` or `030926[dc]`.',
type: 'string'
})
.demandOption(['term'])
.argv;
const searchTerm = args.term;
const logLevel = args.log;
log.setLevel(logLevel);
function progressHandler (event) {
// Log out progress (with debug info if debug loglevel is enabled)
let message = event.message;
if (event.fraction !== undefined) {
message += ': ' + Math.round((event.fraction * 1000) / 10) + '% complete';
}
log.info(message);
if (event.details) {
log.debug(event.details);
}
}
search(searchTerm, args.images, progressHandler)
.then(function (results) {
if (logLevel !== 'debug' && logLevel !== 'trace') {
results.forEach(function (result) {
delete result._html; // debug data, we don't need
});
}
console.log(results);
}).catch(function (error) {
log.error('ERROR: ' + error.message);
log.error('ERROR DETAILS:');
log.error(error.details);
});