-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmeduza.js
executable file
·772 lines (630 loc) · 25 KB
/
meduza.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#!/usr/bin/env node
// TODO: подписка на вечернюю медузу
// TODO: стриминг
// TODO: поиск
// TODO: категории для английской версии
const argv = require('yargs').argv;
const color = require('cli-color');
let restler, moment, cheerio, html2text, wrap, readline;
const gold = function(s) {
return Meduza.settings.color ? color.xterm(215)(s) : s;
};
const gray = function(s) {
return Meduza.settings.color ? color.xterm(245)(s) : s;
};
const dark = function(s) {
return Meduza.settings.color ? color.xterm(237)(s) : s;
};
const underline = function(s) {
return Meduza.settings.color ? color.underline(s) : s;
};
const bold = function(s) {
return Meduza.settings.color ? color.bold.white(s) : s;
};
const italic = function(s) {
return Meduza.settings.color ? color.italic(s) : s;
};
const center = function(s) {
let lines = s.split('\n');
lines = lines.map(function(line) {
const len = line.length;
const paddingLength = parseInt((80 - len) / 2) + 1;
const padding = (new Array(paddingLength)).join(' ');
return padding + line;
});
return lines.join('\n');
};
const urlRegex = new RegExp(new RegExp('(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))(?::\\d{2,5})?(?:/[^ \\f\\n\\r\\t\\v\\u00a0\\u1680\\u180e\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000\\]]*)?', 'g'));
const bracketsRegex = new RegExp('\\[\\d+\\]', 'g');
const replaceUrls = function(html, arr) {
return html.replace(urlRegex, function(link) {
let isSpaned = false;
if (link.search('"><span') > -1) isSpaned = true;
arr.push(link.replace('"><span', ''));
return arr.length + (isSpaned ? '"><span' : '');
});
};
const replaceBrackets = function(html) {
return html.replace(bracketsRegex, function(brackets) {
return gold(brackets);
});
};
const Meduza = {
settings: {},
chronos: {
news: { ru: 'Новости' },
cards: { ru: 'Картотека' },
articles: { ru: 'Истории' },
shapito: { ru: 'Шапито' },
polygon: { ru: 'Полигон' }
},
currency: {
'EUR': '€',
'RUB': '₽',
'USD': '$',
'BTC': 'Ƀ',
'OIL': '▲',
'/': '/',
'up': color.green('↑'),
'down': color.red('↓')
},
spinner: undefined,
i18n: {
['источник']: 'source',
['Установленная версия']: 'Installed version',
['актуальная версия']: 'last version',
['Пожалуйста, обновите программу (npm update -g meduza).']: 'Please, update to the latest version (npm update -g meduza).',
['Это тест, хотите его пройти?']: 'Do you want to pass this test?',
['Тогда приступим!']: 'Well, then go!',
['Как жаль..']: 'How sad..',
['Варианты ответа y/n/yes/no (yes=да, no=нет).']: 'The answers is y/n/yes/no.',
['Ваш вариант?']: 'Your choice?',
['Некорректный ответ']: 'Incorrect answer',
['Правильно!']: 'Right!',
['Неправильно, правильный ответ']: 'Wrong, right answer',
['Ваш результат']: 'Your result',
['Просто ноль. :(']: 'Zero. :(',
['Совсем плохо.']: 'Very bad.',
['Если честно, так себе.']: 'To be honest, so-so.',
['Весьма неплохо!']: 'Not bad!',
['Отлично!']: 'Good!',
['Вы превосходны! :)']: 'You are excellent! :)'
},
translate: function(s) {
return this.settings.locale === 'en' ? this.i18n[s] || `[${s}]` : s;
},
parseArgv: function() {
const settings = {
help: argv.help || argv.h,
version: argv.version || argv.v,
locale: argv.english ? 'en' : 'ru',
chrono: argv.type || argv.t ? argv.type || argv.t : 'news',
show: argv.show || argv.s ? argv.show || argv.s : undefined,
color: !(argv.color === false),
wrap: 80,
number: parseInt(argv.number || argv.n),
exchange: argv.exchange,
sort: argv.sort,
showLogo: !(argv.logo === false)
};
if (isNaN(settings.number)) {
settings.number = 10;
} else if (settings.number < 1) {
settings.number = 10;
} else if (settings.number > 30) {
settings.number = 30;
}
const chronos_arr = Object.keys(this.chronos);
for (let i = 0, l = argv._.length; i < l; i++) {
if (argv._[i] === 'en') {
settings.locale = argv._[i];
} else if (argv._[i].search('meduza.io/') > -1) {
settings.directUrl = argv._[i];
} else if (argv._[i].search(':') > -1) {
settings.show = argv._[i];
} else if (chronos_arr.indexOf(argv._[i]) > -1) {
settings.chrono = argv._[i];
}
}
this.settings = settings;
},
checkUpdates: function() {
const _this = this;
restler
.get('https://raw.githubusercontent.com/ozio/meduza/master/package.json')
.on('complete', function(data) {
const currentVersion = require('./package.json').version;
const lastVersion = JSON.parse(data).version;
_this.spinnerHide();
if (currentVersion !== lastVersion) {
console.log(_this.showLine('', dark));
console.log(color.red(center(`${_this.translate('Установленная версия')}: ${currentVersion}, ${_this.translate('актуальная версия')}: ${lastVersion}.`)));
console.log(color.red(center(_this.translate('Пожалуйста, обновите программу (npm update -g meduza).'))));
console.log(_this.showLine('', dark));
console.log('');
}
});
},
showHelp: function() {
const help = `
Usage: ${underline(gold('meduza'))} [commands/options]
Commands: ${gray('(you can combine every command with each other)')}
meduza \tOutput latest news.
meduza en \tOutput latest news from english version.
meduza <time> \tOutput article by time (i.e. 15:42).
meduza <type> \tChoose articles type (default: news). Only one and only in russian.
meduza <url> \tOutput article by URL.
Options:
-t, --type <type>\tChoose articles type (default: news). Only one and only in russian.
--english \tOutput latest news from english version.
-s, --show <time>\tOutput article by time (i.e. 15:42).
-n, --number <num>\tNumber of news in output (from 1 to 30).
--no-color \tOutput without colors.
--exchange \tShow current exchange rate.
-v, --version \tDisplay version.
-h, --help \tDisplay help information.
--sort <recency>\tSort news by recency (latest or oldest).
--no-logo \tOutput without the Meduza logo.
Categories: \tnews, cards, articles, shapito, polygon.
`;
console.log(help);
return true;
},
showLogo: function() {
const logo = `
██▄
██████▄
▄█░ ▄██░ ▄██░ ▄██ ▀██░ ███░ ███░ ▄██▄ ▄███░
▀███░▀███░▀██░ ███░██░ ██░ ██░ ███░ ███░ ▀████░ ▄███░
███░ ███░ ██░ ███░█░ ██░ ██░ ███░ ███░ ▄▀ ▄█▀███░
███░ ███░ ██░ ███░ ██░ ██░ ███░ ███░ ▄▀ ██░ ███░
███░ ███░ ██░ ███░ ███░ ██░ ███░ ███░ █████░ ██░ ███░
███░ ███░ ███░ ▀████░ ▀████░ ▀███▀███▀ ▀███░ ███▀████░ ${gray('v' + require('./package.json').version)}
`;
console.log(gold(logo));
},
showVersion: function() {
console.log(require('./package.json').version);
return true;
},
showDateLine: function(timestamp) {
let string = `══ ${moment(timestamp, 'X').format('LL')} `;
const len = this.settings.wrap - string.length;
string += (new Array(len + 1)).join('═');
console.log(`\n${string}\n`);
},
showLine: function(separator, color) {
const wrapCharsCount = this.settings.wrap + 1;
let isLengthFloat = false;
let lineLength = (wrapCharsCount - separator.length) / 2;
if (lineLength !== parseInt(lineLength)) {
isLengthFloat = true;
}
lineLength = parseInt(lineLength) + 1;
const line = [
(new Array(lineLength - (isLengthFloat ? 0 : 1))).join('─'),
separator,
(new Array(lineLength)).join('─')
];
return color(line.join(''));
},
showArticleShort: function(doc) {
const time = moment(doc.published_at, 'X').format('H:mm');
const title = wrap(doc.title);
const secondTitle = doc.second_title ? wrap(doc.second_title) : null;
const type = doc.tag ? doc.tag.name : doc.document_type;
const url = `${doc.document_type === 'promo'
? doc.promo_url.replace('http://', '')
: `https://meduza.io/${doc.url}`
}`;
console.log(`${gray(time)} ${gold(type.toUpperCase())}`);
console.log(title);
if (secondTitle) console.log(secondTitle);
console.log(dark(url));
},
showQuote: function(source, urls) {
const quoteWrap = require('wordwrap')(this.settings.wrap - 2);
const wrappedText = quoteWrap(source.quote);
const sourceText = quoteWrap(`${source.name.toUpperCase()} [${replaceUrls(source.url, urls)}]`);
let resText = '';
const textArr = wrappedText.split('\n');
const sourceArr = sourceText.split('\n');
textArr.push(dark((new Array(this.settings.wrap - 1)).join('─')));
textArr.forEach(function(item) {
resText += `${gold('┃ ') + item}\n`;
});
sourceArr.forEach(function(item, idx) {
resText += `${gold('┃ ')}${idx > 0 ? ' ' : gold('→ ')}${replaceBrackets(item)}\n`;
});
return gray(resText.trim());
},
showContext: function(dom, $, urls) {
let context = '<ul>';
dom.each(function(idx, el) {
context += `<li>${replaceUrls($(el).text(), urls)}<br></li>`;
});
context += '</ul>';
return context;
},
showCards: function(doc, dom, urls) {
const article = [];
for (let i = 0; i < doc.chapters_count; i++) {
article.push(this.showLine(` ${(i + 1) < 10 ? '0' + (i + 1) : (i + 1)} `, gold));
article.push(bold(center(wrap(doc.table_of_contents[i]))));
article.push(
replaceBrackets(
wrap(
replaceUrls(
html2text.fromString(
dom.eq(i), { wordwrap: null }
),
urls
)
)
)
);
}
return article.join('\n\n')
},
showLinks: function(arr) {
let links = '';
arr.forEach(function(item, idx) {
links += `[${idx + 1}] – ${underline(item)}\n`;
});
return `${this.showLine('', dark)}\n${gray(links.trim())}`;
},
showRelated: function(dom, arr) {
const res = [];
const head = bold('Читайте также:');
let html = `<ul>${dom.replace('<ul></ul>', '')}</ul>`;
html = replaceBrackets(
html2text.fromString(
replaceUrls(html, arr)
)
);
res.push(head);
res.push(` ${html}`);
return res.join('\n\n');
},
showAuthors: function(dom) {
const lines = [];
lines.push(gold('━━━━━━'));
const authors = dom.find('.Author');
authors.each(function(idx, author) {
const authorName = cheerio(author)
.find('.Author-name')
.text();
const authorInfo = cheerio(author)
.find('.Author-info')
.text();
lines.push(bold(authorName));
lines.push(italic(authorInfo));
});
return lines.join('\n');
},
showQuiz: function(dom) {
const _this = this;
const quizgroups = dom.find('.QuizGroup');
const questions = [];
let currentQuestion = 0;
let rightAnswers = 0;
cheerio(quizgroups).each(function(idx, item) {
const obj = {
question: cheerio(item).find('.QuizGroup-title').text(),
figure: null,
answers: [],
answer: null
};
const figure = cheerio(item).find('.Figure');
if (figure.length) {
obj.figure = {};
const img = cheerio(figure).find('img');
const caption = cheerio(figure).find('.Figure-caption');
if (img.length) {
obj.figure.url = `https://meduza.io${img.attr('src')}`;
obj.figure.caption = caption.text().trim();
}
}
const answers = cheerio(item).find('.QuizGroup-item');
answers.each(function(answerIdx, answerItem) {
const answer = cheerio(answerItem);
obj.answers.push(answer.text());
if (answer.find('.QuizGroup-radio--ok').length) {
obj.answer = `${answerIdx + 1}`;
}
});
questions.push(obj);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function askForQuiz() {
rl.question(`\n${_this.translate('Это тест, хотите его пройти?')} (Y/n) `, function(answer) {
if (answer === '') answer = 'y';
const answers = {
y: true, yes: true, Y: true, YES: true,
n: false, no: false, N: false, NO: false
};
if (answer in answers) {
if (answers[answer]) {
// да
console.log(_this.translate('Тогда приступим!'));
rl.close();
return askQuestion();
} else {
// нет
console.log(`${_this.translate('Как жаль..')}\n`);
}
rl.close();
} else {
// фиг знает что ответил, повторить
console.log(color.red(_this.translate('Варианты ответа y/n/yes/no (yes=да, no=нет).')));
return askForQuiz();
}
});
}
function askQuestion() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if (questions[currentQuestion]) {
const current = questions[currentQuestion];
// если следующий вопрос есть
let askString = `(${currentQuestion + 1}/${questions.length}) ${current.question}`;
askString = '\n' +
bold(wrap(askString)) +
gray(current.figure ? '\n' + underline(current.figure.url) + (current.figure.caption ? '\n' + italic(current.figure.caption) : '') : '') +
'\n';
current.answers.forEach(function(item, idx) {
askString += `${idx + 1}) ${item}\n`;
});
askString += `${_this.translate('Ваш вариант?')} `;
rl.question(askString, function(answer) {
if (parseInt(answer) === answer) {
answer = parseInt(answer);
if (answer > current.answers.length || answer < 1) {
// не подходит, повторить
console.log(color.red(_this.translate('Некорректный ответ')));
rl.close();
return askQuestion();
} else {
// подходит
if (current.answer === answer) {
// угадал
console.log(color.green(_this.translate('Правильно!')));
rightAnswers++;
} else {
// не угадал
console.log(color.red(`${_this.translate('Неправильно, правильный ответ')}: `) + underline(current.answer));
}
rl.close();
currentQuestion++;
return askQuestion();
}
} else {
// не подходит, повторить
console.log(_this.translate('Некорректный ответ'));
rl.close();
return askQuestion();
}
});
} else {
// если следующего вопроса нет и пора уходить
rl.close();
let resString = `\n${_this.translate('Ваш результат')}: ${rightAnswers}/${questions.length}. `;
const resRate = (rightAnswers / questions.length);
if (resRate === 0) {
resString += color.red(_this.translate('Просто ноль. :('));
} else if (resRate <= .20) {
resString += color.red(_this.translate('Совсем плохо.'));
} else if (resRate > .20 && resRate <= .50) {
resString += color.yellow(_this.translate('Если честно, так себе.'));
} else if (resRate > .50 && resRate <= .80) {
resString += color.green(_this.translate('Весьма неплохо!'));
} else if (resRate > .80 && resRate <= .99) {
resString += color.green(_this.translate('Отлично!'));
} else if (resRate === 1) {
resString += gold(_this.translate('Вы превосходны! :)'));
}
console.log(`${resString}\n`);
process.exit(1);
}
}
return askForQuiz();
},
showGallery: function(gallery, urls, compileString) {
const lines = [];
gallery.forEach(function(item) {
const url = `https://meduza.io${item.large_url}`;
const credit = compileString(item.credit);
const text = compileString(item.text);
lines.push(underline(gold(url)));
if (credit) lines.push(gray(credit));
if (text) lines.push(text);
lines.push('');
});
return lines.join('\n');
},
showArticleFull: function(doc) {
const type = doc.tag ? doc.tag.name : doc.document_type;
const title = bold(wrap(doc.title));
const second_title = doc.second_title ? wrap(doc.second_title) : doc.second_title;
const time = moment(doc.published_at, 'X').format('LLL');
const source = doc.source ? doc.source.name : '';
let url = `https://meduza.io/${doc.url}`;
const urls = [];
const $ = cheerio.load(doc.content.body);
const compileString = (function(html) {
return replaceBrackets(
wrap(
replaceUrls(
html2text.fromString(html, { wordwrap: null }), urls
)
)
);
}).bind(this);
const dom = {
lead: $('.Lead'),
body: $('.Body'),
related: $('.Related ul'),
cards: $('.CardChapter-body'),
quiz: $('.Quiz'),
authors: $('.Authors'),
context: $('.Context-item')
};
dom.body.find('.Related').remove(); // "смотреть также" уже вынесено в отдельный блок
dom.body.find('.Figure').remove(); // убрал вообще, потому что картинки в консоли не видны, а подписи ломают текст
dom.body.find('.Embed').remove(); // то же самое и для видео
const article = [];
switch (doc.document_type) {
case 'promo':
url = doc.promo_url;
article.push(gray('→ ') + underline(gold(url)));
break;
case 'card':
article.push(this.showCards(doc, dom.cards, urls));
if (urls.length) article.push(this.showLinks(urls));
break;
case 'feature':
if (dom.quiz.length) {
if (dom.lead.length) article.push(compileString(dom.lead.html()), this.showLine(' ◆ ◆ ◆ ', dark));
if (dom.authors.length) article.push(this.showAuthors(dom.authors));
if (urls.length) article.push(this.showLinks(urls));
}
break;
default:
if (dom.lead.length) article.push(compileString(dom.lead.html()), this.showLine(' ◆ ◆ ◆ ', dark));
if (dom.body.length) article.push(compileString(dom.body.html()));
if (doc.gallery) article.push(this.showGallery(doc.gallery, urls, compileString));
if (dom.authors.length) article.push(this.showAuthors(dom.authors));
if (doc.source && doc.source.quote) article.push(this.showQuote(doc.source, urls));
if (dom.context.length) article.push(' ' + html2text.fromString(this.showContext(dom.context, $, urls), { wordwrap: this.settings.wrap }));
if (dom.related.length) article.push(this.showRelated(dom.related.html(), urls));
if (urls.length) article.push(this.showLinks(urls));
break;
}
console.log(gold(type.toUpperCase()));
console.log(title);
if (second_title) console.log(second_title);
console.log(gray(`${time}${!source || source !== 'Meduza' ? `, ${this.translate('источник')}: ${source}` : ''}`));
console.log('');
console.log(article.join('\n\n').trim());
if (dom.quiz.length) {
console.log(url ? underline(dark(`\n${url}`)) : '');
this.showQuiz(dom.quiz);
} else {
console.log(url ? underline(dark(`\n${url}\n`)) : '');
}
},
spinnerShow: function() {
if (!this.spinner) {
const Spinner = require('cli-spinner').Spinner;
this.spinner = new Spinner('%s');
this.spinner.setSpinnerString('⣾⣽⣻⢿⡿⣟⣯⣷');
this.spinner.setSpinnerDelay(50);
}
this.spinner.start();
},
spinnerHide: function() {
this.spinner.stop(true);
},
getArticleByURL: function(url) {
const _this = this;
const pathnameStart = url.search('meduza.io/') + 'meduza.io/'.length;
const pathname = url.slice(pathnameStart);
this.spinnerShow();
restler.get(`https://meduza.io/api/v3/${pathname}`)
.on('complete', function(data) {
_this.spinnerHide();
_this.showArticleFull(data.root);
});
},
getArticles: function() {
this.spinnerShow();
const _this = this;
restler
.get(`https://meduza.io/api/v3/search?chrono=${this.settings.chrono}&page=0&per_page=${this.settings.number}&locale=${this.settings.locale}`)
.on('complete', function(data) {
_this.spinnerHide();
const documents = data.documents;
let collection = data.collection;
if (_this.settings.sort === 'oldest') {
collection = collection.reverse();
}
let momentBefore;
let momentNow;
if (!_this.settings.show && _this.settings.showLogo) {
_this.showLogo();
}
for (let i = 0; i < _this.settings.number; i++) {
const doc = documents[collection[i]];
if (!doc) return;
if (!_this.settings.show) {
momentNow = moment(doc.published_at, 'X').format('DDD');
if (typeof momentBefore === 'undefined' || momentBefore !== momentNow) {
_this.showDateLine(doc.published_at);
}
momentBefore = momentNow;
_this.showArticleShort(doc);
} else {
const time = moment(doc.published_at, 'X').format('H:mm');
if (time === _this.settings.show) {
_this.spinnerShow();
restler.get(`https://meduza.io/api/v3/${doc.url}`)
.on('complete', function(data) {
_this.spinnerHide();
_this.showArticleFull(data.root);
});
}
}
}
})
},
showExchangeRate: function() {
this.spinnerShow();
const _this = this;
restler
.get('https://meduza.io/api/v3/stock/all')
.on('complete', function(result) {
_this.spinnerHide();
let exchange;
let data;
try {
data = JSON.parse(result);
const strings = [];
strings.push(`${gold(Meduza.currency.EUR)} ${data.eur.current.toFixed(2)}${Meduza.currency[data.eur.state]}`);
strings.push(`${gold(Meduza.currency.USD)} ${data.usd.current.toFixed(2)}${Meduza.currency[data.usd.state]}`);
strings.push(`${gold(Meduza.currency.OIL)} ${data.brent.current.toFixed(2)}${Meduza.currency[data.brent.state]}`);
strings.push(`${gold(Meduza.currency.BTC)} ${data.btc}₽`);
exchange = strings.join(' ');
} catch (e) {
exchange = 'Sorry, something went wrong';
} finally {
console.log(exchange);
}
});
},
init: function() {
this.parseArgv();
if (this.settings.version) return this.showVersion();
if (this.settings.help) return this.showHelp();
wrap = require('wordwrap')(this.settings.wrap);
html2text = require('html-to-text');
cheerio = require('cheerio');
moment = require('moment');
moment.locale(this.settings.locale);
restler = require('restler');
readline = require('readline');
if (this.settings.exchange) return this.showExchangeRate();
if (this.settings.directUrl) {
this.getArticleByURL(this.settings.directUrl)
} else {
this.getArticles();
}
this.checkUpdates();
}
};
Meduza.init();
module.exports = Meduza;