Skip to content

Commit 31970b2

Browse files
committed
Primer commit
0 parents  commit 31970b2

6 files changed

Lines changed: 338 additions & 0 deletions

File tree

.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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Aplicación del Clima - Curso Node
2+
3+
Recuerden ejecutar ```npm install``` para las librerias
4+
5+
### Ejemplo:
6+
7+
```
8+
node app -d "New York"
9+
```

app.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const lugar = require('./lugar/lugar');
2+
const clima = require('./clima/clima');
3+
4+
const argv = require('yargs').options({
5+
direccion: {
6+
alias: 'd',
7+
desc: 'Direccion de la ciudad para obtener el clima',
8+
demand: true
9+
}
10+
}).argv;
11+
12+
// lugar.getLugarLatLng(argv.direccion)
13+
// .then(console.log);
14+
15+
// clima.getClima(35, 139).then(console.log).catch(console.log);
16+
17+
const getInfo = async(direccion) => {
18+
19+
try {
20+
const getLugar = await lugar.getLugarLatLng(direccion);
21+
const getClima = await clima.getClima(getLugar.lat, getLugar.lng);
22+
return `El clima de ${ getLugar.direccion } es de ${getClima}.`
23+
} catch (error) {
24+
return `No se pudo determinar el clima de ${ direccion }`
25+
}
26+
27+
28+
// console.log(clima);
29+
}
30+
31+
getInfo(argv.direccion).then(console.log).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 respuesta = await axios.get(`https://api.openweathermap.org/data/2.5/weather?lat=${ lat }&lon=${ lng }&appid=d7e5e315ab7f339cdf4b1a7e594624f3&units=metric`);
6+
7+
return respuesta.data.main.temp;
8+
9+
}
10+
11+
module.exports = {
12+
getClima
13+
}

lugar/lugar.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const axios = require('axios');
2+
3+
const getLugarLatLng = 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': '97fc01bfe1mshd3fcab6c6586597p176a54jsnb1acf68d65ff' }
10+
});
11+
12+
const resp = await instance.get();
13+
14+
if (resp.data.Results.length === 0) {
15+
throw new Error(`No hay resultados para ${dir}`)
16+
}
17+
18+
const data = resp.data.Results[0];
19+
const direccion = data.name;
20+
const lat = data.lat;
21+
const lng = data.lon;
22+
23+
return {
24+
direccion,
25+
lat,
26+
lng
27+
}
28+
}
29+
const getLugar = async() => {
30+
const encodedUrl = encodeURI(dir);
31+
32+
const instance = axios.create({
33+
baseURL: `https://devru-latitude-longitude-find-v1.p.rapidapi.com/latlon.php?location=${encodedUrl}`,
34+
headers: { 'x-rapidapi-key': '97fc01bfe1mshd3fcab6c6586597p176a54jsnb1acf68d65ff' }
35+
});
36+
37+
const resp = await instance.get();
38+
39+
if (resp.data.Results.length === 0) {
40+
throw new Error(`No hay resultados para ${dir}`)
41+
}
42+
43+
const data = resp.data.Results[0];
44+
const direccion = data.name;
45+
const lat = data.lat;
46+
const lng = data.lon;
47+
48+
return {
49+
direccion,
50+
lat,
51+
lng
52+
}
53+
}
54+
55+
56+
57+
module.exports = {
58+
getLugarLatLng
59+
}

package-lock.json

Lines changed: 225 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)