Skip to content

Commit 0176d60

Browse files
authored
Merge pull request sagarkarira#26 from ssiyad/master
Filter top contries using ?top= and --top
2 parents 44ae662 + 666b6a6 commit 0176d60

File tree

7 files changed

+117
-30
lines changed

7 files changed

+117
-30
lines changed

app.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const port = process.env.PORT || 3001;
99
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
1010
const { getCompleteTable } = require('./lib/corona');
1111
const { lookupCountry } = require('./lib/helpers');
12+
const { getLiveUpdates } = require('./lib/reddit.js');
1213

1314

1415
function errorHandler(error, res) {
@@ -26,36 +27,39 @@ app.use((req, res, next) => {
2627
});
2728

2829

29-
+app.get('/updates', (req, res) => {
30-
const format = req.query.format ? req.query.format : '';
31-
if (format.toLowerCase() === 'json') {
32-
return getLiveUpdates(true).then(result => {
33-
return res.json(result);
34-
}).catch(error => errorHandler(error, res));
35-
}
36-
return getLiveUpdates(false).then(result => {
37-
return res.send(result);
38-
}).catch(error => errorHandler(error, res));
39-
});
4030

4131
app.get('/', (req, res) => {
4232
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
4333
const format = req.query.format ? req.query.format : '';
4434
const minimal = req.query.minimal === 'true' ? true : false;
4535
const emojis = req.query.emojis === 'true' ? true : false;
36+
const top = req.query.top ? Number(req.query.top) : 1000;
4637

4738
if (format.toLowerCase() === 'json') {
4839
return getJSONData().then(result => {
4940
return res.json(result);
5041
}).catch(error => errorHandler(error, res));
5142
}
5243

53-
return getCompleteTable({ isCurl, emojis, minimal })
44+
return getCompleteTable({ isCurl, emojis, minimal, top })
5445
.then(result => {
5546
return res.send(result);
5647
}).catch(error => errorHandler(error, res));
5748
});
5849

50+
app.get('/updates', (req, res) => {
51+
const format = req.query.format ? req.query.format : '';
52+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
53+
if (format.toLowerCase() === 'json') {
54+
return getLiveUpdates({ json: true, isCurl }).then(result => {
55+
return res.json(result);
56+
}).catch(error => errorHandler(error, res));
57+
}
58+
return getLiveUpdates({ json: false, isCurl }).then(result => {
59+
return res.send(result);
60+
}).catch(error => errorHandler(error, res));
61+
});
62+
5963
app.get('/:country', (req, res) => {
6064
const { country } = req.params;
6165
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;

bin/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,24 @@ const { argv } = yargs
4444
},
4545
m: {
4646
alias: 'minimal',
47-
describe: 'remove borders and padding from table',
47+
describe: 'Remove borders & padding from table',
4848
type: 'boolean',
4949
default: false,
50+
},
51+
t: {
52+
alias: 'top',
53+
describe: 'Filter table by rank',
54+
type: 'int'
5055
}
5156
})
5257
.strict()
5358
.help('help');
5459

55-
const { emojis, country, minimal } = argv;
60+
61+
const { emojis, country, minimal, top } = argv;
5662
(
5763
country === 'ALL'
58-
? getCompleteTable({ emojis, minimal })
64+
? getCompleteTable({ emojis, minimal, top })
5965
: getCountryTable({ countryCode: country, emojis, minimal })
6066
)
6167
.then(console.log)

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 0.6.0
4+
5+
* Added filter to show top N countries. ``corona --top=20``
6+
37
## Version 0.5.0
48

59
* Added minimal / comapct table command. ``corona --minimal``

lib/corona.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,23 @@ exports.getCompleteTable = async ({
8484
isCurl = true,
8585
emojis = false,
8686
minimal = false,
87+
top = 1000
8788
}) => {
89+
const head = [
90+
'',
91+
'Country',
92+
`Confirmed ${emojis ? ' ✅': ''}`,
93+
`Recovered${emojis ? ' 😀' : ''}`,
94+
`Deaths${emojis ? ' 😞' : ''}`,
95+
`Active${emojis ? ' 😷' : ''}`,
96+
'Mortality %',
97+
'Recovered %',
98+
'1 Day ▲',
99+
'1 Week ▲',
100+
// 'RoG',
101+
...( emojis ? ['🏳'] : [] ),
102+
];
103+
88104
const table = new Table({
89105
head: helpers.getTableHeaders(emojis, 'Country'),
90106
chars: helpers.getTableBorders(minimal),
@@ -110,7 +126,7 @@ exports.getCompleteTable = async ({
110126
]
111127
})
112128
let rank = 1;
113-
countryData.forEach(cd => {
129+
countryData.some(cd => {
114130
const countryEmoji = getEmoji(cd.countryCode);
115131
const values = [
116132
getCountry(`${cd.country} (${cd.countryCode})`),
@@ -126,6 +142,7 @@ exports.getCompleteTable = async ({
126142
...(emojis ? [countryEmoji] : [])
127143
]
128144
table.push({ [rank++]: values })
145+
return rank == top + 1;
129146
});
130147
table.push({
131148
'': [

lib/reddit.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const axios = require('axios');
2+
const LIVE_THREAD_URL = 'https://www.reddit.com/live/14d816ty1ylvo/.json';
3+
const moment = require('moment');
4+
// const terminalLink = require('terminal-link');
5+
const Table = require('cli-table3');
6+
const stripAnsi = require('strip-ansi');
7+
8+
exports.getLiveUpdates = async ({ json = false, isCurl = true }) => {
9+
const result = await axios(`https://www.reddit.com/live/14d816ty1ylvo/.json`);
10+
if (!result && !result.data) {
11+
throw new Error(`Reddit live thread API faliure`);
12+
}
13+
const mainInfo = result.data.data.children
14+
.filter(o => o.data.mobile_embeds.length > 0)
15+
.map(o => {
16+
return {
17+
description: o.data.mobile_embeds[0].description,
18+
url:o.data.mobile_embeds[0].original_url,
19+
timestamp: o.data.created_utc,
20+
};
21+
});
22+
if (json) {
23+
return mainInfo;
24+
}
25+
return formattedForTerminal(mainInfo, isCurl);
26+
};
27+
28+
const formattedForTerminal = (mainInfo, isCurl) => {
29+
let liveUpdates = 'Latest Updates: \n\n';
30+
var table = new Table({
31+
head: ['Time', 'Update'],
32+
wordWrap: true,
33+
colWidths: [20, 120],
34+
});
35+
mainInfo.forEach(obj => {
36+
const prefixTime = moment(moment.unix(obj.timestamp).format()).fromNow();
37+
const sourceLink = prefixTime;
38+
if (obj.description && sourceLink.indexOf('imgur') === -1) {
39+
// liveUpdates += `${obj.description} --[${sourceLink}] \n\n`;
40+
table.push([prefixTime, obj.description ]);
41+
}
42+
});
43+
liveUpdates += 'Source: https://www.reddit.com/live/14d816ty1ylvo';
44+
// return liveUpdates;
45+
return isCurl ? table.toString() : stripAnsi(table.toString());
46+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coronavirus-tracker-cli",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "track conronavirus cases from cli",
55
"repository": {
66
"type": "git",

readme.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ where <country> can be country name or its ISO code.
3232
curl https://corona-stats.online?minimal=true
3333
````
3434

35+
**Only show top N countries**
36+
37+
```sh
38+
curl https://corona-stats.online?top=20
39+
```
40+
41+
**Latest News (Work in Progress)**
42+
43+
```sh
44+
curl https://corona-stats.online/updates
45+
```
46+
3547
## API
3648

3749
Add `?format=json` at the end of any API to get json formatted data.
@@ -52,15 +64,22 @@ npm install coronavirus-tracker-cli -g
5264

5365
**Run command**
5466

55-
````sh
67+
```sh
5668
corona
57-
````
69+
```
5870

5971
**Filter by country**
6072

61-
````sh
73+
```sh
6274
corona italy
63-
````
75+
```
76+
77+
**Top N countries**
78+
79+
```sh
80+
corona --top=10
81+
```
82+
6483
**With emojis**
6584

6685
````sh
@@ -79,15 +98,6 @@ corona --minimal
7998
corona --color=false
8099
````
81100

82-
**Top 10** (Working on native command)
83-
84-
Note: This command will cause colored output to be discarded.
85-
86-
````sh
87-
# Grep the rank of 10 and the 23 lines preceding it
88-
corona | grep -B 23 ' 10 '
89-
````
90-
91101
### ToDos
92102

93103
* ~~Filter by country to get cases by local states.~~

0 commit comments

Comments
 (0)