Last active
October 31, 2016 05:48
-
-
Save yasyf/06801c37f1785cc430b52edc87c648ea to your computer and use it in GitHub Desktop.
Scape All Of Piazza For A Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Inject JQuery | |
(function(){function l(u,i){ | |
var d=document;if(!d.getElementById(i)){var s=d.createElement('script');s.src=u;s.id=i;d.body.appendChild(s);}}l('https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js','jquery')})(); | |
$j = jQuery.noConflict(); | |
// Helpers | |
function expandChildren(result, level) { | |
if (!result.children.length) { | |
return []; | |
} | |
var newResults = []; | |
let headingOpen = `<h${level}>`; | |
let headingClose = `</h${level}>`; | |
result.children.forEach(child => { | |
var content; | |
if (child.history) { | |
content = child.history.pop().content; | |
} else { | |
content = child.subject; | |
} | |
var heading; | |
if (child.type === "i_answer") { | |
heading = "Staff Answer"; | |
} else if (child.type === "followup") { | |
heading = "Followup"; | |
} else if (child.type === "feedback") { | |
heading = "Followup Response"; | |
} else if (child.type === "s_answer") { | |
heading = "Student Answer"; | |
} else { | |
return; | |
} | |
newResults.push(`${headingOpen}${heading}${headingClose}\n<div>${content}</div>`); | |
newResults = newResults.concat(expandChildren(child, level + 1)); | |
}); | |
return newResults; | |
} | |
// Fetch all posts | |
let promises = $j('.feed_item').map((i, el) => { | |
return $j.ajax({ | |
type: 'POST', | |
contentType: "application/json", | |
dataType: 'json', | |
url: `https://piazza.com/logic/api?method=content.get&aid=${el.id}`, | |
data: JSON.stringify({ | |
method: "content.get", | |
params: { | |
nid: "is6r7b6pn1s5nm", | |
cid: el.id, | |
} | |
}) | |
}); | |
}); | |
// Create blob and force download | |
$j.when.apply($j, promises).then((...args) => { | |
let posts = args.map(resp => { | |
let result = resp[0].result; | |
let entry = result.history.pop(); | |
var elements = expandChildren(result, 2); | |
elements.unshift(`<h1>${entry.subject}</h1>\n<h3>${entry.created}</h3>\n<div>${entry.content}</div>`); | |
return elements.join('\n'); | |
}); | |
let blob = new Blob([posts.join('<hr>')], {type: 'text/html'}); | |
let url = webkitURL.createObjectURL(blob); | |
let a = document.createElement("a"); | |
a.href = url; | |
a.download = 'piazza.html'; | |
a.click(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment