|
const axios = require('axios'); |
|
const http = require('http'); |
|
const cron = require('node-cron'); |
|
const port = process.env.PORT || 7860; |
|
const moment = require('moment-timezone'); |
|
|
|
|
|
const urls = [ |
|
|
|
|
|
|
|
|
|
|
|
'https://www.yahoo.com', |
|
'https://www.baidu.com', |
|
'https://www.yahoo.com', |
|
'https://illustrious-little-can.glitch.me', |
|
|
|
|
|
|
|
'https://handy-harmonious-utensil.glitch.me', |
|
'https://noiseless-ash-reaction.glitch.me', |
|
'https://sharp-changeable-viscount.glitch.me', |
|
|
|
]; |
|
|
|
|
|
function visitWebsites() { |
|
const websites = [ |
|
'https://instinctive-night-gruyere.glitch.me', |
|
'https://rattle-simple-fog.glitch.me', |
|
'https://oasis-cheddar-change.glitch.me', |
|
'https://lofty-glacier-coconut.glitch.me', |
|
'https://working-splendid-nigella.glitch.me', |
|
'https://peppermint-sticky-magnesium.glitch.me', |
|
'https://butter-ultra-grandparent.glitch.me', |
|
'https://cubic-dashing-pyjama.glitch.me', |
|
'https://creative-clover-cerise.glitch.me', |
|
'https://northern-scythe-celestite.glitch.me', |
|
'https://shorthaired-rowan-jade.glitch.me', |
|
'https://sleepy-knowing-neem.glitch.me', |
|
'https://hot-relieved-galleon.glitch.me', |
|
'https://stingy-ionian-eggplant.glitch.me', |
|
'https://rainbow-laced-sandwich.glitch.me', |
|
'https://bouncy-round-aspen.glitch.me', |
|
'https://illustrious-little-can.glitch.me', |
|
'https://sneaky-available-force.glitch.me', |
|
'https://quasar-square-route.glitch.me', |
|
'https://rattle-valiant-milkshake.glitch.me', |
|
'https://guttural-burly-sceptre.glitch.me', |
|
'https://encouraging-boiling-blouse.glitch.me', |
|
'https://abstracted-available-queen.glitch.me', |
|
'https://caring-darkened-okra.glitch.me', |
|
'https://honored-verbena-taurus.glitch.me', |
|
'https://concrete-imaginary-worm.glitch.me', |
|
'https://numerous-nice-polyester.glitch.me', |
|
'https://lead-chiseled-parrot.glitch.me', |
|
'https://invented-spiffy-centipede.glitch.me', |
|
'https://admitted-adjoining-botany.glitch.me', |
|
'https://successful-rift-show.glitch.me', |
|
'https://iced-abundant-punishment.glitch.me', |
|
'https://deciduous-bubbly-bag.glitch.me', |
|
'https://fine-continuous-neighbor.glitch.me', |
|
'https://fanatical-young-cork.glitch.me', |
|
'https://translucent-magnificent-quicksand.glitch.me', |
|
'https://dazzling-near-contraption.glitch.me', |
|
'https://spiky-curly-salto.glitch.me', |
|
'https://standing-elated-trader.glitch.me', |
|
'https://voltaic-sequoia-alfalfa.glitch.me', |
|
'https://eminent-remarkable-lawyer.glitch.me', |
|
'https://impartial-hushed-butterfly.glitch.me', |
|
'https://hexagonal-reflective-toast.glitch.me', |
|
'https://muddy-nasal-gasosaurus.glitch.me', |
|
'https://butternut-trail-market.glitch.me', |
|
'https://jungle-cooked-foam.glitch.me', |
|
'https://whimsical-locrian-baroness.glitch.me', |
|
'https://early-thundering-brake.glitch.me', |
|
'https://butter-husky-year.glitch.me', |
|
'https://bolder-bush-wok.glitch.me', |
|
'https://solar-mewing-clave.glitch.me', |
|
'https://trite-mountainous-pirate.glitch.me', |
|
'https://mica-light-paper.glitch.me', |
|
'https://lyrical-spiral-anaconda.glitch.me', |
|
'https://skinny-difficult-hoodie.glitch.me', |
|
'https://stormy-misty-space.glitch.me', |
|
'https://shorthaired-alpine-liver.glitch.me', |
|
'https://flaxen-impartial-huckleberry.glitch.me', |
|
'https://cliff-cheddar-ton.glitch.me', |
|
'https://enchanted-brief-waxflower.glitch.me', |
|
'https://stealth-sugar-scorpion.glitch.me', |
|
'https://ruddy-bow-wormhole.glitch.me', |
|
'https://shrouded-bustling-respect.glitch.me', |
|
'https://unleashed-capricious-respect.glitch.me', |
|
'https://solstice-habitual-tenor.glitch.me', |
|
'https://surf-stingy-mailman.glitch.me', |
|
'https://thoracic-locrian-mask.glitch.me', |
|
'https://impartial-defiant-hockey.glitch.me', |
|
'https://past-subsequent-error.glitch.me', |
|
'https://respected-efficacious-prince.glitch.me', |
|
'https://noiseless-ash-reaction.glitch.me', |
|
'https://sharp-changeable-viscount.glitch.me', |
|
'https://handy-harmonious-utensil.glitch.me', |
|
'https://rapid-lacy-mile.glitch.me', |
|
'https://material-lopsided-carver.glitch.me', |
|
'https://helix-angry-stew.glitch.me', |
|
'https://wooded-creative-crime.glitch.me', |
|
'https://glow-low-gastonia.glitch.me', |
|
'https://easy-holly-copper.glitch.me', |
|
'https://indecisive-helix-toad.glitch.me', |
|
|
|
|
|
|
|
|
|
|
|
'https://www.google.com' |
|
|
|
]; |
|
|
|
|
|
websites.forEach(async (url) => { |
|
try { |
|
const response = await axios.get(url); |
|
console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Visited web successfully: ${url} - Status code: ${response.status}\n`); |
|
} catch (error) { |
|
console.error(`Error visiting ${url}: ${error.message}\n`); |
|
} |
|
}); |
|
} |
|
|
|
|
|
function checkAndSetTimer() { |
|
const currentMoment = moment().tz('Asia/Hong_Kong'); |
|
const formattedTime = currentMoment.format('YYYY-MM-DD HH:mm:ss'); |
|
if (currentMoment.hours() >= 1 && currentMoment.hours() < 5) { |
|
console.log(`Stop visit from 1:00 to 8:00 --- ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`); |
|
clearInterval(visitIntervalId); |
|
const nextVisitTime = moment().tz('Asia/Hong_Kong').add(0, 'day').hours(8).minutes(0).seconds(0); |
|
const nextVisitInterval = nextVisitTime.diff(currentMoment); |
|
setTimeout(() => { |
|
startVisits(); |
|
}, nextVisitInterval); |
|
} else { |
|
startVisits(); |
|
} |
|
} |
|
|
|
let visitIntervalId; |
|
function startVisits() { |
|
clearInterval(visitIntervalId); |
|
|
|
visitIntervalId = setInterval(() => { |
|
visitWebsites(); |
|
}, 3 * 60 * 1000); |
|
} |
|
|
|
function runScript() { |
|
const runScriptIntervalId = setInterval(() => { |
|
|
|
checkAndSetTimer(); |
|
}, 3 * 60 * 1000); |
|
} |
|
checkAndSetTimer(); |
|
runScript(); |
|
|
|
|
|
async function scrapeAndLog(url) { |
|
try { |
|
const response = await axios.get(url); |
|
console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Web visited Successfully: ${url} - Status code: ${response.status}\n`); |
|
} catch (error) { |
|
console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}: Web visited Error: ${url}: ${error.message}\n`); |
|
} |
|
} |
|
|
|
cron.schedule('*/3 * * * *', () => { |
|
console.log('Running webpage access...'); |
|
urls.forEach((url) => { |
|
scrapeAndLog(url); |
|
}); |
|
}); |
|
|
|
|
|
const server = http.createServer((req, res) => { |
|
if (req.url === '/') { |
|
res.writeHead(200, {'Content-Type': 'text/plain'}); |
|
res.end('Hello, World!\n'); |
|
} else { |
|
res.writeHead(404, {'Content-Type': 'text/plain'}); |
|
res.end('Not Found\n'); |
|
} |
|
}); |
|
|
|
server.listen(port, () => { |
|
console.log(`Server is running on port:${port}`); |
|
}); |
|
|