-
Notifications
You must be signed in to change notification settings - Fork 3
/
quiz.js
373 lines (319 loc) · 11.3 KB
/
quiz.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
370
371
372
373
var cheerio = require('cheerio');
var request = require('request');
var Templater = require('./templater');
var Markov = require('./markov');
var fs = require("fs");
var findPictures = require("./flickr.js");
var allShows = JSON.parse(fs.readFileSync("allShows.json", 'utf8')).data;
var maxPhotos = 7;
function getQuizPhotos(subject, callback) {
findPictures(subject, function(photos) {
if (photos.length > maxPhotos) {
photos = photos.slice(0, maxPhotos);
}
callback(photos);
});
}
function assignAuthor(quiz, author){
quiz.username = author.username;
quiz.authorName = author.name;
quiz.profileUrl = "/users/" + quiz.username;
quiz.authorProfilePicture = author.authorProfilePicture;
quiz.authorTitle = author.authorTitle;
}
function rand(min, max){
return Math.floor(Math.random() * (max-min))+min;
}
module.exports = new function () {
// var scrapeImdb = function(callback) {
// request('http://www.imdb.com/search/title?num_votes=5000,&sort=user_rating,desc&title_type=tv_series', function(err, res, html) {
// if (err) return console.log(err);
// var $ = cheerio.load(html);
// var titles = $('.title a');
// var links = [];
// var coverPhotoUrl;
// for (var i = 0; i < titles.length; i++) {
// links.push({url: $(titles.get(i)).attr("href"), title: $(titles.get(i)).text().trim()});
// }
// links = links.filter(function(el) {
// return el.url.indexOf("/title/") !== -1 && el.url.indexOf("/vote?v") === -1;
// });
// var show = links[Math.floor((Math.random() * links.length))];
// request('http://imdb.com' + show.url, function (err, res, html) {
// var $ = cheerio.load(html);
// var characters = $('.character a');
// var links = [];
// var returnLinks = [];
// var showCover = $('.image a');
// // showCover.filter(function(el) { el. });
// console.log("Show cover", $(showCover).attr("href"));
// request('http://imdb.com' + $(showCover).attr("href"), function (err, res, html) {
// var temp = cheerio.load(html); // crashed here, "Cannot read property 'parent' of undefined" with http://www.imdb.com/list/ls073461000/?ref_=tt_tt_edw_OscarNom_i_1#1
// coverPhotoUrl = temp('#primary-img').attr("src");
// for (var i = 0; i < characters.length; i++) {
// if ($(characters.get(i)).attr("href"))
// links.push({url: $(characters.get(i)).attr("href"), name: $(characters.get(i)).text().trim()});
// }
// // console.log(links);
// var index = 0;
// function getImage(callback){
// if (index < links.length) {
// request('http://imdb.com' + links[index].url, function (err, res, html) {
// var $ = cheerio.load(html);
// var photos = $("a[name='headshot']");
// if ($(photos).attr("href")) {
// request('http://imdb.com' + $(photos).attr("href"), function (err, res, html) {
// var $ = cheerio.load(html);
// var photo = $('#primary-img');
// links[index].src = photo.attr('src');
// returnLinks.push(links[index]);
// index++;
// getImage(callback);
// });
// } else {
// index++;
// getImage(callback);
// }
// });
// } else {
// callback({title: show.title, coverPhoto: coverPhotoUrl, links: returnLinks});
// }
// }
// getImage(callback);
// });
// for (var j = 0; j < links.length; j++) {
// request('http://imdb.com' + links[j].url, function (err, res, html) {
// var $ = cheerio.load(html);
// var photos = $("a[name='headshot']");
// console.log(links[j].name + ": " + $(photos.first()).attr("href"));
// });
// }
//callback({showTitle: show.title, characters:})
// });
// });
// };
var scrapeImdb = function(callback) {
var show = allShows[rand(0, allShows.length)];
show.coverPhoto = "/imdb/coverPhoto-"+show.title.replace(/ /g, "_")+".jpg";
show.links.map(function(v) {
v.src = "/imdb/"+show.title.replace(/ /g, "_")+"-"+v.name.replace(/ /g, "_") + ".jpg";
});
callback(show);
};
function create(author, templater, callback) {
scrapeImdb(function(imdb) {
var quiz = {};
quiz.title = generateQuizTitle(imdb.title, templater);
quiz.articleName = quiz.title.toLowerCase().replace(/[\"\'\?]/g, "").replace(/ /g, "-");
quiz.isQuiz = true;
quiz.possibleResults = [];
var markov = new Markov();
markov.pretrainPersonality();
markov.pretrainHoroscope();
if (imdb.links.length === 0) return create(author, templater, callback);
for (var i = 0; i < imdb.links.length; i++) {
quiz.possibleResults.push({
imageUrl: imdb.links[i].src,
title: "You Got "+imdb.links[i].name+"!",
body: markov.generate(rand(10,15), 4)});
}
quiz.previewUrl = imdb.coverPhoto;
quiz.elements = [];
assignAuthor(quiz, author);
var max = rand(5, 10);
var func = null;
for (var i = 0; i < max; i++) {
var r = rand(0, 100);
if(r > 33) {
if(r > 66) {
func = generatePhotoQuestion;
} else {
func = generateWordQuestion;
}
} else {
func = generateYesNoQuestion;
}
func(templater, function(q){
quiz.elements.push(q);
if(max <= quiz.elements.length) {
quiz.responses = rand(10, 800);
quiz.subtitle = "";
quiz.timestamp = Date.now();
quiz.url = "/quizzes/" + quiz.articleName;
shuffle(quiz.elements);
return callback(quiz);
}
});
}
});
}
/**
* Shuffles a given given inplace.
* @param {[a']} coll array to be shuffled
* @return {[a']} shuffled array
*/
function shuffle(coll){
for(var j, x, i = coll.length; i; j = Math.floor(Math.random() * i), x = coll[--i], coll[i] = coll[j], coll[j] = x);
return coll;
}
this.create = create;
var generatePhotoQuestion = function(templater, callback) {
var question = {};
templater.loadQuizPhotoQuestions();
question.questionType = "openEnded";
var nameObj = templater.generateName();
getQuizPhotos(nameObj.subj, function(quizPhotos) {
question.imageUrl = quizPhotos.pop();
question.possibleAnswers = quizPhotos.map(function(obj) {return {url: obj}});
question.title = nameObj.title;
callback(question);
});
};
var generateYesNoQuestion = function(templater, callback) {
var question = {};
templater.loadQuizYesNoQuestions();
question.questionType = "yesNo";
var nameObj = templater.generateName();
question.title = nameObj.title;
callback(question);
};
var generateWordQuestion = function(templater, callback) {
var question = {};
if(rand(0, 100) < 50) {
templater.loadQuizNounQuestions();
question.possibleAnswers = [];
for (var i = 0; i < 6; i++) {
question.possibleAnswers.push({
text: templater.getRand('noun')
});
}
} else {
templater.loadQuizPeopleQuestions();
question.possibleAnswers = [];
for (var i = 0; i < 6; i++) {
question.possibleAnswers.push({
text: templater.getRand('people')
});
}
}
question.questionType = "openEnded";
var nameObj = templater.generateName();
question.title = nameObj.title;
callback(question);
};
var generateQuizTitle = function(showTitle, templater) {
templater.loadQuizTitles();
templater.loadKey("showTitle", [showTitle]);
return templater.generateName().title;
};
};
var RUNME = function(start, toScrape, callback) {
request(start, function(err, res, html) {
if (err) return console.error(err);
var $ = cheerio.load(html);
var titles = $(toScrape);
var links = [];
var coverPhotoUrl;
for (var i = 0; i < titles.length; i++) {
links.push({url: $(titles.get(i)).attr("href"), title: $(titles.get(i)).text().trim()});
}
links = links.filter(function(el) {
return el.url.indexOf("/title/") !== -1 && el.url.indexOf("/vote?v") === -1;
});
// var show = links[Math.floor((Math.random() * links.length))];
var allShows = [];
var loop = function(i) {
console.log(i + 1, "/", links.length);
if(i >= links.length) {
return callback(allShows);
}
var show = links[i];
request('http://imdb.com' + show.url, function (err, res, html) {
var $ = cheerio.load(html);
var characters = $('.character a');
var links = [];
var returnLinks = [];
var showCover = $('.image a');
// showCover.filter(function(el) { el. });
console.log("Show cover", $(showCover).attr("href"));
request('http://imdb.com' + $(showCover).attr("href"), function (err, res, html) {
if(!html) {
return loop(i+1);
}
var temp = cheerio.load(html); // crashed here, "Cannot read property 'parent' of undefined" with http://www.imdb.com/list/ls073461000/?ref_=tt_tt_edw_OscarNom_i_1#1
coverPhotoUrl = temp('#primary-img').attr("src");
for (var j = 0; j < characters.length; j++) {
if ($(characters.get(j)).attr("href"))
links.push({url: $(characters.get(j)).attr("href"), name: $(characters.get(j)).text().trim()});
}
// console.log(links);
var index = 0;
function getImage(callback){
if (index < links.length) {
request('http://imdb.com' + links[index].url, function (err, res, html) {
var $ = cheerio.load(html);
var photos = $("a[name='headshot']");
if ($(photos).attr("href")) {
request('http://imdb.com' + $(photos).attr("href"), function (err, res, html) {
var $ = cheerio.load(html);
var photo = $('#primary-img');
links[index].src = photo.attr('src');
returnLinks.push(links[index]);
index++;
getImage(callback);
});
} else {
index++;
getImage(callback);
}
});
} else {
var s = {title: show.title, coverPhoto: coverPhotoUrl, links: returnLinks};
fs.appendFile("allMovies.json", JSON.stringify(s), function() {
console.log("DONE ", index);
});
allShows.push(s);
// request(s.coverPhoto).pipe(fs.createWriteStream("./imdb/coverPhoto-"+s.title+".jpg")).on('close', function() {
// console.log("Done Cover");
// });
// s.links.map(function(v) {
// request(v.src).pipe(fs.createWriteStream("./imdb/"+s.title+"-"+v.name + ".jpg")).on('close', function() {
// console.log("done");
// });
// });
loop(i+1);
}
}
getImage(callback);
});
// for (var j = 0; j < links.length; j++) {
// request('http://imdb.com' + links[j].url, function (err, res, html) {
// var $ = cheerio.load(html);
// var photos = $("a[name='headshot']");
// console.log(links[j].name + ": " + $(photos.first()).attr("href"));
// });
// }
//callback({showTitle: show.title, characters:})
});
};
loop(0);
});
};
// RUNME("http://www.imdb.com/chart/top?ref_=nv_ch_250_4", ".titleColumn a", function(arr) {
// });
// fs.readFile("allShows.json", 'utf8', function(err, data) {
// var arr = JSON.parse(data).data;
// var cur = 0;
// var max = arr.length;
// arr.map(function(show) {
// request(show.coverPhoto).pipe(fs.createWriteStream("./imdb/coverPhoto-"+show.title+".jpg")).on('close', function() {
// cur++;
// console.log("Done Cover --->", cur, "/", max);
// });
// show.links.map(function(v) {
// request(v.src).pipe(fs.createWriteStream("./imdb/"+show.title+"-"+v.name + ".jpg")).on('close', function() {
// console.log("done");
// });
// });
// });
// });