-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.cjs
133 lines (113 loc) · 4.72 KB
/
index.cjs
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
const xml2js = require('xml2js');
/**
* @class WeatherMSN
*/
class WeatherMSN {
/**
* @private
*/
#lang;
/**
* @private
*/
#degree;
/**
* @constructor
* @param {string} lang result language (ISO 639-1: en, id, ja)
* @param {string} degree degree type (C, F)
*/
constructor(lang, degree) {
this.#lang = lang;
this.#degree = degree;
if (!this.#lang || typeof this.#lang !== 'string') throw new Error('Please provide a valid lang parameter');
if (!this.#degree || typeof this.#degree !== 'string') throw new Error('Please provide a valid degree parameter');
if (this.#degree.toLowerCase() !== 'c' && this.#degree.toLowerCase() !== 'f') throw new Error('Invalid degree, must be either "C" or "F"');
}
/**
* @async
* @public
* @param {string} search location name
* @returns {Promise<{ temperature: string; weather: string; humidity: string; windspeed: string; location: string; }>}
*/
async getCurrentData(search) {
if (!search) {
throw new Error('Please provide a valid search parameter');
}
const msnWeatherUrl = `http://weather.service.msn.com/find.aspx?src=outlook&weasearchstr=${search}&weadegreetype=${this.#degree}&culture=${this.#lang}`;
let temperature, weather, humidity, windspeed, location, date, icon;
try {
const response = await fetch(msnWeatherUrl, {
method: 'GET'
});
const xml = await response.text();
const parser = new xml2js.Parser();
const data = await parser.parseStringPromise(xml);
temperature = data.weatherdata.weather[0].current[0]['$'].temperature;
weather = data.weatherdata.weather[0].current[0]['$'].skytext;
humidity = data.weatherdata.weather[0].current[0]['$'].humidity;
windspeed = data.weatherdata.weather[0].current[0]['$'].windspeed;
location = data.weatherdata.weather[0].current[0]['$'].observationpoint;
date = data.weatherdata.weather[0].current[0]['$'].date;
icon = data.weatherdata.weather[0]['$'].imagerelativeurl + 'law/' + data.weatherdata.weather[0].current[0]['$'].skycode;
return {
temperature,
weather,
humidity,
windspeed,
location,
date,
icon
};
} catch (err) {
throw new Error('Error fetching or parsing weather data');
}
}
/**
* @async
* @public
* @param {string} search location name
* @param {number} days many day from today
* @returns {Promise<{ lowTemperature: string; highTemperature: string; date: string; day: string; weather: string; location: string; }>}
*/
async getForecastData(search, days) {
if (!search && !days) {
throw new Error('Please provide a valid search parameter and days parameter');
}
if (typeof days !== 'number') {
throw new Error('Please provide a valid day type');
}
days -= 1;
if (days < 0 || days > 4) {
throw new Error('Days parameter can\'t be less than one or greater than five');
}
const msnWeatherUrl = `http://weather.service.msn.com/find.aspx?src=outlook&weasearchstr=${search}&weadegreetype=${this.#degree}&culture=${this.#lang}`;
let lowTemperature, highTemperature, date, day, weather, location, icon;
try {
const response = await fetch(msnWeatherUrl, {
method: 'GET'
});
const xml = await response.text();
const parser = new xml2js.Parser();
const data = await parser.parseStringPromise(xml);
lowTemperature = data.weatherdata.weather[0].forecast[days]['$'].low;
highTemperature = data.weatherdata.weather[0].forecast[days]['$'].high;
date = data.weatherdata.weather[0].forecast[days]['$'].date;
day = data.weatherdata.weather[0].forecast[days]['$'].day;
weather = data.weatherdata.weather[0].forecast[days]['$'].skytextday;
location = data.weatherdata.weather[0].current[0]['$'].observationpoint;
icon = data.weatherdata.weather[0]['$'].imagerelativeurl + 'law/' + data.weatherdata.weather[0].forecast[days]['$'].skycode;
return {
lowTemperature,
highTemperature,
date,
day,
weather,
location,
icon
};
} catch (err) {
throw new Error('Error fetching or parsing weather data');
}
}
}
module.exports = WeatherMSN;