require('dotenv').config(); const Mustache = require('mustache'); const fetch = require('node-fetch'); const RssParser = require('rss-parser'); const fs = require('fs'); const puppeteerService = require('./services/puppeteer.service'); const MUSTACHE_MAIN_DIR = './main.mustache'; let DATA = { refresh_date: new Date().toLocaleDateString('en-US', { weekday: 'long', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZoneName: 'short', timeZone: 'America/Detroit', }), posts: [] }; async function setWeatherInformation() { await fetch( `https://api.openweathermap.org/data/2.5/weather?q=grand%20blanc%2C%20michigan&appid=${process.env.OPEN_WEATHER_MAP_KEY}&units=imperial` ) .then(r => r.json()) .then(r => { DATA.city_temperature = Math.round(r.main.temp); DATA.city_weather = r.weather[0].description; DATA.city_weather_icon = r.weather[0].icon; DATA.sun_rise = new Date(r.sys.sunrise * 1000).toLocaleString('en-US', { timeZone: 'America/Detroit', }); DATA.sun_set = new Date(r.sys.sunset * 1000).toLocaleString('en-US', { timeZone: 'America/Detroit', }); }); } async function setRssFeed() { const parser = new RssParser() await parser.parseURL("https://srp.life/feed") .then(r => r.items.map(val => { if (!val.title.includes("Daily Orthodox") && val !== undefined) { return val; }})) .then(r => { for (let i = 0; i < 3; i++) { if (r[i]) { DATA.posts.push({ "title": r[i].title, "desc": r[i].summary, "link": r[i].link}); } } }) } async function setInstagramPosts() { const instagramImages = await puppeteerService.getLatestInstagramPostsFromAccount('orthodoxyforyou', 3); DATA.img1 = instagramImages[0]; DATA.img2 = instagramImages[1]; DATA.img3 = instagramImages[2]; } async function generateReadMe() { await 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(); /** * Get pictures */ await setInstagramPosts(); await setRssFeed(); /** * Generate README */ await generateReadMe(); /** * Fermeture de la boutique 👋 */ await puppeteerService.close(); } action();