-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl-crawler.js
More file actions
161 lines (134 loc) · 4.24 KB
/
cl-crawler.js
File metadata and controls
161 lines (134 loc) · 4.24 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
var url = require("url");
var Crawler = require('./crawler/crawler.js').Crawler;
var request = require('request');
var fs = require('fs');
//Some config
var cacheFileName = 'coordCache.ex'
, ccfHandle = fs.openSync(cacheFileName, "w", 0666)
, baseURL = "http://montreal.en.craigslist.ca/apa/"
, coordsCacheArray = []
//This is used to keep track of wether we're finished
//geocoding everything in the queue.
, geocoded = 0
, pages = 0;
function writeCoordCache() {
var str = "var coordCache = ";
str += JSON.stringify(coordsCacheArray);
str += ";\n";
console.log('started writing');
fs.write(ccfHandle, str);
console.log('finished writing');
fs.close(ccfHandle);
}
//Geocoder function
function geoCode(clPost) {
//geocoding API url + dev key
var base = "http://where.yahooapis.com/geocode?appid=YOUR_YAHOO_API_KEY&flags=CJ&q=";
var queryURL = base + clPost.gqAddress;
request({uri: queryURL, method: 'GET'}, function (error, response, body) {
//Sink failed queries to the geocoding api.
try {
response = body && JSON.parse(body);
}
catch (e) {
console.log('Error parsing body of post');
// response = "";
return;
}
var results = response.ResultSet.Results;
if (typeof results !== "undefined" && results !== null) {
var results = results[0]
, coords = { lat : results.latitude
, lng : results.longitude
};
clPost.coords = coords;
coordsCacheArray.push(clPost);
checkState();
}
else {
console.log("Geocoding failed!");
console.log(error);
checkState();
}
console.log(geocoded + ": " + clPost.clURL);
});
}
function checkState() {
geocoded--;
console.log("Geocoded: " + geocoded);
if (!geocoded) {
console.log("Pages: " + pages);
if (!pages) writeCoordCache();
}
}
var processPost = function processPostBody() {
var $ = this.jQuery
, addressLink = $("small > a").attr("href")
, postTitle = $("h2").text()
, rent = postTitle.match(/(\$\d{3,})/)
, bedrooms = postTitle.match(/(\dbr)/)
, postBody = null
, qAddress = ""
, imageList = null
, clPostID = $("span.postingidtext").text().split(":")[1].trim()
, clPostObj = { };
imageList = [];
//Collect all the image links.
$(".iw #iwt .tn a").each(function(index, elem) {
imageList.push($(elem).attr("href"));
});
//remove image section from DOM.
$(".iw").remove();
$("#userbody script").remove();
postBody = $("#userbody").html();
if (typeof addressLink !== 'undefined' && addressLink !== null) {
//Use url.parse to extract the query portion of the url and split based on loc argument.
//We're not decoding the url here because we will use it shortly for geocoding
//the address.
qAddress = url.parse(addressLink, true).search.split('loc%3A+')[1];
clPostObj.title = postTitle;
clPostObj.body = postBody;
clPostObj.clURL = baseURL + clPostID + ".html";
clPostObj.gqAddress = qAddress;
clPostObj.bedrooms = bedrooms ? parseInt(bedrooms[0].replace("br", "")) : 2;
clPostObj.rent = rent ? parseInt(rent[0].replace("$", "")) : 700;
clPostObj.imageList = imageList;
geoCode(clPostObj);
}
else {
checkState();
console.log('address link not found');
console.log(geocoded + ":" + baseURL + clPostID + ".html");
}
}
var pageCrawler = new Crawler({ callback: processPost });
var clCrawler = new Crawler({
callback: function () {
if (pages > 0) {
var $ = this.jQuery
, paras = $("p")
, links = []
, linkText
console.log("Gathering links to crawl.\n");
$.each(paras, function (index, value) {
var pLink = $(value).children("a");
if (pLink.length !== 0) {
linkText = pLink.attr("href");
links.push(linkText);
}
});
console.log("Crawling pages.");
geocoded = links.length + geocoded;
pageCrawler.enqueue(links);
console.log("Queueing next page for link gathering.\n");
var nextPageLink = $("#nextpage").find("a").attr("href");
nextPageLink && clCrawler.enqueue(baseURL + nextPageLink);
pages--;
}
else {
return;
}
}
});
pages = 4;
clCrawler.enqueue(baseURL);