-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
236 lines (200 loc) · 7.46 KB
/
main.js
File metadata and controls
236 lines (200 loc) · 7.46 KB
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
const cheerio = require('cheerio')
const iconv = require('iconv-lite')
const request = require("request")
const fs = require("fs")
const districts = ["HK", "KL", "NE", "NW"]
const urlLevel1 = "http://www1.centadata.com/paddresssearch1.aspx?type=district16&code="
const urlLevel2 = "http://www1.centadata.com/paddresssearch1.aspx"
const urlLevel3 = "http://www1.centadata.com/"
const INDENT_SYMBOL = "\t"
const INDENT_LEVEL = 1
class Writer {
constructor(writeMode){
this.writeMode = writeMode? writeMode: Writer.writeMode.ToConsole
if (writeMode == Writer.writeMode.ToFile){
this.message = ""
} else if (writeMode == Writer.writeMode.ToConsole) {
} else {
throw "Writer mode cannot be set to values other than 1 or 2"
}
}
static get writeMode(){
return {
ToFile: 1,
ToConsole: 2
}
}
print(str){
if (this.writeMode == Writer.writeMode.ToFile){
this.message += str + "\n"
} else if (this.writeMode == Writer.writeMode.ToConsole){
console.log(str)
}
}
flush(path){
if (this.writeMode == Writer.writeMode.ToFile){
fs.writeFileSync(path, this.message, (err) => {
console.err(err)
})
console.log("File written to " + path)
} else {
throw "Writer not in file mode"
}
}
}
function getHttp(url){
return new Promise((resolve, reject) => {
request({
url: url,
encoding: null}, // Important
function(err, resp, body){
if (err != null){
return reject(err)
}
resolve(body)
}
)
})
}
function extractParams(paramsText){
let params = []
while (paramsText.match(/'([^']+)'/) != null && paramsText.match(/'([^']+)'/).length > 0){
let matches = paramsText.match(/'([^']+)'/)
params.push(matches[1])
paramsText = paramsText.replace(matches[0], "")
}
return params
}
// HK, KL etc
class Level1Manager{
constructor(district){
this.district = district
this.url = urlLevel1 + district
this.children = []
}
async getChildren(){
var body = await getHttp(this.url).catch((err) => {
console.error(err)
})
var queue = []
var str = iconv.decode(new Buffer(body), "big5")
var $ = cheerio.load(str,
{ decodeEntities: false })
$(".tbreg1>tbody>tr").each((i,e) => {
let regionName = $(e).find(".tdreg1cname span").text()
let paramsText = $(e).attr("onclick")
let params = extractParams(paramsText)
let level2Manager = new Level2Manager(params[0],params[1],params[2],params[3],params[4],regionName)
this.children.push(level2Manager)
queue.push(level2Manager.getChildren())
})
await Promise.all(queue)
return this
}
print(indent = 0){
writer.print(INDENT_SYMBOL.repeat(indent) + "Region: " + this.district)
for (var i = 0; i<this.children.length; i++){
this.children[i].print(indent + INDENT_LEVEL)
}
}
}
// Kwun Tong, Fanling etc
class Level2Manager {
constructor(atype,acode,acode2,ainfo,apage, regionName){
this.url = Level2Manager.makeUrl(atype, acode, acode2, ainfo,apage)
this.regionName = regionName
this.children = []
this.pageParams = [atype,acode,acode2,ainfo,apage]
}
static makeUrl(atype,acode,acode2,ainfo,apage){
if(apage < 0 || apage > 1000 || apage =='undefined')apage=0;
return urlLevel2 + "?type="+atype+"&code="+acode+"&info=" + ainfo + "&code2="+acode2 + "&page="+apage;
}
async getChildren(){
var toLoop = true
let maxPage = 100
var toLoadPage = 0
while(toLoop && toLoadPage <= maxPage){
var body = await getHttp(this.url).catch((err) => {
console.error(err)
})
var str = iconv.decode(new Buffer(body), "big5")
var $ = cheerio.load(str,
{ decodeEntities: false })
// next page button
var $nextBtn = $("td[align=right] a").last()
if ($nextBtn.html() == null || !$nextBtn.html().includes("???")){
toLoop = false
} else {
let paramsText = $nextBtn.attr("href")
let params = extractParams(paramsText)
toLoadPage = parseInt(params[2])
this.url = Level2Manager.makeUrl(this.pageParams[0], this.pageParams[1], this.pageParams[2], this.pageParams[3], toLoadPage)
}
$(".tbscp1>tbody>tr").each((i,e) => {
let $el = $(e)
let estateName = $el.find(".tdscp1cname span").text()
let addr = $el.find(".tdscp1addr").text()
let bldAge = $el.find(".tdscp1bldgage").text()
let usablePrice = $el.find(".tdscp1Suprice").text()
let paramsText = $el.attr("onclick")
let params = extractParams(paramsText)
let centaCode = params[1]
let level3Manager = new Level3Manager(
params[0],
params[1],
params[2],
params[3],
null,
estateName,
addr,
bldAge,
usablePrice
)
this.children.push(level3Manager)
})
}
return this
}
print(indent = 0){
writer.print(INDENT_SYMBOL.repeat(indent) + "District: " + this.regionName)
for (var i = 0; i<this.children.length; i++){
this.children[i].print(indent + INDENT_LEVEL)
}
}
}
// represent each xx Garden, xx Court
class Level3Manager {
constructor(atype,acode,ainfo,apage, aref, estateName, addr, bldAge, usablePrice){
this.url = Level3Manager.makeUrl(atype, acode, ainfo, apage,aref)
this.estateName = estateName
this.addr = addr
this.bldAge = bldAge
this.usablePrice = usablePrice
}
static makeUrl(atype,acode,ainfo,apage, aref){
if(apage < 0 || apage > 1000 || apage =='undefined')apage=0;
return urlLevel3 + "ptest.aspx?type="+atype+"&code="+acode+"&info=" + ainfo + "&page="+apage + "&ref=" + aref;
}
print(indent = 0){
writer.print(INDENT_SYMBOL.repeat(indent) + "Property: " + this.estateName)
writer.print(INDENT_SYMBOL.repeat(indent + INDENT_LEVEL) + "Address: " + this.addr)
writer.print(INDENT_SYMBOL.repeat(indent + INDENT_LEVEL) + "Building Age: " + this.bldAge)
writer.print(INDENT_SYMBOL.repeat(indent + INDENT_LEVEL) + "Recent price per ft: " + this.usablePrice)
}
}
var managers = []
var queue = []
var writer = new Writer(Writer.writeMode.ToFile)
for (var i = 0; i< districts.length; i++){
let manager = new Level1Manager(districts[i])
queue.push(manager.getChildren())
managers.push(manager)
}
Promise.all(queue).then((values) => {
for (idx in managers){
let manager = managers[idx]
manager.print(0)
}
writer.flush("./property-list.txt")
})