-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchcraigslist.js
179 lines (148 loc) · 4.88 KB
/
searchcraigslist.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
var request = require('request');
var cheerio = require('cheerio');
var _ = require("underscore");
var Promise = require('bluebird');
//promisifies http request method to make code clearer
var request = Promise.promisify(request)
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.split(search).join(replacement);
};
module.exports.getLinks = function(url){
return new Promise(function(resolve, reject){
//constructs search url based on city, apartment type
var listinglinks = []; //array to hold links to listings returned from search
var requestbodies = []; //array to hold request bodies
var results = []; ///array to hold final results (listings plus request bodies)
request(url).then(function(req){
var $ = cheerio.load(req.body); //cheerio is a library that parses an html string to return a manipulable DOM
$('.hdrlnk').each(function(index,link) {
listinglinks.push('http://sfbay.craigslist.org' + link.attribs.href)
})
})
.then(function(){
listinglinks.forEach(function(link, index){
requestbodies[index] = request(link)
})
Promise.all(requestbodies).then(function(){
requestbodies.forEach(function(promise,index){
promise.then(function(req){
$ = cheerio.load(req.body);
var postbodytext = $('#postingbody').html();
//var postbodytext = decodeURIComponent($('#postingbody').html());
var posttitle = $('#titletextonly').html();
var postprice = $('.price').html()
var map = $('#map')
var postlatitude = map.attr('data-latitude');
var postlongitude = map.attr('data-longitude');
results[index] = {link:listinglinks[index],title:posttitle,price:postprice,lat:postlatitude,lon:postlongitude,text: postbodytext}
})
})
return Promise.all(results).then(function(){
getSignificantWords(results);
resolve(results);
})
})
})
})
}
module.exports.getLinksCallback = function(city, apartmenttypekey, cb){
//constructs search url based on city, apartment type
var url = 'http://' + city + '.craigslist.org/search/' + apartmenttypekey;
var listinglinks = []; //array to hold links to listings returned from search
var requestbodies = []; //array to hold request bodies
var results = []; ///array to hold final results (listings plus request bodies)
request(url).then(function(req){
var $ = cheerio.load(req.body); //cheerio is a library that parses an html string to return a manipulable DOM
$('.hdrlnk').each(function(index,link){
listinglinks.push('http://'+city + '.craigslist.org/' + link.attribs.href)
})
})
.then(function(){
listinglinks.forEach(function(link,index){
requestbodies[index] = request(link)
})
Promise.all(requestbodies).then(function(){
requestbodies.forEach(function(promise,index){
promise.then(function(req){
$ = cheerio.load(req.body);
var postbodytext = decodeURIComponent($('#postingbody').html());
var posttitle = $('#titletextonly').html();
var postprice = $('.price').html()
var map = $('#map')
var postlatitude = map.attr('data-latitude');
var postlongitude =map.attr('data-longitude');
results[index] = {link:listinglinks[index],title:posttitle,price:postprice,lat:postlatitude,lon:postlongitude,text: postbodytext}
})
})
return Promise.all(results).then(function(){
cb(results);
})
})
})
}
var getSignificantWords = function(results){
console.log('GOT SIGNIFICANT WORDS!');
results.forEach(function(obj){
var newtext = obj.text.replace(/\n/g,' ').replaceAll("&"," ").replaceAll('<br>'," ").replaceAll(''',"'")
obj.text = newtext;
obj.wordcount = countWords(obj.text)
})
//console.log(res);
var commonwords = {};
results.forEach(function(obj){
var wordcount = obj.wordcount;
for(var word in wordcount){
if(word in commonwords){
commonwords[word] = commonwords[word] + wordcount[word];
} else {
commonwords[word] = wordcount[word];
}
}
})
results.forEach(function(obj){
var wordcount = obj.wordcount;
for(var word in wordcount){
obj['wordcount'][word] = wordcount[word]/commonwords[word];
}
})
results.forEach(function(obj){
var common = [];
for(var word in obj.wordcount){
common.push([word,obj.wordcount[word]]);
}
common.sort(function(a,b){
if(a[1]>b[1]){
return 1;
}
if(a[1]<b[1]){
return -1
}
return 0;
})
obj['common'] = common;
obj['sig'] = obj['common'].slice(-15);
})
}
//Helper functions for finding significant words in each listing
function countWords(string){
var count = {};
var arr = string.split(' ');
arr.forEach(function(word){
if (word in count){
count[word] = count[word] + 1;
} else {
count[word] = 1;
}
})
return count;
}
function objFilter(obj,cb){
var results = {};
for (var key in obj){
if (cb(obj[key],key)){
results[key] = obj[key]
}
}
return results;
}