autoliveformcst / index.js
connorlixyz's picture
Update index.js
242ed1a verified
raw
history blame contribute delete
No virus
2.58 kB
const axios = require('axios');
const fs = require('fs');
const cron = require('node-cron');
const http = require('http');
const path = require('path');
const port = process.env.PORT || 7860;
// 定义要访问的网页URL数组
const urls = [
//huggingface
'https://connorlixyz-autoliveformcst.hf.space', // huggingface-本项目
//Codesphere
'https://48907-3000.2.codesphere.com', // CSP-DE
'https://hello.37128.codesphere.site', // CSP-DE
'https://47269-3000.4.codesphere.com', // CSP-US
'https://codesphere.nezha.bf', // CSP-US
'https://56945-3000.4.codesphere.com', // CSP-US4
'https://csp.littlejerry.eu.org', // CSP-US4
//pantheon
'https://dev-jonahconrad.pantheonsite.io/core/keep.php', // pantheon-only-nezha
//nether.host
// 添加更多的URL
];
// 创建日志文件
//const logFile = 'visit-log.txt';
// 访问网页并将结果写入日志
async function scrapeAndLog(url) {
try {
const response = await axios.get(url, {
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8'
}
});
const timestamp = new Date().toISOString();
const logMessage = `${timestamp}: Web visited Successfully ${url}\n`;
// 将访问结果写入日志文件
// fs.appendFileSync(logFile, logMessage);
console.log(logMessage);
} catch (error) {
const timestamp = new Date().toISOString();
const errorMessage = `${timestamp}: Web visited Error ${url}: ${error.message}\n`;
// 将错误信息写入日志文件
// fs.appendFileSync(logFile, errorMessage);
console.error(errorMessage);
}
}
// 使用cron来安排定期任务
cron.schedule('*/1 * * * *', () => {
console.log('Running webpage access...');
// 循环访问每个URL
urls.forEach((url) => {
scrapeAndLog(url);
});
});
const server = http.createServer((req, res) => {
if (req.url === '/') {
const filePath = path.join(__dirname, 'index.html');
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500);
res.end('Error loading index.html');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
} else {
res.writeHead(404);
res.end('Not Found');
}
});
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});