-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconjugate.js
executable file
·69 lines (58 loc) · 1.51 KB
/
conjugate.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
#!/usr/bin/env node
'use strict';
const program = require('commander');
const pkg = require('./package.json');
const osmosis = require('osmosis');
const clear = require('clear');
const Table = require('cli-table');
const URL = 'https://pt.bab.la/verbo/ingles';
program.version(pkg.version, '-v, --version');
program
.arguments('<verb>')
.description('Write the verb that you want find')
.action((verb) => {
search(verb);
});
function search(verb = '') {
const results = [];
osmosis.get(`${URL}/${verb}`)
.find('div.quick-results div.quick-result-entry:nth-child(1n+1n)')
.set({
'type': 'div.quick-result-option',
'overview': 'div.quick-result-overview > ul li'
})
.data(result => {
if (result.hasOwnProperty('type')) {
results.push(result);
}
})
.done(() => {
showResults(results);
})
.error(err => {
if (err) {
console.error(`The verb "${verb}" is not found`);
return;
}
});
}
function showResults(verbs = []) {
let data = verbs.filter((x, i) => i < 3); // Didn't get to third item with CSS selector
let getData = (data = [], key) => data.map((obj) => {
if (!obj.hasOwnProperty(key)) {
console.log(`There isn't the keys: ${key}`);
return;
}
return obj[key];
});
if (data.length === 0) {
return;
}
let table = new Table({
head: getData(data, 'type')
});
table.push(getData(data, 'overview'));
clear();
console.log(table.toString());
}
program.parse(process.argv);