Skip to content

Instantly share code, notes, and snippets.

@jmbeach
Created April 4, 2016 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmbeach/a25665b30fa6255f9cf74df1496f5b47 to your computer and use it in GitHub Desktop.
Save jmbeach/a25665b30fa6255f9cf74df1496f5b47 to your computer and use it in GitHub Desktop.
beastie scraper
// each h2 is a section
var $h2s = $("h2");
console.log($h2s.length)
var alphabet = ['A','B','C','D','E','F','G','H','I','J','K'];
function Quiz() {
var self = this;
self.concepts = [];
self.print = function() {
var txtPrint = "";
// for each concept
for (var i = 0; i < self.concepts.length; i++) {
var curConcept = self.concepts[i];
// print concept
txtPrint += curConcept.name +"\n"
// for each question in concept
for (var j = 0; j < curConcept.questions.length;j++) {
var curQuestion = curConcept.questions[j];
txtPrint += curQuestion.number+"\n";
txtPrint += curQuestion.text + "\n"
}
}
return txtPrint;
}
}
var quiz = new Quiz();
function Concept(name) {
var self = this;
self.name = name;
self.questions = [];
}
function Question(number, text) {
var self = this;
self.number = number;
self.text = text;
}
for (var i = 0; i < $h2s.length; i++) {
var $h2 = $($h2s[i]);
var $itemsInSection = $h2.nextUntil("h2");
quiz.concepts.push(new Concept($h2.text().replace("Concept: ","").trim()));
console.log($h2.text())
for (var j = 0;j < $itemsInSection.length;j++) {
var curQuestion = new Question();
var $li = $($itemsInSection[j]).find("li").first();
var questionText = $li.text().replace(
$li.children().text()
.replace("strong","")
.replace("weak","")
.replace('nn','')
.trim()
.replace('N P','')
.replace('B N P A N P','')
.trim()
,'')
.replace(/\t/g, '')
.replace(/\r\n/g,'')
.replace(/\n/g,'')
.trim();
// if not t or f
var $questionNumber = $li.parent();
curQuestion.number = parseInt($questionNumber.attr("start"));
console.log($questionNumber.attr("start"))
var $answers = $li.children().children("li");
//console.log($answers);
var totalAnswer = questionText+"\n";
for (var k = 0; k < $answers.length; k++) {
var $answer = $($answers[k]);
var letter = alphabet[k];
var txtAnswer = letter + ". "+$answer.text().trim()
.replace("\n","")
.replace(/\t/g, '')+"\n";
//console.log(txtAnswer)
totalAnswer += txtAnswer;
}
console.log(totalAnswer)
curQuestion.text = totalAnswer;
quiz.concepts[i].questions.push(curQuestion);
}
}
console.log(quiz.concepts);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment