|
const axios = require('axios'); |
|
const moment = require('moment-timezone'); |
|
const http = require('http'); |
|
const cron = require('node-cron'); |
|
const port = process.env.PORT || 7860; |
|
|
|
|
|
const webpages = [ |
|
|
|
|
|
]; |
|
|
|
|
|
const urls = [ |
|
'https://asfag654-sss.hf.space', |
|
'https://asfag654-kk.hf.space', |
|
'https://asfag654-g.hf.space', |
|
'https://asfag654-z.hf.space', |
|
'https://asfag654-t.hf.space', |
|
'https://asfag654-libretranslate.hf.space', |
|
'https://shegy-kg.hf.space' |
|
|
|
]; |
|
|
|
|
|
const visit = 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: ${response.status}\n`); |
|
} catch (error) { |
|
console.error(`Failed to visit ${url}: ${error.message}\n`); |
|
} |
|
}; |
|
const visitAll = async () => { |
|
for (let url of urls) { |
|
await visit(url); |
|
} |
|
}; |
|
|
|
|
|
const isWithinTime = () => { |
|
const now = moment().tz('Asia/Hong_Kong'); |
|
const hour = now.hour(); |
|
if (hour >= 1 && hour < 6) { |
|
return false; |
|
} |
|
return true; |
|
}; |
|
|
|
|
|
const createServer = () => { |
|
const server = http.createServer((req, res) => { |
|
if (req.url === '/') { |
|
res.writeHead(200, {'Content-Type': 'text/plain'}); |
|
res.end('Hello world!'); |
|
} else { |
|
res.writeHead(404, {'Content-Type': 'text/plain'}); |
|
res.end('404 Not Found'); |
|
} |
|
}); |
|
server.listen(port, () => { |
|
console.log(`Server started on port ${port}`); |
|
}); |
|
}; |
|
|
|
|
|
const main = async () => { |
|
createServer(); |
|
setInterval(async () => { |
|
if (isWithinTime()) { |
|
await visitAll(); |
|
} else { |
|
console.log(`Stop visiting at ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`); |
|
} |
|
}, 2 * 60 * 1000); |
|
}; |
|
main(); |
|
|
|
|
|
async function access(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:${response.status}`); |
|
} catch (error) { |
|
console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Failed to visit ${url}, Error ${error.message}`); |
|
} |
|
} |
|
|
|
async function batchVisit() { |
|
|
|
for (let url of webpages) { |
|
await access(url); |
|
} |
|
} |
|
cron.schedule('*/2 * * * *', batchVisit); |