File size: 2,131 Bytes
ce5a60e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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-本项目
  //Goorm
  'https://my-nodejs-ivzud.run-us-west2.goorm.site',    // Goorm-linshi
  'https://ide-run.goorm.io/workspace/d8MS7D1BaRxuHgRKnSW?token=ada5536dbe5768f245c935ab547af078&guestname=0123',    // MC-servers-py
  

  // 添加更多的URL
];

// 创建日志文件
//const logFile = 'visit-log.txt';

// 访问网页并将结果写入日志
async function scrapeAndLog(url) {
  try {
    const response = await axios.get(url, {timeout: 1000});
    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('0 */2 * * * *', () => {
  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}`);
});