-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataSource.js
369 lines (321 loc) · 10.7 KB
/
dataSource.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
var http = require('http');
var fs = require('fs');
var path = require('path');
var config = require("./config");
var logger = require("./logger");
var qs = require('querystring');
qs.unescape = function (str){
return unescape(str);
};
qs.escape = function (str){
return escape(str);
};
module.exports = dataSource;
config.server = config.hkgoldenServer();
config.host = "forum" + config.server + ".hkgolden.com";
config.prefix = "http://" + config.host + "/";
function dataSource(){
}
//common function for requesting hkgolden
var request = function (url, callback, method, headers, data){
if (! method){
method = "GET";
}
if (! headers){
headers = {
"Host": config.host,
"Connection": "close"
};
}
if (url.substr(0, config.prefix.length) != config.prefix){
url = config.prefix + url;
}
var hkg = http.createClient(80, config.host);
var hkgReq = hkg.request(method, url, headers);
hkgReq.addListener('response', function(res) {
res.setEncoding("binary");
var temp = "";
var cLength = 0;
res.addListener('data', function(chunk) {
temp += chunk;
});
res.addListener('end', function() { //full packet received
if (typeof callback == "function"){
callback(temp, res.headers);
}
});
});
if (data != undefined){
hkgReq.end(data, "binary");
}else{
hkgReq.end();
}
};
dataSource.prototype.getQuote = function (id, rid, callback){
var reqStr = "{'s_MessageID':" + id + ",'s_ReplyID':" + rid + "}";
var headers = {
"Host": config.host,
"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.9) Gecko/2008061015 Firefox/3.0",
"Referer": config.prefix + "default.aspx",
"Content-Type": "application/json; charset=utf-8",
"Content-Length": reqStr.length,
"Connection": "close"
};
request("MessageFunc.asmx/quote_message", function (temp, resHeaders){
if (callback != undefined){
var ret = "";
try{
var tempObj = JSON.parse(temp);
ret = tempObj.d;
}catch (e){
ret = temp.substr(6, temp.length - 8);
var map = {
"\\\\u003c": "<",
"\\\\u003e": ">",
"\\\\u0026": "&",
"\\\\u0027": "'",
"\\\\u0022": "\"",
"\\\\\"": "\"",
"\\\\r\\\\n": "\r\n",
"\\\\r": "\r",
"\\\\n": "\n",
"\\\\\\\\": "\\"
};
for (var x in map){
ret = ret.replace(new RegExp(x, "gm"), map[x]);
}
}
callback(ret);
}
}, "POST", headers, reqStr);
};
dataSource.prototype.submitPost = function (type, title, content, userInfo, replyTo, callback){
var gurl;
if (replyTo) {
gurl = "post.aspx?mt=Y&ft=" + type + "&rid=0&id=" + replyTo;
} else {
gurl = "post.aspx?mt=N&ft=" + type;
}
var headers1 = {
"Host": config.host,
"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.9) Gecko/2008061015 Firefox/3.0",
"Referer": config.prefix + "login.aspx",
"Cookie": userInfo.session,
"Connection": "close"
};
request(gurl, function (temp, resHeaders){
var inputPattern = /<input name="([^"]*)"/g;
var inputPattern2 = /<input.*name="([^"]*)".*value="([^"]*)"/g;
var formData = {};
var matches;
while (matches = inputPattern.exec(temp)){
formData[matches[1]] = "";
}
while (matches = inputPattern2.exec(temp)){
formData[matches[1]] = matches[2];
}
formData['messagetype'] = 'Y';
formData['ctl00$ContentPlaceHolder1$ddl_forum_type'] = type;
formData['ctl00$ContentPlaceHolder1$messagesubject'] = title;
formData['ctl00$ContentPlaceHolder1$messagetext'] = content;
formData['ctl00$ContentPlaceHolder1$btn_Submit.x'] = 41;
formData['ctl00$ContentPlaceHolder1$btn_Submit.y'] = 9;
formData['ctl00$ContentPlaceHolder1$btn_Submit'] = 'I1';
var postData = qs.stringify(formData);
var headers2 = {
"Host": config.host,
"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.9) Gecko/2008061015 Firefox/3.0",
"Referer": config.prefix + gurl,
"Content-Length": postData.length,
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": userInfo.session,
"Connection": "close"
};
request(gurl, function (temp2, resHeaders2){
if (callback != undefined){
callback();
}
}, "POST", headers2, postData);
}, "GET", headers1);
};
dataSource.prototype.login = function (email, password, callback){
logger("Trying to login");
var session = "";
var headers1 = {
"Host": config.host,
"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.9) Gecko/2008061015 Firefox/3.0",
"Referer": config.prefix + "login.aspx"
};
request("login.aspx", function (temp, resHeaders){
try{
var inputPattern = /<input name="([^"]*)"/g;
var inputPattern2 = /<input.*name="([^"]*)".*value="([^"]*)"/g;
var formData = {};
var matches;
while (matches = inputPattern.exec(temp)){
formData[matches[1]] = "";
}
while (matches = inputPattern2.exec(temp)){
formData[matches[1]] = matches[2];
}
formData["ctl00$ContentPlaceHolder1$txt_email"] = email;
formData["ctl00$ContentPlaceHolder1$txt_pass"] = password;
formData["ctl00$ContentPlaceHolder1$cb_remember_login"] = "on";
var sessionPattern = /(ASP\.NET_SessionId[^;]*);/g;
var cookies = resHeaders["set-cookie"];
for (var i = 0, l = cookies.length; i < l; i++){
var cookie = cookies[i];
if (matches = sessionPattern.exec(cookie)){
session = matches[1];
}
}
var postData = qs.stringify(formData);
var headers2 = {
"Host": config.host,
"User-Agent": "Mozilla/5.0 (X11; U; Linux i686; zh-TW; rv:1.9) Gecko/2008061015 Firefox/3.0",
"Referer": config.prefix + "login.aspx",
"Content-Length": postData.length,
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": session,
"Connection": "close"
};
request("login.aspx", function (temp2, resHeaders){
fs.writeFile("debug", temp2, "binary");
var cookies2 = resHeaders["set-cookie"];
if (cookies2){
for (var i = 0, l = cookies2.length; i < l; i++){
var cookie = cookies2[i];
var cookiePattern = /^([^;]*);/;
var match = cookiePattern.exec(cookie);
session += ";" + match[1];
}
}else{
callback("LoginError");
return;
}
if (typeof callback == "function"){
callback(session);
}
}, "POST", headers2, postData);
}catch (e){
callback("LoginError");
return;
}
}, "POST", headers1);
};
dataSource.prototype.topics = function (type, page, callback){
request("topics.aspx?type=" + type + "&page=" + page, function (temp){
var topicPattern = /<a href=\"view\.aspx\?type=[A-Z]+&message=([0-9]+)\">([^<]*)<\/a>/g;
var matches;
var topics = [];
while (matches = topicPattern.exec(temp)){
var topic = {
id: matches[1],
title: matches[2]
};
topics.push(topic);
}
var authorPattern = /<tr id=\"Thread_No[0-9]*\" userid=\"([0-9]+)\" username=\"(.*)\">/g;
var i = 0;
while (matches = authorPattern.exec(temp)){
topics[i].authorID = matches[1];
topics[i].authorName = matches[2];
i++;
}
var lastReplyPattern = /<td style=\"width: 18%;[^>]*>.*([^<]*)<span style=\"color: #800000\">(.*)<\/span><\/td>/g;
i = 0;
while (matches = lastReplyPattern.exec(temp)){
var d = new Date(matches[1] + " " + matches[2]);
d.setTime(d.getTime() - 8 *3600 * 1000); //reset GMT
topics[i].lastReplyTime = (+ d);
i++;
}
var replyNumPattern = /<td style=\"width: 6%;[^>]*>[^0-9]*([-0-9,]+)<\/td>/g;
i = 0;
while (matches = replyNumPattern.exec(temp)){
topics[i].totalReplies = parseInt(matches[1].replace(",", ""));
matches = replyNumPattern.exec(temp);
topics[i].rating = parseInt(matches[1].replace(",", ""));
i++;
}
if (typeof callback == "function"){
callback(topics);
}
});
};
dataSource.prototype.post = function (id, page, callback){
request("view.aspx?message=" + id + "&page=" + page, function (temp){
var post = {};
try{
var topicPattern = /<td class=\"repliers_header\">[.\n\r\t ]*<div style=\"float: left\">([^<]*)<\/div>/g;
var matches;
matches = topicPattern.exec(temp);
post.title = matches[1];
var replyNumPattern = /<strong> ([0-9]+)<\/strong>/g;
matches = replyNumPattern.exec(temp);
post.replyNum = matches[1];
var posts = [];
var authorInfoPattern = /<tr id=\"Thread.*userid=\"([0-9]*)\" username="([^>]*)">/g;
var genderPattern = /<a href=\"javascript: ToggleUserDetail.*color: #([0-9A-F]*);">/g;
var postDatePattern = /<span style=\"font-size: 12px; color:gray;\">[^0-9]*([^<]*)<\/span>/g;
var contentPattern = /<table class=\"repliers_right\" cellpadding=\"0\" cellspacing=\"0\">[.\n\r\t ]*<tbody><tr>[.\n\r\t ]*<td valign=\"top\">([\W\w]*?)<\/td>/gm;
var ridPattern = /href=\"post\.aspx\?mt=Y&rid=([0-9]*)&id=.*\">/g;
var breadcrumbPattern = /<div style=\"padding: 8px 0px 0px 0px;\">[\w\W\n\r\t]*?<a href=\"topics\.aspx\?type=([a-zA-Z]+)\">([^<]*)<\/a>[\w\W\n\r\t]*?<\/div>/gm;
matches = breadcrumbPattern.exec(temp);
post.category = matches[1];
post.categoryName = matches[2];
var sc = 0;
while (matches = authorInfoPattern.exec(temp)){
var postData = {
authorID: parseInt(matches[1]),
authorName: matches[2]
};
matches = genderPattern.exec(temp);
postData.authorGender = (matches[1] == "0066FF" ? "M" : "F");
matches = postDatePattern.exec(temp);
var d = new Date(matches[1]);
d.setTime(d.getTime() - 8 *3600 * 1000); //reset GMT
postData.postDate = (+d);
matches = contentPattern.exec(temp);
postData.content = matches[1];
matches = ridPattern.exec(temp);
postData.rid = matches[1];
posts.push(postData);
}
post.posts = posts;
}catch (e){
//parse error
}
if (typeof callback == "function"){
callback(post);
}
});
};
dataSource.prototype.channels = function (callback){
request("", function (temp){
var channels = [];
try{
var channelPattern = /<a class=\"BoxLink2\" href=\"\/topics\.aspx\?type=([A-Z]+)\">(.*?)<\/div>/g;
while (matches = channelPattern.exec(temp)){
var namePattern = /^([^<]*)/g;
var tagPattern = /<a[^>]*>(.*?)<\/a>/g;
var channel = {
id: matches[1]
};
var content = matches[2];
matches = namePattern.exec(content);
channel.name = matches[1];
channel.tags = [];
while (tagMatches = tagPattern.exec(content)){
channel.tags.push(tagMatches[1]);
}
channels.push(channel);
}
}catch (e){
//parse error
}
if (typeof callback == "function"){
callback(channels);
}
});
};