File size: 2,245 Bytes
ce5a60e
 
 
 
 
 
 
 
 
cb2ad31
e814157
cb2ad31
b12a0f9
cb2ad31
 
31a42cf
ce5a60e
 
 
 
 
 
 
 
 
 
 
dd20700
ce5a60e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd20700
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
77
78
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-jgkp9p1macbvfqzu.hf.space',    // huggingface-本项目
  // Goorm
  //'https://75b4f056-6316-4113-8079-5465cacbbc7d.e1-eu-north-azure.choreoapps.dev',    // Goorm-linshi
  'https://798f9c74-90e5-496e-b0bf-fe010324b0e7.e1-eu-north-azure.choreoapps.dev',    // MC-servers-py
  // US4
  'https://20c2aa79-0795-4b71-bad9-8a8d05ed3e92.e1-eu-north-azure.choreoapps.dev',    // MC-servers-py
  

  // 添加更多的URL
];

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

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