-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (128 loc) · 3 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const querystring = require('querystring');
const ora = require('ora');
const inquirer = require('inquirer');
const opn = require('opn');
const request = require('request');
const chalk = require('chalk');
const meow = require('meow');
const homepage = meow().pkg.homepage;
const hitokotoApi = 'https://api.imjad.cn/hitokoto/?';
const config = {
cat: '',
charset: 'utf-8',
length: 50,
encode: 'json',
fun: '',
source: ''
};
let getHitokoto = url => {
const spinner = ora('☕ Take a second .... ').start();
return request(url, (error, response, body) => {
spinner.stop();
if (!error && response.statusCode == 200) {
const data = JSON.parse(body);
console.log(chalk.green('✔ ') + chalk.white(`${data.hitokoto} ———— 《${data.source}》`));
} else {
console.log(error);
}
});
};
let questions = [
{
type: 'list',
name: 'cat',
message: 'category:',
choices: [
'Anime',
'Comic',
'Game',
'Novel',
'Original',
'Internet',
'Other'
]
},
{
type: 'list',
name: 'charset',
message: 'charset:',
choices: ['gbk', 'utf-8']
},
{
type: 'input',
name: 'length',
message: 'length:',
validate: value => {
const validateNumber = n =>
!isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n && Number(n) > 0;
if (validateNumber(value)) return true;
return 'Please enter a valid number greater than 0';
}
},
// {
// type: 'list',
// name: 'encode',
// message: 'encode:',
// choices: ['json', 'js', 'jsc', 'Plain Text']
// },
{
type: 'list',
name: 'source',
message: 'source:',
choices: ['Random', 'System included', 'My hitokoto']
}
];
let runHitokoto = options => {
if (options.s) {
return opn(homepage);
} else if (options.o) {
inquirer.prompt(questions).then(answers => {
switch (answers.cat) {
case 'Anime':
answers.cat = 'a';
break;
case 'Comic':
answers.cat = 'b';
break;
case 'Game':
answers.cat = 'c';
break;
case 'Novel':
answers.cat = 'd';
break;
case 'Original':
answers.cat = 'e';
break;
case 'Internet':
answers.cat = 'f';
break;
case 'Other':
answers.cat = 'g';
break;
default:
answers.cat = '';
break;
}
if (answers.encode == 'Plain Text') {
answers.encode = '';
}
switch (answers.source) {
case 'System included':
answers.source = '0';
break;
case 'My hitokoto':
answers.source = '1';
break;
default:
answers.source = '';
break;
}
return getHitokoto(
hitokotoApi + querystring.stringify(Object.assign({}, config, answers))
);
});
} else {
return getHitokoto(hitokotoApi + querystring.stringify(config));
}
};
module.exports = runHitokoto;