connorlixyz's picture
Update index.js
4f74c5a verified
raw
history blame contribute delete
No virus
3.87 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-autokeepgormtest2mins.hf.space', // huggingface-本项目
//Goorm
'https://my-ddog-uqhbz.run-us-west2.goorm.site', // Goorm-linshi
'https://ide-run.goorm.io/workspace/dP4mA4YUJ1Uu3IoEAcV?token=0e179347ab1a58b7849d0af907a7faaa&guestname=0123', // MC-servers-py
// 添加更多的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_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36',
'Referer': 'https://ide-run.goorm.io/workspace/d8MS7D1BaRxuHgRKnSW?token=ada5536dbe5768f245c935ab547af078&guestname=0123',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Cookie': 'goormaccounts.sid=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDU1NTkyMTEsIlRva2VuVHlwZSI6ImxldmVsMSIsImlkIjoiM2M4bzZzbHFiX2d1ZXN0IiwiZW1haWwiOiIzYzhvNnNscWJfZ3Vlc3QiLCJjcmVhdGVkYXRlIjoxNzA0OTU0NDExLCJzZXNzaW9uaWQiOiJpTzlQR0pRNE5QQl8zV1dGcjZpMFVBM0VRNFNvcUJ6XzI5UHZyaUxVR09NPSIsIlRUTCI6MH0.O2j1SQHAyTL0FtH-2Ywd2nTBrgRZ5I9QTdtSjwsmyH-2NbWo000XEFH7tYNKx8OVJnK_y7aN4ee121rjI7RkA8NJ8DK6c6_WnKqRrS3M-CuxaTZdGvNw0qY0zLB5O8eg_e4ZdKnaV5aiLKmk0xPODZvSt7TdaBmCqnS-ThLrcB-aQywE3QsoNEc8bNUgyhWO9Hgg5uD5fyMACHLRO3xyxeW0We74f1JygE8DGJDhNYwPJlKi0QLVTFrWE55E0_DAjpjFDr1p5ncqZkjYe6GWkFnCBQJSnBiFhLsjUHId0vSretDlLc6M_QSfknNzlci0HNdT2CmZlNoCezoHvsG2gg; ch-veil-id=8fcdacf5-73ab-420e-83f6-1e55ff87ca08; ch-session-90340=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzZXMiLCJrZXkiOiI5MDM0MC02NTlmOGE0ZjNkMGEzZmE0MTAyZiIsImlhdCI6MTcwNDk1NDQ0NywiZXhwIjoxNzA3NTQ2NDQ3fQ.INcZwu6niJCzgARW51468_Al-m6HCSMaV73C6d7_W4c; ide.goorm.sid=s%3APmipTC94XLHtyG8Sw_TxvUqePnRE5iew.kvBbRzqUC7%2FlG5fY%2FXD%2B2vHuRKTILdGCLghErYc1SuY; io=9kkzAltNMh4aKwebAAAs; AWSALB=yZpqjKy76tEQFfdMehyishqcbaSZ0zLbfbhDDRPPM3ycYocpqqjyv4XkPBbdzuXmwb4IHMvuRY+qIhWbyD/oWiakVmLrdN/CTnJWQxuz2MBorhe6/yE9qoljWoEH; AWSALBCORS=yZpqjKy76tEQFfdMehyishqcbaSZ0zLbfbhDDRPPM3ycYocpqqjyv4XkPBbdzuXmwb4IHMvuRY+qIhWbyD/oWiakVmLrdN/CTnJWQxuz2MBorhe6/yE9qoljWoEH'
}
});
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('*/30 * * * * *', () => {
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}`);
});