-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
160 lines (142 loc) · 4.44 KB
/
api.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var phantom = require('phantom');
var cheerio = require('cheerio');
var http = require('http');
var URL = require('url');
var DEPOSIT = 25.00; // いくら入れたかは自己申告
var PORT = 3002; // このサーバーが待ち受けるポート
var frontObj = {
deposit: DEPOSIT,
difference: '',
totalDifference: '',
balance: DEPOSIT,
beforeBalance: DEPOSIT,
text: ''
};
config = {};
config.exitFlag = false;
http.createServer(function (req, res) {
if(req.method == 'GET') {
sendResponseToFront(req, res); // フロント部にレスポンスを返している。
}
}).listen(PORT);
console.log('this server is listening port: ' + PORT);
getBalance = function() {
config.exitFlag = true;
phantom.create(function(ph) {
ph.createPage(function(page) {
// ページが読み込まれたら page.onCallback を呼ぶ
page.set('onInitialized', function() {
page.evaluate(function() {
document.addEventListener('DOMContentLoaded', function() {
window.callPhantom('DOMContentLoaded');
}, false);
});
});
// ページが読み込まれたら登録した関数の配列を順次実行してくれるクラス
var funcs = function(funcs) {
this.funcs = funcs;
this.init();
};
funcs.prototype = {
// ページが読み込まれたら next() を呼ぶ
init: function() {
var self = this;
page.set('onCallback', function(data) {
if (data === 'DOMContentLoaded') self.next();
});
},
// 登録した関数の配列から1個取り出して実行
next: function() {
var func = this.funcs.shift();
if (func !== undefined) {
func();
} else {
page.set('onCallback', undefined);
}
}
};
// 順次実行する関数
new funcs([
function() {
// evaluateが返ってこなくなったときに処理を止めるsetTimeout
setTimeout(function() {
if (config.exitFlag){
console.log('exit this time');
ph.exit();
}
}, 1*60*1000);
page.open('https://accounts.pkr.com/LogOn.aspx?RedirectUrl=https://accounts.pkr.com/default.aspx'); // ログインページヘ
},
function() {
console.log('trying login....');
setTimeout(function() {
page.evaluate(function() {
document.getElementById('_ctl0_txtUserName').value = 'name';
document.getElementById('_ctl0_txtPassword').value = 'pass';
document.getElementById('_ctl0_btnLogOn').click();
});
}, 5*1000);
},
function() {
console.log('Lets get balance!');
setTimeout(function() {
page.evaluate(function() {
return document.getElementsByTagName('html')[0].innerHTML;
}, function(html) {
// cheerio でパースしてユーザ名とポイントを取得
var $ = cheerio.load(html);
var balance = $('a#_ctl0_lnkRealMoneyAccount').text();
balance = Number(balance.substr(1));
console.log('balance = ' + balance);
if (frontObj.balance != balance) { // 変更のないときは更新しない。
frontObj.beforeBalance = frontObj.balance;
frontObj.balance = balance;
frontObj.difference = Math.floor((balance - frontObj.beforeBalance)*100)/100;
if (frontObj.difference < 0) {
frontObj.difference = '-$' + frontObj.difference;
} else {
frontObj.difference = '+$' + frontObj.difference;
}
frontObj.totalDifference = Math.floor((balance - frontObj.deposit)*100)/100;
if (frontObj.totalDifference < 0) {
frontObj.totalDifference = '-$' + frontObj.totalDifference;
} else {
frontObj.totalDifference = '+$' + frontObj.totalDifference;
}
frontObj.text = '$'+frontObj.balance+'('+frontObj.difference+') total: '+frontObj.totalDifference;
}
// お忘れなきよう (-人-)
config.exitFlag = false;
ph.exit();
});
}, 2*1000);
}
]).next();
});
});
}
try{
getBalance();
} catch(e){
getBalance();
}
setInterval(function() {
try{
getBalance();
} catch(e){
getBalance();
}
}, 3*60*1000);
function sendResponseToFront(req, res) {
var query = URL.parse(req.url, true).query;
var data = JSON.stringify(frontObj);
var callback;
for (var key in query) {
var val = query[key];
if (key === 'callback' && /^[a-zA-Z]+[0-9a-zA-Z]*$/.test(val) ) {
callback = val;
}
}
res.writeHead(200, {'Content-Type':'application/json; charset=utf-8'});
res.end( callback ? callback + "(" + data + ");" : data );
}