-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
207 lines (188 loc) · 7.01 KB
/
search.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
function main() {
var program = require("commander");
var cheerio = require('cheerio');
var pretty = require("prettyjson")
var fs = require("fs");
var async = require("async");
var counter = 0;
var http = require('http');
var table = {};
var offset = 0;
var max = -1;
var query = "";
// Applies a function to a page
function getPage(fn, options, args) {
var callback = function(response) {
var body = '';
response.on('data', function(chunk) {body += chunk;});
//the whole response has been recieved, so we just print it out here
response.on('end', function() {
fn(body, args);}
);
response.on('error', function() {console.log("ERRORED OUT");});
}
http.request(options, callback).end();
}
function search(offset) {
var options = {
host: 'www.ratemyprofessors.com',
path: '/search.jsp?query='+query+'&queryoption=HEADER&stateselect=&country=&dept=&queryBy=&facetSearch=&schoolName=&offset=' + offset + '&max=20',
keepAlive: true
};
getPage(function(body) {
$ = cheerio.load(body);
// Init the max
if(max == -1){
var regex = /[0-9-]+/g;
var resultString = $(".result-count").last().text();
var filtered = resultString.match(regex);
max = Math.abs(parseInt(filtered[1])) - 20;
if(max <= -20){
return;
}
}
var professorsOnPage = $("li[class='listing PROFESSOR']");
async.each(professorsOnPage,
function(element,cb) {
element = $(element);
var profPage = element.children("a").attr('href')
var profName = element.find('.main').text();
var profOptions = {
host: 'www.ratemyprofessors.com',
path: profPage,
keepAlive: true
};
getPage(function(body) {
$ = cheerio.load(body);
var avgGrade = getGrade($);
var rating = getRating($);
var classes = getClasses($);
var scores = getScores($);
var reviews = getReviews($);
var metrics = {
"rating": rating,
"avgGrade": avgGrade,
"scores": scores
};
var prof = createProf(profName, classes, metrics, reviews);
prof["link"] = "www.ratemyprofessors.com" + profPage;
addProf(prof, table)
cb();
}, profOptions, offset);
},function(err){
if(err){
console.log(err());
return;
}
if(offset < max) {
startSearch();
}else{
if(program.save){
fs.writeFile(program.save,JSON.stringify(table));
}else{
console.log(pretty.render(table));
}
}
})
},
options);
}
function getClasses($) {
var classes = {};
$(".tftable").find(".name").each(function(i, elem) {
var _class = $(this).find(".response").text().toUpperCase().replace(/ /, '', 'g');
classes[_class] = _class;
});
return classes;
}
function getGrade($) {
var grade = $(".grade").slice(1).first().text();
return grade;
}
function getRating($) {
var grade = $(".grade").first().text();
return grade;
}
function getScores($) {
var sliders = $(".faux-slides").find(".rating-slider");
var scores = [];
sliders.each(function(i, ele) {
var label = $(this).find(".label").text();
var score = $(this).find(".rating").text();
scores.push(label + " " + score);
//console.log(label + " " + score);
});
return scores
}
function getReviews() {
var reviews = $(".tftable").find("tr").slice(1);
if (reviews.length == 0) {
console.log("quitting")
return;
}
var count = 3;
var i = 0;
var size = reviews.length;
var reviewsTable = {};
while (count > 0 && reviews.length != i) {
var query = $(reviews[i])
var _class = query.find(".name").find(".response").text().toUpperCase()
// Check if already been reviewed
if (reviewsTable[_class] == undefined && _class != '') {
var review = {};
var date = query.find(".date").text();
count--;
var textbook = query.find(".textbook-use").text();
var avg = 0;
query.find(".score").each(function(i, ele) {
var score = parseInt($(this).text());
avg += score;
});
avg = (avg / 3).toFixed(2);
var reviewText = query.find(".comments").find("p").text()
review["class"] = _class;
review["date"] = date;
review["textbook"] = textbook;
review["score"] = avg;
review["review"] = reviewText;
reviewsTable[_class] = review;
}
i++;
}
return reviewsTable;
}
function createProf(name, classes, metrics, reviews) {
return {
"name": name,
"metrics": metrics,
"classes": classes,
"reviews": reviews
}
}
function addProf(prof, table) {
if (table[prof.name] === undefined) {
table[prof.name] = [prof]
}
else {
table[prof.name].push(prof)
}
}
function startSearch(){
setTimeout(function(){
search(offset);
offset += 20;
},0);
}
program
.option("find [query]","Queries for a teacher/teachers in a organization")
.option("-s, --save [filename]","Saves the results to a json file")
.parse(process.argv);
if(program.find){
query = program.find.replace(/ /g,"+");
startSearch();
}
if (!process.argv.slice(2).length) {
program.outputHelp();
}
}
main();