Skip to content

Commit 2c2fc8f

Browse files
committed
Primer commit
0 parents  commit 2c2fc8f

File tree

8 files changed

+575
-0
lines changed

8 files changed

+575
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Aplicacion del clima - Curso node
2+
3+
Recuerden ejecutar ```npm install``` para las librerias
4+
5+
### Ejemplo:
6+
```
7+
node app -d "Madrid"
8+
```

app.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const lugar = require ('./lugar/lugar');
2+
const clima = require ('./clima/clima');
3+
const info = require ('./info/info');
4+
5+
const argv =require ('yargs').options({
6+
direccion: {
7+
alias: 'd',
8+
desc: 'Direcion de la cuidad a obtener el clima',
9+
deman: true
10+
}
11+
12+
}).argv;
13+
14+
lugar.getLugarLatLong(argv.direccion)
15+
.then (console.log);
16+
17+
18+
clima.getClima(35,139)
19+
.then (console.log)
20+
.catch (console.log);
21+
22+
info.getInfo(argv.direccion)
23+
.then (console.log)
24+
.catch (console.log);

clima/clima.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const axios = require ('axios');
2+
3+
const getClima = async (lat, lng) => {
4+
5+
const resp = await axios.get(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&APPID=edee8540d6d522f3e3c675d3b5fd38df&units=metric`)
6+
7+
return resp.data.main.temp;
8+
9+
}
10+
11+
module.exports = {
12+
getClima
13+
}

info/info.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
const lugar = require ('../lugar/lugar');
3+
const clima = require ('../clima/clima');
4+
5+
const getInfo = async (direccion) => {
6+
7+
try {
8+
const coords= await lugar.getLugarLatLong(direccion);
9+
const temp= await clima.getClima(coords.lat, coords.lng);
10+
return console.log(`El clima de ${direccion} es de ${temp}`);
11+
12+
} catch (error) {
13+
throw new Error (`No se pudo determinar el clima de ${direccion}`);
14+
15+
}
16+
}
17+
18+
module.exports = {
19+
getInfo
20+
}

lugar/lugar.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const axios= require('axios');
2+
3+
const getLugarLatLong = async(dir) => {
4+
5+
const encodedUrl = encodeURI(dir);
6+
7+
const instance = axios.create({
8+
baseURL: `https://devru-latitude-longitude-find-v1.p.rapidapi.com/latlon.php?location=${encodedUrl}`,
9+
headers: {'X-RapidAPI-Key': '9878a58125mshaedb75df5ecb322p15df4djsnbd795bcd4d51'}
10+
});
11+
12+
const resp = await instance.get();
13+
if (resp.data.Results.length === 0){
14+
throw new Error (`No hay resultados para ${dir}`);
15+
}
16+
17+
const data = resp.data.Results[0];
18+
const direccion = data.name;
19+
const lat= data.lat;
20+
const lng= data.lon;
21+
22+
return {
23+
direccion,
24+
lat,
25+
lng
26+
}
27+
}
28+
29+
module.exports= {
30+
getLugarLatLong
31+
}

0 commit comments

Comments
 (0)