-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
310 lines (260 loc) · 8.18 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env node
// The sindresorhus block
import got from 'got';
import ora from 'ora';
import isReachable from 'is-reachable';
import ansiEscapes from 'ansi-escapes';
import terminalLink from 'terminal-link';
import terminalImage from 'terminal-image';
import cFonts from 'cfonts';
import debug from 'debug';
import htmlToPlainText from 'html2plaintext';
import Parser from '@postlight/parser';
// Custom modules
import exit from './lib/exit.js';
import prompts from './lib/prompts.js';
import formatDate from './lib/formatDate.js';
import retrieveArticlesFromNewsSite from './lib/noticieros/index.js';
const logger = debug('noticias-pr');
/* istanbul ignore next */
function terminalLinkFallback(text, url) {
return `${text} ${url}`;
}
/* istanbul ignore next */
function printArticle(articleImage, article) {
console.log(`
\n
${articleImage}
\n
\t\t\tTítulo: ${article.title}
\t\t\tPublicado: ${formatDate(article.date_published)}
\t\t\tLeer en la web: ${terminalLink(article.domain, article.url, {
fallback: terminalLinkFallback,
})}
\n\n
${htmlToPlainText(article.content)}
\n
`);
}
/* istanbul ignore next */
async function checkConnectionToNewsSite(chosenNewsSite) {
const reachableSpinner = ora('Verificando conneción...').start();
const siteIsReachable = await isReachable(chosenNewsSite);
if (siteIsReachable === true) {
reachableSpinner.succeed();
} else {
reachableSpinner.fail(
'La connecíon falló! Favor de verificar la conectividad al internet.'
);
throw new Error('Site unreachable');
}
}
export async function retrieveNewsSiteChoices() {
return [
{
title: `\u001B[9mEl Nuevo Día\u001B[29m (${terminalLink(
'Ver discusión en Github',
'https://github.com/rnegron/noticias-pr-cli/issues/361'
)})`,
value: 'www.elnuevodia.com',
disabled: true,
},
{
title: `\u001B[9mPrimera Hora\u001B[29m (${terminalLink(
'Ver discusión en Github',
'https://github.com/rnegron/noticias-pr-cli/issues/105'
)})`,
value: 'www.primerahora.com',
disabled: true,
},
{ title: 'El Vocero', value: 'www.elvocero.com' },
{ title: 'Noticel', value: 'www.noticel.com' },
{ title: 'Salir', value: 'exit' },
];
}
/* istanbul ignore next */
async function promptForNewsSite(newsSiteChoices) {
const newsSiteChoice = await prompts({
message: 'Escoge un noticiero',
choices: newsSiteChoices,
});
logger('newsSiteChoice: %O', newsSiteChoice);
return newsSiteChoice.value;
}
export async function retrieveArticlesAvailable(chosenNewsSite) {
// return candidate articles given a news site
const articleChoices = [];
const articleLoadingSpinner = ora('Cargando titulares...');
articleLoadingSpinner.start();
try {
const articles = await retrieveArticlesFromNewsSite[chosenNewsSite]();
for (const article of articles) {
logger('Article: %O', article);
articleChoices.push({
title: article.title,
value: article.link,
});
}
// Add back option
articleChoices.push({ title: 'Regresar', value: 'back' });
articleLoadingSpinner.succeed();
return articleChoices;
} catch (err) {
articleLoadingSpinner.fail(err.message);
process.exit(1);
}
}
/* istanbul ignore next */
async function promptForArticle(articleChoices) {
// Prompt the user to select an article given some choices
process.stdout.write(ansiEscapes.clearScreen);
logger('Article choices: %O', articleChoices);
try {
const articleResponse = await prompts({
message: 'Escoge un artículo',
choices: articleChoices,
});
logger('Article choices response: %O', articleResponse);
// Return to news site selection manually if the user chose to
if (articleResponse.value === 'back') {
await cliInitialMenu();
}
return articleResponse;
} catch (err) {
process.exit(1);
}
}
export async function retrieveArticleData(article) {
const prepareArticleSpinner = ora('Cargando artículo...');
// Get article data from Mercury
try {
prepareArticleSpinner.start();
const articleData = await Parser.parse(article);
logger('Article Data: %O', articleData);
prepareArticleSpinner.succeed();
return articleData;
} catch (err) {
prepareArticleSpinner.fail(err.message);
process.exit(1);
}
}
export async function retrieveArticleImage(article) {
// Verifies that the article parsed via Mercury has a lead image, and tries to fetch it
const imageLoadingSpinner = ora('Descargando y preparando imágen...');
try {
logger('Working on image: %s', article.lead_image_url);
imageLoadingSpinner.start();
// Fetch the image using the "got" package
const response = await got(article.lead_image_url).buffer();
// Prepare the image for displaying it in the terminal
const articleImage = await terminalImage.buffer(response);
imageLoadingSpinner.succeed();
return articleImage;
} catch (err) {
imageLoadingSpinner.warn('No se pudo desplegar la imágen! ' + err.message);
return '';
}
}
/* istanbul ignore next */
async function promptAfterArticle() {
const afterArticleResponse = await prompts({
message: 'Opción',
choices: [
{ title: 'Regresar a noticieros', value: '1' },
{ title: 'Regresar a artículos', value: '2' },
{ title: 'Salir', value: '3' },
],
});
return afterArticleResponse.value;
}
/* istanbul ignore next */
async function performChosenAction(chosenAction, articlesAvailable) {
if (chosenAction === '1') {
// Go back to selecting a news site
await cliInitialMenu();
} else if (chosenAction === '2') {
// Go back to selecting an article
process.stdout.write(ansiEscapes.clearScreen);
await cliInitialMenu(articlesAvailable);
} else {
// Exit the CLI
exit();
}
}
/* istanbul ignore next */
async function cliInitialMenu(articlesAvailable = null) {
// Menu for picking a news site. Outputs the article chosen by the user.
// If given an array of article choices, skips the step of choosing the site.
let articleData;
let articleImage;
// If there are no articles available, go through the whole process
if (articlesAvailable === null) {
try {
// Prompt for a news site
const newsSiteChoices = await retrieveNewsSiteChoices();
const chosenNewsSite = await promptForNewsSite(newsSiteChoices);
// Manual exit available
if (chosenNewsSite === 'exit') exit();
// If not testing, check network connectivity to the news site
if (process.env.NODE_ENV !== 'test') {
await checkConnectionToNewsSite(chosenNewsSite);
}
// Obtain available articles from the chosen news site
articlesAvailable = await retrieveArticlesAvailable(chosenNewsSite);
} catch (err) {
process.exit(1);
}
}
// At this point, there are articles available
try {
// Prompt for an article
const chosenArticle = await promptForArticle(articlesAvailable);
// Parse the article data and image (if possible)
articleData = await retrieveArticleData(chosenArticle.value);
articleImage = await retrieveArticleImage(articleData);
} catch (err) {
process.exit(1);
}
// Render the article in the terminal
printArticle(articleImage, articleData);
// After displaying the article, give choices on how to proceed
const chosenAction = await promptAfterArticle();
// Execute the choice
await performChosenAction(chosenAction, articlesAvailable);
}
/* istanbul ignore next */
function entrypoint() {
// Fancy font intro
cFonts.say('noticias|PR', {
font: 'block',
align: 'center',
colors: ['system'],
background: 'transparent',
letterSpacing: 1,
lineHeight: 1,
space: true,
maxLength: '0',
});
// Start at the main menu
cliInitialMenu();
}
/* istanbul ignore if */
if (process.env.NODE_ENV !== 'test') {
// Entrypoint to CLI
entrypoint();
}
export default {
terminalLinkFallback,
printArticle,
checkConnectionToNewsSite,
retrieveNewsSiteChoices,
promptForNewsSite,
retrieveArticlesAvailable,
promptForArticle,
retrieveArticleData,
retrieveArticleImage,
promptAfterArticle,
performChosenAction,
cliInitialMenu,
entrypoint,
};