const urlMetadata = require('url-metadata') const csv = require('csvtojson') const log = console.log const { spawnSync } = require( 'child_process' ), // uptime = spawnSync( 'uptime', [ '-p' ] ) uptime = spawnSync( 'uptime', [ '-s' ] ) const csvFilePath = 'server-pages.csv' // const csvFilePath = 'server-pages-test.csv' const checkIfTitleMatches = obj => { return urlMetadata(obj['url']).then(metadata => { // success handler if (metadata.title == obj['siteTitle']) { // log(obj['shortUrl']) obj['status'] = 'up' } else { // console.log(metadata) obj['status'] = 'down' } return true }, error => { // failure handler console.log(error) }) } 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) { 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)) }) })