-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·193 lines (167 loc) · 5.66 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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const got = require('got');
const boxen = require('boxen');
const chalk = require('chalk');
const Ora = require('ora');
const firstRun = require('first-run');
const Conf = require('conf');
const prompts = require('prompts');
const ervy = require('ervy');
const getCoords = require('city-to-coords');
const spinner = new Ora();
const config = new Conf();
const {bar} = ervy;
// Meow configuration
const cli = meow(`
Usage
$ airly <options>
Options
--city, -c Search using city name
--installation, -i Search using specific sensor id
--reset, -r Reset API key
Examples
$ airly --city Krakow
$ airly --installation 204
$ airly -r
`, {
flags: {
installation: {
type: 'string',
alias: 'i'
},
city: {
type: 'string',
alias: 'c'
},
reset: {
type: 'boolean',
alias: 'r',
default: 'false'
}
}
});
// Search by installation id
const startId = async () => {
try {
spinner.start('Fetching data...');
// Fetch pollution data from airly api
const pollutions = await got(`https://airapi.airly.eu/v2/measurements/installation?installationId=${cli.flags.installation}`, {
headers: {
apikey: `${config.get('key')}`
},
json: true
});
// Fetch installation info from airly api
const id = await got(`https://airapi.airly.eu/v2/installations/${cli.flags.installation}`, {
headers: {
apikey: `${config.get('key')}`
},
json: true
});
spinner.succeed('Done:\n');
const data = pollutions.body.current.values.filter(item => (
item.name === 'PM1' || item.name === 'PM25' || item.name === 'PM10'
));
const newData = data.map(el => ({...el, key: el.name}));
// Show user the table with envy
console.log(boxen(
`Particulate Matter (PM) in μg/m3:\n\n${bar(newData, {style: `${chalk.green('+')}`, padding: 3, barWidth: 5})} \n\n${chalk.dim.gray(`[Data from sensor nr. ${id.body.id} located in ${id.body.address.street}, ${id.body.address.city}, ${id.body.address.country}]`)}`
, {padding: 1, borderColor: 'yellow', borderStyle: 'round'}));
// Some info about air quality guidelines
console.log('\nAir quality guidelines recommended by WHO (24-hour mean):\n');
console.log(`${chalk.cyan('›')} PM 10: 50 μg/m3`);
console.log(`${chalk.cyan('›')} PM 2.5: 25 μg/m3`);
console.log('\nReady more about air quality here: https://bit.ly/2tbIhek');
} catch (error) {
spinner.fail('Something went wrong :(');
process.exit(1);
}
};
// Search by city
const startLocation = () => {
try {
// Get coordinates from provided location
getCoords(cli.flags.city)
.then(async coords => {
const {lat, lng} = coords;
// Search for the nearest installation
const search = await got(`https://airapi.airly.eu/v2/installations/nearest?lat=${lat}&lng=${lng}&maxResults=3&maxDistanceKM=-1`, {
headers: {
apikey: `${config.get('key')}`
},
json: true
});
// Select the installation
const sensor = search.body.map(v => v.id);
const address = search.body.map(v => v.address.street);
const response = await prompts({
type: 'select',
name: 'value',
message: 'Available sensors:',
choices: [
{title: `${address[0]}`, value: `${sensor[0]}`},
{title: `${address[1]}`, value: `${sensor[1]}`},
{title: `${address[2]}`, value: `${sensor[2]}`}
],
initial: 1
});
spinner.start('Fetching data...');
// Fetch pollution data from airly api
const pollutions = await got(`https://airapi.airly.eu/v2/measurements/installation?installationId=${response.value}`, {
headers: {
apikey: `${config.get('key')}`
},
json: true
});
// Fetch installation info from airly api
const id = await got(`https://airapi.airly.eu/v2/installations/${response.value}`, {
headers: {
apikey: `${config.get('key')}`
},
json: true
});
spinner.succeed('Done:\n');
const data = pollutions.body.current.values.filter(item => (
item.name === 'PM1' || item.name === 'PM25' || item.name === 'PM10'
));
const newData = data.map(el => ({...el, key: el.name}));
// Show user the table with envy
console.log(boxen(
`Particulate Matter (PM) in μg/m3:\n\n${bar(newData, {style: `${chalk.green('+')}`, padding: 3, barWidth: 5})} \n\n${chalk.dim.gray(`[Data from sensor nr. ${id.body.id} located in ${id.body.address.street}, ${id.body.address.city}, ${id.body.address.country}]`)}`
, {padding: 1, borderColor: 'yellow', borderStyle: 'round'}));
// Some info about air quality guidelines
console.log('\nAir quality guidelines recommended by WHO (24-hour mean):\n');
console.log(`${chalk.cyan('›')} PM 10: 50 μg/m3`);
console.log(`${chalk.cyan('›')} PM 2.5: 25 μg/m3`);
console.log('\nReady more about air quality here: https://bit.ly/2tbIhek');
});
} catch (error) {
spinner.fail('Something went wrong :(');
process.exit(1);
}
};
if (firstRun() === true) {
(async () => {
console.log('Welcome to airly-cli. If you want to use this CLI tool, you need to paste your Airly API Key below.');
console.log('You can get it here: https://bit.ly/2JAQGPK\n');
const response = await prompts({
type: 'text',
name: 'api',
message: 'Paste your API key here:'
});
config.set('key', response.api);
console.log('API key successfully set! Type `airly --help` for usage instructions.');
})();
} else if (cli.flags.city) {
startLocation();
} else if (cli.flags.reset) {
firstRun.clear();
config.clear();
console.log('API key deleted! Type `airly` to configure a new one.');
} else if (cli.flags.installation) {
startId();
} else {
console.log('Type `airly --help` for usage instructions.');
}