-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (78 loc) · 1.97 KB
/
index.js
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
79
80
81
82
83
84
85
86
87
88
89
const phantom = require('phantom');
const fs = require('fs');
const config = require('./config');
const url = config.url;
const ms2s = 1000;
const interval = config.interval * ms2s;
let count = 1;
function sleep(time) {
// logger2(`sleep ${time/1000} s`)
return new Promise(res => {
setTimeout(function() {
res();
}, time);
})
}
async function beep(duration) {
while (duration > 0) {
await sleep(300);
console.log("\007");
duration --;
}
}
async function canBook(page) {
let res = false;
res = await page.evaluate(function() {
return $('#zreserve').text();
});
logger2(` 测试结果: ${res} ${res === '我要看房'}`)
return res === '我要看房';
}
async function reqBook(page) {
console.log(`
----------testing book 第${count}次请求------------`);
logger2(`----------testing book 第${count}次请求------------`)
const status = await canBook(page);
logger(new Date().toString(), status);
if (status) {
await beep(3000);
} else {
await sleep(interval);
}
count ++;
return status;
}
function logger(time, result, other) {
try {
let record = fs.readFileSync('log', 'utf-8');
record += ` ${time} ${result ? '可预订' : 'no'}\n`;
fs.writeFileSync('log', record);
} catch(e) {
console.log(e);
}
}
function logger2(other) {
try {
let record = fs.readFileSync('log', 'utf-8');
record += `${other}\n`;
fs.writeFileSync('log', record);
} catch(e) {
console.log(e);
}
}
(async function() {
const instance = await phantom.create();
let page = await instance.createPage();
await page.on("onResourceRequested", function(requestData) {
console.info('Requesting', requestData.url)
// logger2(`Requesting ${requestData.url}`)
});
const status = await page.open(url);
console.log(status);
while(true) {
await reqBook(page);
logger2(` 重新加载...`)
await page.open(url);
}
// await instance.exit();
}());