-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (125 loc) · 3.88 KB
/
index.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
const query = process.argv[2];
if (!query) return console.error('Exemplo de uso: \x1b[1mnpx previsao brasilia\x1b[0m');
const latinize = require('latinize');
const https = require('https');
const fs = require('fs');
const path = require('path');
let ibge = 'https://www.ibge.gov.br/components/com_dados_municipios/assets/municipios.json';
let ibge_path = path.resolve(__dirname, 'ibge.json');
let ibge_data = [];
fs.stat(ibge_path, err => {
if (err) {
getHTTPS(ibge).then(data => {
ibge_data = data;
findPlace();
fs.writeFile(ibge_path, JSON.stringify(data, null, 4), (err) => {
if (err) { console.log(err); }
});
}).catch(err => {
console.log(err);
});
}
else {
ibge_data = require(ibge_path);
findPlace();
}
});
function findPlace() {
for (let i = 0; i < ibge_data.length; i++) {
let item = ibge_data[i];
if (item.id === query || (latinize(item.nome.toLowerCase()) === latinize(query.toLowerCase())) && item.id > 1000) {
queryPrevMet(item.id);
}
}
}
function queryPrevMet(id) {
let url = `https://apiprevmet3.inmet.gov.br/previsao/${id}`;
getHTTPS(url).then(data => {
let result = data[id];
if (!result) return console.error(`Dados do id ${id} não encontrados. ${(data || {}).error || ''}`);
let resumed = {};
for (let day in result) {
let item = result[day];
for (let period in item) {
if (item['manha']) {
if (!resumed[day]) resumed[day] = {};
for (let key in item[period]) {
if (key.split('icone').length > 1) {
delete item[period][key];
}
}
resumed[day][period] = item[period]
}
}
}
prettyPrint(resumed, id);
});
}
const map = {
'S': '⇑',
'N': '⇓',
'E': '⇐',
'W': '⇒',
'SE': '⇖',
'SW': '⇗',
'NW': '⇘',
'NE': '⇙'
}
function getArrow(direction) {
return map[direction];
}
function prettyPrint(data, id) {
let cityLog = false;
let keys = Object.keys(data).reverse();
for (let day of keys) {
let item = data[day];
if (!cityLog) {
console.log(`\n\n\x1b[1m\x1b[4m${item.manha.entidade} - ${item.manha.uf} (COD. IBGE ${id})\x1b[0m`)
cityLog = true;
console.log('\n');
}
console.log(`\x1b[1m\x1b[4m● ${day}${checkIfToday(day)}\x1b[0m`);
for (let period in item) {
let p = item[period];
let string_period = getString(period);
let dir = (p.dir_vento || '').split('-');
console.log('\x1b[1m', `
\x1b[4m● ${string_period}\x1b[0m (${p.resumo})`);
console.log(`
Temperatura: ${p.temp_min}º min / ${p.temp_max}º max
Vento: ${p.dir_vento} ${(p.int_vento || '').toLowerCase()} ${getArrow(dir[0])} - ${getArrow(dir[1])}
Umidade: ${p.umidade_min}% min / ${p.umidade_max}% max`)
}
console.log('\n');
}
}
function checkIfToday(day) {
return '';
}
let translate = {
manha: 'Manhã',
tarde: 'Tarde',
noite: 'Noite'
}
function getString(str) {
return translate[str];
}
function getHTTPS(url) {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (err) {
console.log(url, '\n\n', data);
}
});
}).on("error", (err) => {
reject(err.message);
});
})
}