oooooo commited on
Commit
eae699e
1 Parent(s): f0c2839

Create index.js

Browse files
Files changed (1) hide show
  1. index.js +106 -0
index.js ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const axios = require('axios');
2
+ const http = require('http');
3
+ const cron = require('node-cron');
4
+ const port = process.env.PORT || 7860;
5
+ const moment = require('moment-timezone');
6
+
7
+ // 添加24小时访问的URL数组
8
+ const urls = [
9
+ 'https://elfin-root-passbook.glitch.me/', // 此处可备注名称,例如:glitch
10
+ 'https://cloud-common-curve.glitch.me', // 此处可备注名称,例如:glitch
11
+ 'https://www.baidu.com', // 此处可备注名称,例如:glitch
12
+ 'https://www.yahoo.com', // 此处可备注名称,例如:glitch
13
+ 'https://www.baidu.com', // 此处可备注名称,例如:glitch
14
+ 'https://www.yahoo.com', // 此处可备注名称,例如:glitch
15
+ 'https://www.baidu.com', // 此处可备注名称,例如:glitch
16
+ 'https://www.yahoo.com', // 此处可备注名称,例如:glitch
17
+ // 添加更多24小时不间断访问的URL
18
+ ];
19
+
20
+ // 添加在01:00至05:00暂停访问,其他时间正常访问的URL数组
21
+ function visitWebsites() {
22
+ const websites = [
23
+ 'https://www.google.com', // 此处可备注名称,例如:Back4app
24
+ 'https://www.google.com', // 此处可备注名称,例如:Back4app
25
+ 'https://www.google.com', // 此处可备注名称,例如:Back4app
26
+ 'https://www.google.com' // 此处可备注名称,例如:Back4app,最后一个url后面没有逗号
27
+ //添加更多的指定时间访问的URL
28
+ ];
29
+
30
+ // 遍历网页数组并发送请求
31
+ websites.forEach(async (url) => {
32
+ try {
33
+ const response = await axios.get(url);
34
+ console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Visited web successfully: ${url} - Status code: ${response.status}\n`);
35
+ } catch (error) {
36
+ console.error(`Error visiting ${url}: ${error.message}\n`);
37
+ }
38
+ });
39
+ }
40
+
41
+ // 检查并设置定时器
42
+ function checkAndSetTimer() {
43
+ const currentMoment = moment().tz('Asia/Hong_Kong');
44
+ const formattedTime = currentMoment.format('YYYY-MM-DD HH:mm:ss');
45
+ if (currentMoment.hours() >= 1 && currentMoment.hours() < 5) {
46
+ console.log(`Stop visit from 1:00 to 5:00 --- ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`);
47
+ clearInterval(visitIntervalId); // 清除定时器
48
+ const nextVisitTime = moment().tz('Asia/Hong_Kong').add(0, 'day').hours(5).minutes(0).seconds(0);
49
+ const nextVisitInterval = nextVisitTime.diff(currentMoment);
50
+ setTimeout(() => {
51
+ startVisits();
52
+ }, nextVisitInterval);
53
+ } else {
54
+ startVisits();
55
+ }
56
+ }
57
+
58
+ let visitIntervalId;
59
+ function startVisits() {
60
+ clearInterval(visitIntervalId);
61
+ // visitWebsites();
62
+ visitIntervalId = setInterval(() => {
63
+ visitWebsites();
64
+ }, 2 * 60 * 1000); // 每2分钟执行一次访问
65
+ }
66
+
67
+ function runScript() {
68
+ const runScriptIntervalId = setInterval(() => {
69
+ //console.log('Running script');
70
+ checkAndSetTimer();
71
+ }, 2 * 60 * 1000); // 每2分钟检查一次
72
+ }
73
+ checkAndSetTimer();
74
+ runScript();
75
+
76
+ // 24小时不间断访问
77
+ async function scrapeAndLog(url) {
78
+ try {
79
+ const response = await axios.get(url);
80
+ console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Web visited Successfully: ${url} - Status code: ${response.status}\n`);
81
+ } catch (error) {
82
+ console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}: Web visited Error: ${url}: ${error.message}\n`);
83
+ }
84
+ }
85
+ // 每2分钟访问一次
86
+ cron.schedule('*/2 * * * *', () => {
87
+ console.log('Running webpage access...');
88
+ urls.forEach((url) => {
89
+ scrapeAndLog(url);
90
+ });
91
+ });
92
+
93
+ // 创建HTTP服务
94
+ const server = http.createServer((req, res) => {
95
+ if (req.url === '/') {
96
+ res.writeHead(200, {'Content-Type': 'text/plain'});
97
+ res.end('Hello, World!\n');
98
+ } else {
99
+ res.writeHead(404, {'Content-Type': 'text/plain'});
100
+ res.end('Not Found\n');
101
+ }
102
+ });
103
+
104
+ server.listen(port, () => {
105
+ console.log(`Server is running on port:${port}`);
106
+ });