-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·227 lines (178 loc) · 5.21 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
const fetch = require("node-fetch");
const pjson = require("./package.json");
const Configstore = require('configstore');
const treasure = require('./RA.json');
const list = treasure.list;
const config = new Configstore(pjson.name, {
setLimit: false,
limit: 100,
onlyMatches: false,
showScore: false,
showYear: false
})
let limvalue = 100;
let query = process.argv;
let search = ' ';
if(query.length <= 2) {
search = list[Math.floor(Math.random() * list.length)].split(' ').join('_');
} else {
search = query.slice(2,query.length).join('_');
}
const arg = query;
let Table = require("cli-table3");
const tableHead = ["Title", "Episodes", "Type", "Status"];
if (config.get('showScore') == true) {
tableHead.splice(1, 0, "Score");
}
if (config.get('showYear') == true) {
tableHead.splice(1, 0, "Year");
}
let table = new Table({
head: tableHead,
colWidths: [46, 11],
});
const chalk = require("chalk");
const greenText = "\x1b[32m";
const resetFont = "\x1b[0m";
const cyanText = "\x1b[36m";
if (arg[2] === "--help" || arg[2] === "-h") {
console.log(`
NAME
${cyanText}anime-cli: command line application to fetch anime details${resetFont}
USAGE
${cyanText}anime-cli [ <anime name> | arguments ]
anime-cli [ <anime name> | -help | --h ...]${resetFont}
OPTIONS
<anime name>
The name of the anime whose information you would
like to fetch
${greenText}eg: anime-cli Naruto${resetFont}
help, --h
view this information
${greenText}eg: anime-cli -help${resetFont}
version, --v
view the version number of the anime-cli app
${greenText}eg: anime-cli -version${resetFont}
DESCRIPION
anime-cli is a command line application that can be used to
fetch information regarding the user-queried anime, like
number of episodes, type of anime and it's airing status.
`);
return;
}
if(arg[2] === "--version" || arg[2] === "-v") {
console.log(chalk.cyanBright(`anime-cli\nversion: ${pjson.version}\n`))
return;
}
if(arg.includes('setLimit') && arg[arg.indexOf('setLimit') + 1] === 'true' ) {
config.set('setLimit', true);
if(arg[4] === undefined) {
console.log('Add the limit value pls');
} else {
config.set('limit', parseInt(arg[4]));
}
return;
}
else if(arg.includes('setLimit') && arg.includes('false')) {
config.set('setLimit', false);
return;
}
if(arg.includes('onlyMatches') && arg.includes('true')) {
config.set('onlyMatches', true);
return;
}
else if (arg.includes('onlyMatches') && arg.includes('false')) {
config.set('onlyMatches', false);
return;
}
if (arg.includes('showScore')) {
if(arg[arg.indexOf('showScore') + 1] === 'true' ) {
config.set('showScore', true);
}
else if(arg[arg.indexOf('showScore') + 1] === 'false' ) {
config.set('showScore', false);
}
else {
console.log('Allowed value for showScore: [true, false]');
}
return;
}
if (arg.includes('showYear')) {
if(arg[arg.indexOf('showYear') + 1] === 'true' ) {
config.set('showYear', true);
}
else if(arg[arg.indexOf('showYear') + 1] === 'false' ) {
config.set('showYear', false);
}
else {
console.log('Allowed value for showYear: [true, false]');
}
return;
}
//let search = '';
//if ()
fetch(`https://api.jikan.moe/v3/search/anime?q=${search}`)
.then((response) => response.json())
.then((data) => {
limvalue = config.get('setLimit') ? config.get('limit') : 100;
const bunch = data.results.slice(0,limvalue);
let status = "";
let PTitle = "";
if (arg[2] !== undefined) {
arg[2] = arg[2].toLowerCase();
}
bunch.map((item) => {
if(config.get('onlyMatches') == true) {
if (item.airing === true) {
status = chalk.red("Ongoing");
} else if (item.airing === false) {
status = chalk.cyanBright("Finished");
}
if (item.title.toLowerCase().includes(arg[2])) {
PTitle = chalk.bold.green;
//new row for new features
const row = [
PTitle(item.title),
item.episodes,
chalk.yellow(item.type),
status,
];
if (config.get('showScore') == true) {
row.splice(1, 0, item.score);
}
if (config.get('showYear') == true) {
row.splice(1, 0, item.start_date ? item.start_date.split('-')[0] : '');
}
table.push(row);
}
}
else if(config.get('onlyMatches') == false) {
if (item.title.toLowerCase().includes(arg[2])) {
PTitle = chalk.bold.green;
} else {
PTitle = chalk.bold.white;
}
if (item.airing === true) {
status = chalk.red("Ongoing");
} else if (item.airing === false) {
status = chalk.cyanBright("Finished");
}
const row = [
PTitle(item.title),
item.episodes,
chalk.yellow(item.type),
status,
];
if (config.get('showScore')) {
row.splice(1, 0, item.score);
}
if (config.get('showYear')) {
row.splice(1, 0, item.start_date ? item.start_date.split('-')[0] : '');
}
table.push(row);
}
});
console.log(table.toString());
})
.catch((error) => console.log(error));