Skip to content

Commit e72f97b

Browse files
authored
Merge pull request sagarkarira#104 from sagarkarira/feat-us-states
Feat us states
2 parents c308b15 + 05b670a commit e72f97b

File tree

10 files changed

+152
-206
lines changed

10 files changed

+152
-206
lines changed

app.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const { getCompleteTable, getGraph } = require('./lib/corona');
1212
const { lookupCountry, htmlTemplate, footer } = require('./lib/helpers');
1313
const { getLiveUpdates } = require('./lib/reddit.js');
1414
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
15-
const { helpContent, countryNotFound } = require('./lib/constants');
15+
const { getUsaStats } = require('./lib/country/us.js');
16+
const { helpContent, countryNotFound, stateCountryNotFound } = require('./lib/constants');
1617

1718
const app = express();
1819
const port = process.env.PORT || 3001;
@@ -125,6 +126,26 @@ app.get('/help', (req, res) => {
125126
return res.send(chalk.green(helpContent));
126127
});
127128

129+
app.get('/states/:country', (req, res) => {
130+
const { country } = req.params;
131+
const isCurl = req.isCurl;
132+
const format = req.query.format ? req.query.format : '';
133+
const minimal = req.query.minimal === 'true';
134+
const top = req.query.top ? Number(req.query.top) : 1000;
135+
136+
const lookupObj = lookupCountry(country);
137+
138+
if (!lookupObj) {
139+
return res.status(404).send(stateCountryNotFound(isCurl));
140+
}
141+
if (lookupObj.iso2 === 'US') {
142+
return getUsaStats({ isCurl, minimal, top, format})
143+
.then(result => {
144+
return res.send(result);
145+
}).catch(error => errorHandler(error, req, res));
146+
}
147+
});
148+
128149

129150
app.get('/:country', (req, res) => {
130151
const { country } = req.params;

bin/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { getCompleteTable, getGraph } = require('../lib/corona');
77
const { getCountryTable } = require('../lib/byCountry');
88
const { getWorldoMetersTable } = require('../lib/worldoMeters');
99
const { lookupCountry } = require('../lib/helpers');
10+
const { getUsaStats } = require('../lib/country/us');
1011

1112
const { argv } = yargs
1213
.command('$0 [country]', 'Tool to track COVID-19 statistics from terminal', yargs =>
@@ -68,12 +69,27 @@ const { argv } = yargs
6869
describe: 'Get graph',
6970
type: 'boolean',
7071
default: false,
72+
},
73+
st: {
74+
alias: 'states',
75+
describe: 'Get state level data of country ',
76+
type: 'string',
7177
}
7278
})
7379
.strict()
7480
.help('help');
7581

7682
argv.countryCode = argv.country;
83+
if (argv.states === 'US') {
84+
getUsaStats(argv).then(result => {
85+
console.log(result);
86+
process.exit(1);
87+
}).catch(error => {
88+
console.error(error);
89+
process.exit(0);
90+
});
91+
}
92+
7793
if (argv.source === 1) {
7894
(
7995
argv.country === 'ALL'

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.9.1
4+
5+
* Added US states api `corona --states=US`
6+
37
## Version 0.9.0
48

59
* Changed default source to worldoMeters. i.e source 2 is now default

lib/api.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
const NodeCache = require('node-cache');
99
const axios = require('axios');
10-
const { countryNameMap } = require('./constants');
1110

1211
const myCache = new NodeCache({ stdTTL: 100, checkperiod: 600 });
1312

@@ -75,8 +74,8 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
7574
});
7675

7776
result.data.forEach(obj => {
78-
obj.countryCode = countryNameMap[obj.country];
7977
obj.confirmed = obj.cases;
78+
obj.countryCode = obj.countryInfo.iso2 || '';
8079
});
8180
worldStats.casesPerOneMillion = (worldStats.cases / 7794).toFixed(2);
8281
worldStats.confirmed = worldStats.cases;
@@ -89,3 +88,18 @@ exports.getWorldoMetersData = async (countryCode = 'ALL') => {
8988
myCache.set(key, returnObj, 60 * 15);
9089
return returnObj;
9190
};
91+
92+
exports.usaStats = async () => {
93+
const key = 'usaStats';
94+
const cache = myCache.get(key);
95+
96+
if (cache) {
97+
console.log('cache', key);
98+
return cache;
99+
}
100+
const result = await axios('https://corona.lmao.ninja/states');
101+
if (!result || !result.data) {
102+
throw new Error('usa stats API failure');
103+
}
104+
return result;
105+
};

lib/constants.js

Lines changed: 25 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -1,201 +1,4 @@
11
const { htmlTemplate, footer } = require('./helpers');
2-
exports.countryNameMap = {
3-
China: 'CN',
4-
UK: 'GB',
5-
Martinique: 'MQ',
6-
Liechtenstein: 'LI',
7-
'Réunion': 'RE',
8-
Ukraine: 'UA',
9-
Honduras: 'HN',
10-
Afghanistan: 'AF',
11-
Bangladesh: 'BD',
12-
Macao: 'MO',
13-
Bolivia: 'BO',
14-
Cuba: 'CU',
15-
Netherlands: 'NL',
16-
Jamaica: 'JM',
17-
'French Guiana': 'GF',
18-
DRC: 'CD',
19-
Cameroon: 'CM',
20-
Maldives: 'MV',
21-
Montenegro: 'ME',
22-
Paraguay: 'PY',
23-
Nigeria: 'NG',
24-
Guam: 'GU',
25-
'French Polynesia': 'PF',
26-
Austria: 'AT',
27-
Ghana: 'GH',
28-
Rwanda: 'RW',
29-
Monaco: 'MC',
30-
Gibraltar: 'GI',
31-
Guatemala: 'GT',
32-
'Ivory Coast': 'CI',
33-
Ethiopia: 'ET',
34-
Togo: 'TG',
35-
'Trinidad and Tobago': 'TT',
36-
Kenya: 'KE',
37-
Belgium: 'BE',
38-
Mauritius: 'MU',
39-
'Equatorial Guinea': 'GQ',
40-
Kyrgyzstan: 'KG',
41-
Mongolia: 'MN',
42-
'Puerto Rico': 'PR',
43-
Seychelles: 'SC',
44-
Tanzania: 'TZ',
45-
Guyana: 'GY',
46-
Aruba: 'AW',
47-
Barbados: 'BB',
48-
Norway: 'NO',
49-
Mayotte: 'YT',
50-
'Cayman Islands': 'KY',
51-
'Curaçao': 'CW',
52-
Bahamas: 'BS',
53-
Congo: 'CD',
54-
Gabon: 'GA',
55-
Namibia: 'NA',
56-
'St. Barth': 'BL',
57-
'Saint Martin': 'MF',
58-
'U.S. Virgin Islands': 'VI',
59-
Sweden: 'SE',
60-
Sudan: 'SD',
61-
Benin: 'BJ',
62-
Bermuda: 'BM',
63-
Bhutan: 'BT',
64-
CAR: 'CF',
65-
Greenland: 'GL',
66-
Haiti: 'HT',
67-
Liberia: 'LR',
68-
Mauritania: 'MR',
69-
'New Caledonia': 'NC',
70-
Denmark: 'DK',
71-
'Saint Lucia': 'LC',
72-
Zambia: 'ZM',
73-
Nepal: 'NP',
74-
Angola: 'AO',
75-
'Antigua and Barbuda': 'AG',
76-
'Cabo Verde': 'CV',
77-
Chad: 'TD',
78-
Djibouti: 'DJ',
79-
'El Salvador': 'SV',
80-
Fiji: 'FJ',
81-
Japan: 'JP',
82-
Gambia: 'GM',
83-
Guinea: 'GN',
84-
'Vatican City': 'VA',
85-
'Isle of Man': 'IM',
86-
Montserrat: 'MS',
87-
Nicaragua: 'NI',
88-
Niger: 'NE',
89-
'St. Vincent Grenadines': 'VC',
90-
'Sint Maarten': 'SX',
91-
Somalia: 'SO',
92-
Malaysia: 'MY',
93-
Suriname: 'SR',
94-
Eswatini: 'SZ',
95-
Australia: 'AU',
96-
Italy: 'IT',
97-
Canada: 'CA',
98-
Portugal: 'PT',
99-
Czechia: 'CZ',
100-
Israel: 'IL',
101-
Brazil: 'BR',
102-
Luxembourg: 'LU',
103-
Ireland: 'IE',
104-
Greece: 'GR',
105-
Qatar: 'QA',
106-
Pakistan: 'PK',
107-
Iran: 'IR',
108-
Finland: 'FI',
109-
Poland: 'PL',
110-
Turkey: 'TR',
111-
Singapore: 'SG',
112-
Chile: 'CL',
113-
Iceland: 'IS',
114-
Thailand: 'TH',
115-
Slovenia: 'SI',
116-
Indonesia: 'ID',
117-
Bahrain: 'BH',
118-
Spain: 'ES',
119-
Romania: 'RO',
120-
'Saudi Arabia': 'SA',
121-
Estonia: 'EE',
122-
Ecuador: 'EC',
123-
Egypt: 'EG',
124-
Peru: 'PE',
125-
Philippines: 'PH',
126-
'Hong Kong': 'HK',
127-
India: 'IN',
128-
Russia: 'RU',
129-
Germany: 'DE',
130-
Iraq: 'IQ',
131-
Mexico: 'MX',
132-
Lebanon: 'LB',
133-
'South Africa': 'ZA',
134-
Kuwait: 'KW',
135-
'San Marino': 'SM',
136-
UAE: 'AE',
137-
Panama: 'PA',
138-
Armenia: 'AM',
139-
Taiwan: 'TW',
140-
USA: 'US',
141-
Argentina: 'AR',
142-
Colombia: 'CO',
143-
Slovakia: 'SK',
144-
Serbia: 'RS',
145-
Croatia: 'HR',
146-
Bulgaria: 'BG',
147-
Uruguay: 'UY',
148-
Algeria: 'DZ',
149-
'Costa Rica': 'CR',
150-
Latvia: 'LV',
151-
France: 'FR',
152-
Hungary: 'HU',
153-
Vietnam: 'VN',
154-
'Faeroe Islands': 'FO',
155-
Andorra: 'AD',
156-
Brunei: 'BN',
157-
Belarus: 'BY',
158-
Jordan: 'JO',
159-
Cyprus: 'CY',
160-
'Sri Lanka': 'LK',
161-
Albania: 'AL',
162-
'S. Korea': 'KR',
163-
'Bosnia and Herzegovina': 'BA',
164-
Morocco: 'MA',
165-
Malta: 'MT',
166-
'North Macedonia': 'MK',
167-
Moldova: 'MD',
168-
Kazakhstan: 'KZ',
169-
Lithuania: 'LT',
170-
Oman: 'OM',
171-
Cambodia: 'KH',
172-
Palestine: 'PS',
173-
Switzerland: 'CH',
174-
Guadeloupe: 'GP',
175-
Azerbaijan: 'AZ',
176-
Georgia: 'GE',
177-
Venezuela: 'VE',
178-
Tunisia: 'TN',
179-
'New Zealand': 'NZ',
180-
Senegal: 'SN',
181-
'Dominican Republic': 'DO',
182-
'Burkina Faso': 'BF',
183-
Uzbekistan: 'UZ',
184-
'Madagascar': 'MG',
185-
'Uganda': 'UG',
186-
'Zimbabwe': 'ZW',
187-
'Dominica': 'DM',
188-
'Laos': 'LA',
189-
'Myanmar': 'MM',
190-
'Belize': 'BZ',
191-
'Eritrea': 'ER',
192-
'Grenada': 'GD',
193-
'Mozambique': 'MZ',
194-
'Papua New Guinea': 'PG',
195-
'Syria': 'SY',
196-
'Timor-Leste': 'TL',
197-
'Turks and Caicos': 'TC'
198-
};
1992

2003
exports.helpContent = `
2014
@@ -239,6 +42,19 @@ https://corona-stats.online/UK
23942
24043
---------------------------------------------------------------------------------
24144
45+
# State wise api (Only for US as of now)
46+
47+
## Format:
48+
https://corona-stats.online/states/[countryCode]
49+
https://corona-stats.online/states/[countryName]
50+
51+
## Example: From source 1
52+
https://corona-stats.online/us
53+
https://corona-stats.online/USA?format=json
54+
https://corona-stats.online/USA?minimal=true
55+
56+
---------------------------------------------------------------------------------
57+
24258
# Minimal Mode - remove the borders and padding from table
24359
24460
## Example:
@@ -296,4 +112,16 @@ exports.countryNotFound = (isCurl) => {
296112
${footer(new Date)}
297113
`;
298114
return isCurl ? body : htmlTemplate(body);
115+
};
116+
117+
exports.stateCountryNotFound = (isCurl) => {
118+
const body = `
119+
State wise api is only available for:
120+
- US
121+
Try:
122+
/US or /USA
123+
124+
${footer(new Date)}
125+
`;
126+
return isCurl ? body : htmlTemplate(body);
299127
};

lib/corona.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ exports.getGraph = async ({ countryCode = 'ALL', isCurl = true}) => {
7979

8080
const confirmedGraph = asciichart.plot(confirmedByDay, graphConfig);
8181
const body = chalk.greenBright(confirmedGraph)
82-
+ chalk.cyanBright('\n\n' +padding + '22 Feb' + graphLength + '22 Mar') + '\n';
82+
+ chalk.cyanBright('\n\n' +padding + '22 Feb' + graphLength + 'Today') + '\n';
8383

8484
if (!isCurl) {
8585
return htmlTemplate(body);

0 commit comments

Comments
 (0)