MCPcopy Create free account
hub / github.com/BrasilAPI/BrasilAPI / getSwellData

Function getSwellData

services/cptec/swell.js:9–72  ·  view source on GitHub ↗
(cityCode, days)

Source from the content-addressed store, hash-verified

7const parser = new XMLParser();
8
9export const getSwellData = async (cityCode, days) => {
10 const url = `${CPTEC_URL}/cidade/${cityCode}/todos/tempos/ondas.xml`;
11
12 try {
13 const swellData = await axios.get(url, {
14 responseType: 'application/xml',
15 responseEncoding: 'binary',
16 });
17
18 const jsonData = parser.parse(swellData.data);
19 const newSwellArr = {
20 cidade: jsonData.cidade.nome,
21 estado: jsonData.cidade.uf,
22 atualizado_em: jsonData.cidade.atualizacao,
23 ondas: [],
24 };
25
26 // group data by day
27 let oldDate = '';
28
29 let newItem = {};
30
31 newSwellArr.ondas = [];
32
33 jsonData.cidade.previsao.forEach((oneDay) => {
34 const datePart = oneDay.dia.split(' ');
35 const [date, hour, tz] = datePart;
36
37 if (date !== oldDate) {
38 [oldDate] = datePart;
39
40 newItem = {};
41 [newItem.data] = normalizeBrazilianDate(oldDate, '-', false)
42 .toISOString()
43 .split('T');
44 newItem.dados_ondas = [];
45 newSwellArr.ondas.push(newItem);
46 }
47
48 newItem.dados_ondas.push({
49 hora: `${hour.replace('h', ':')}00${tz}`,
50 vento: oneDay.vento,
51 direcao_vento: oneDay.vento_dir,
52 direcao_vento_desc: WIND_SWELL_DIRECTIONS[oneDay.vento_dir],
53 altura_onda: oneDay.altura,
54 direcao_onda: oneDay.direcao,
55 direcao_onda_desc: WIND_SWELL_DIRECTIONS[oneDay.direcao],
56 agitation: oneDay.agitacao,
57 });
58
59 return newItem;
60 });
61
62 // IF total data greater than requested number of days, slice array into correct size
63 if (newSwellArr.ondas.length > days) {
64 newSwellArr.ondas = newSwellArr.ondas.slice(0, days);
65 }
66

Callers 2

getSwellPredictionsFunction · 0.90
actionFunction · 0.90

Calls 1

normalizeBrazilianDateFunction · 0.85

Tested by

no test coverage detected