-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
53 lines (51 loc) · 1.48 KB
/
weather.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
import axios from "axios";
import { xml2json } from "./xml2json.js";
import { response } from "express";
import { DOMParser } from "@xmldom/xmldom";
export async function getWeather() {
try {
const response = await axios.get(
"http://cab.inta-csic.es/rems/rems_weather.xml",
{
headers: {
Accept: "application/xml",
},
responseType: "document",
}
);
const xmlDoc = new DOMParser().parseFromString(response.data, "text/xml");
var json = xml2json(xmlDoc);
json = json.replace("undefined", "");
json = JSON.parse(json).weather_report;
console.log(json);
const interestingData = {
date: json.terrestrial_date,
min_air_temp: json.magnitudes.min_temp,
max_air_temp: json.magnitudes.max_temp,
pressure: json.magnitudes.pressure,
min_ground_temp: json.magnitudes.min_gts_temp,
max_ground_temp: json.magnitudes.max_gts_temp,
overallWeather: json.magnitudes.atmo_opacity,
sunrise: json.magnitudes.sunrise,
sunset: json.magnitudes.sunset,
month: json.magnitudes.season.split(" ")[1],
season: getSeason(json.magnitudes.season),
};
return interestingData;
} catch (error) {
console.log(error);
}
}
function getSeason(month) {
month = parseInt(month.split(" ")[1]);
console.log(month);
if (month < 4) {
return "Autumn";
} else if (month < 7) {
return "Winter";
} else if (month < 10) {
return "Spring";
} else {
return "Summer";
}
}