/
index.js
76 lines (71 loc) · 2.01 KB
/
index.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
// index.js
require('dotenv').config();
const Mustache = require('mustache');
const fetch = require('node-fetch');
const fs = require('fs');
const MUSTACHE_MAIN_DIR = './main.mustache';
/**
* DATA is the object that contains
* the data to be provided to Mustache
* (the refresh time).
*/
let DATA = {
refreshTime: new Date().toLocaleDateString('en-US', {
weekday: 'long',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
timeZoneName: 'short',
timeZone: 'America/Bogota',
}),
};
/**
* Here is the temperature
* of Bogota city
*/
async function setWeatherInformation() {
await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=Bogota&units=metric&appid=${process.env.OPEN_WEATHER_MAP_KEY}`
)
.then(r => r.json())
.then(r => {
DATA.city_temperature = Math.round(r.main.temp);
DATA.feels_like_temp = Math.round(r.main.feels_like);
DATA.city_weather = r.weather[0].description;
DATA.weather_icon = 'http://openweathermap.org/img/w/' + r.weather[0].icon + '.png';
DATA.humidity = r.main.humidity;
DATA.sun_rise = new Date(r.sys.sunrise * 1000).toLocaleString('en-US', {
hour: '2-digit',
minute: '2-digit',
timeZone: 'America/Bogota',
});
DATA.sun_set = new Date(r.sys.sunset * 1000).toLocaleString('en-US', {
hour: '2-digit',
minute: '2-digit',
timeZone: 'America/Bogota',
});
});
}
/**
* A - We open 'main.mustache'
* B - We ask Mustache to render our file with the data
* C - We create a README.md file with the generated output
*/
function generateReadMe() {
fs.readFile(MUSTACHE_MAIN_DIR, (err, data) => {
if (err) throw err;
const output = Mustache.render(data.toString(), DATA);
fs.writeFileSync('README.md', output);
});
}
async function action() {
/**
* Fetch weather
*/
await setWeatherInformation();
/**
* Generate README
*/
generateReadMe();
}action();