-
Notifications
You must be signed in to change notification settings - Fork 0
/
weatherAPI.js
102 lines (89 loc) · 2.97 KB
/
weatherAPI.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
import { DateTime } from 'luxon';
import { OPEN_WEATHER_MAP_API_KEY } from './credentials.js';
import axios from 'axios';
import Table from 'cli-table3';
import chalk from 'chalk';
const units = {
temperature: ' °C',
wind_speed: ' m/s',
};
async function getData(url) {
try {
const response = await axios.get(url);
const data = response.data;
console.log(data);
return data;
} catch (error) {
const errorMessages = {
404:
'Denumirea orașului nu este validă. ' +
'Vă rugăm verificați dacă ați introdus corect numele orașului.',
401: 'API key este incorectă. Vă rugăm verificați fișierul credentials.js.',
429: 'Ați depășit limita de cererei către OpenWeatherMap API.',
500: 'Ne pare rău, a apărut o eroare internă a serverului.',
ENOTFOUND:
'Nu există o conexiune cu internetul.' +
'Verificați setările și aparatajul pentru interent.',
get EAI_AGAIN() {
return this.ENOTFOUND;
},
};
const errorCode = error.code || Number(error.response.data.cod);
console.log(chalk.red.bgYellow.bold(errorMessages[errorCode]));
process.exit();
}
}
/**
* @typedef {Object} Coords
* @property {number} lat - geo latitute
* @property {number} lon - geo longitude
*/
/**
* Prints current weather condition for a city and returns its coordinates
* @param {string} cityName
* Name of the city
* @returns {Coords}
* The geographical coordinates
*/
export async function printCurrentWeather(cityName) {
const OPEN_WEATHER_MAP_API =
`https://api.openweathermap.org/data/2.5/weather?q=${cityName}` +
`&appid=${OPEN_WEATHER_MAP_API_KEY}&units=metric&lang=ro`;
const data = await getData(OPEN_WEATHER_MAP_API);
console.log(
`În ${data.name} este ${data.weather[0].description}.` +
`\nTemperatura curentă este de ${data.main.temp}${units.temperature}.` +
`\nLong: ${data.coord.lon} Lat: ${data.coord.lat}`
);
return data.coord;
}
/**
* Print 8 days weather forecast for a geographical location
* @param {Coords} coords - geographical coordinates of the locatoin
*/
export async function printWeatherFor7Days({ lat, lon }) {
const OPEN_WEATHER_MAP_API =
`https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}` +
`&appid=${OPEN_WEATHER_MAP_API_KEY}&units=metric&lang=ro`;
const data = await getData(OPEN_WEATHER_MAP_API);
printForecastTable(data);
}
function printForecastTable(data) {
const table = new Table({
head: ['Data', 'Temp max.', 'Temp min.', 'Vinteza vântului'],
});
const dailyData = data.daily;
dailyData.forEach((dayData) => {
const date = DateTime.fromSeconds(dayData.dt)
.setLocale('ro')
.toLocaleString(DateTime.DATE_MED);
const row = [
date,
dayData.temp.max + units.temperature,
dayData.temp.min + units.temperature,
dayData.wind_speed + units.wind_speed,
];
table.push(row);
});
console.log(table.toString());
}