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

Function getPredictionWeather

services/cptec/weather.js:135–185  ·  view source on GitHub ↗
(cityCode, days)

Source from the content-addressed store, hash-verified

133 * @returns {object}
134 */
135export const getPredictionWeather = async (cityCode, days) => {
136 const baseUrl = `${CPTEC_URL}/cidade/`;
137 let url = baseUrl;
138 if (days <= 4) {
139 url += `${cityCode}/previsao.xml`;
140 } else {
141 url += `7dias/${cityCode}/previsao.xml`;
142 }
143
144 const weatherPredictions = await axios.get(url, {
145 responseType: 'application/xml',
146 responseEncoding: 'binary',
147 });
148
149 const parsed = parser.parse(weatherPredictions.data);
150
151 if (parsed.cidade) {
152 const jsonData = formatPrediction(parsed);
153 if (jsonData.cidade === 'null') {
154 return null;
155 }
156
157 // If number of days requested was greater than 7, load extended data from service
158 // Disabled by inconsistences on CPTEC endpoints
159 /* if (days > 7) {
160 const extendedPredictions = await axios.get(
161 `${baseUrl + cityCode}/estendida.xml`,
162 {
163 responseType: 'application/xml',
164 responseEncoding: 'binary',
165 }
166 );
167
168 const extendedJsonData = parser.parse(extendedPredictions.data);
169 if (extendedJsonData.cidade) {
170 jsonData.clima = [
171 ...jsonData.clima,
172 ...formatPrediction(extendedJsonData).clima,
173 ];
174 }
175 } */
176
177 // IF total data greater than requested number of days, slice array into correct size
178 if (jsonData.clima.length > days) {
179 jsonData.clima = jsonData.clima.slice(0, days);
180 }
181
182 return jsonData;
183 }
184 return [];
185};

Callers 2

actionFunction · 0.90
actionFunction · 0.90

Calls 1

formatPredictionFunction · 0.85

Tested by

no test coverage detected