const urlMetadata = require('url-metadata') const csv = require('csvtojson') const log = console.log const { spawnSync } = require( 'child_process' ), uptime = spawnSync( 'uptime', [ '-s' ] ) const csvFilePath = 'server-pages.csv' // const csvFilePath = 'server-pages-test.csv' // consider webpage to be up if stored title matches fetched title const checkIfTitleMatches = obj => { return urlMetadata(obj['url']).then(metadata => { // success handler // log(metadata.title, obj['siteTitle']) if (metadata.title == obj['siteTitle']) { obj['status'] = 'up' } else { obj['status'] = 'down' } return true }, error => { // failure handler console.log(error) }) } // update all sites' status in JSON const updateSiteStatus = json => { const promiseArray = [] json.forEach(async function (obj) { if (obj['siteTitle'] != '') { promiseArray.push(checkIfTitleMatches(obj)) } else{ obj['status'] = 'up' } }) return promiseArray } csv().fromFile(csvFilePath).then(async function (json) { // finish updating all sites' status before outputting JSON Promise.all(updateSiteStatus(json)).then(results => { const statusJson = { "sites": json, "updateTime": Date.now(), "upSince": `${uptime.stdout.toString()}`.replace(/\n/g, ''), } // console.log(JSON.stringify(statusJson)) console.log(JSON.stringify(statusJson, null, 2)) }) })