Skip to content

Instantly share code, notes, and snippets.

@jrjohnson
Created March 5, 2015 21:50
Show Gist options
  • Save jrjohnson/4fd4b3f8533944d4579c to your computer and use it in GitHub Desktop.
Save jrjohnson/4fd4b3f8533944d4579c to your computer and use it in GitHub Desktop.
extractor_for_php.html
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
</head>
<script type='text/javascript'>
function download(filename, text) {
var div = $('<pre>');
div.append(text);
$('#links').append(div);
// var link = $('<a>');
// link.attr('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
// link.attr('download', filename);
// link.text(filename);
// $('#links').append(link);
// $('#links').append($('<br>'));
}
function pullData(queue)
{
if(queue.length == 0){
return true;
}
var obj = queue.shift();
var endpoint = obj.endpoint;
if(endpoint == 'learningMaterialStatus'){
endpoint = 'learningMaterialStatuses';
}
var maxRecords = obj.maxRecords;
var blockSize = maxRecords > 2000? maxRecords / 20: maxRecords;
var dfdrt = new $.Deferred();
var str = '$arr = array();\n';
var requests = [];
for(var i = 0; i < maxRecords; i += blockSize){
var loc = "http://ilios.dev/app_dev.php/api/v1/" +
endpoint.toLowerCase() +
"?limit=" +
blockSize +
'&orderBy[id]=ASC'+
"&offset=" + i;
var request = $.getJSON( loc, function( data ) {
var arr = data[endpoint];
if(arr === undefined){
arr = data[endpoint.substring(0, endpoint.length -1)];
}
if(arr === undefined && endpoint === 'learningMaterialStatuses'){
arr = data.learningMaterialStatus;
}
if(arr !== undefined){
for(var i = 0; i < arr.length; i++){
var obj = arr[i];
str += "\n$arr[" + obj.id + "] = array(\n" + DumpObjectIndented(obj, " ") + "\n);\n";
}
}
});
requests.push(request);
}
$.when.apply($, requests).done(function(){
download(endpoint + '.js', str);
pullData(queue);
});
return dfdrt.promise();
}
function DumpObjectIndented(obj, indent)
{
var result = "";
if (indent == null) indent = "";
for (var property in obj)
{
var value = obj[property];
if (typeof value == 'string')
value = '"' + value.replace(/"/g, "'").replace(/\n/g, " ") + '"';
else if (typeof value == 'object')
{
if (value instanceof Array)
{
if(value.length > 0){
var str = '';
if(value.length < 10){
str = "'" + value.join("','") + "'";
} else {
str += "\n" + indent;
for(var m = 0; m < value.length; m++){
str += indent + "'" + value[m] + "',\n "
}
}
value = "[" + str + "]";
} else {
value = "[]";
}
}
}
result += indent + "'" + property + "' => " + value + ",\n";
}
return result.replace(/,\n$/, "");
}
$(document).ready(function(){
var queue = [];
// queue.push({endpoint: 'aamcPcrs', maxRecords: 200});
// queue.push({endpoint: 'aamcMethods', maxRecords: 200});
// queue.push({endpoint: 'alerts', maxRecords: 100});
// queue.push({endpoint: 'alertChangeTypes', maxRecords: 100});
// queue.push({endpoint: 'cohorts', maxRecords: 200});
// queue.push({endpoint: 'competencies', maxRecords: 100});
//queue.push({endpoint: 'courses', maxRecords: 1500});
// queue.push({endpoint: 'disciplines', maxRecords: 500});
// queue.push({endpoint: 'learningMaterials', maxRecords: 5300});
// queue.push({endpoint: 'learnerGroups', maxRecords: 750});
// queue.push({endpoint: 'courseLearningMaterials', maxRecords: 500});
// queue.push({endpoint: 'curriculumInventoryInstitutions', maxRecords: 200});
// queue.push({endpoint: 'curriculumInventoryReports', maxRecords: 200});
// queue.push({endpoint: 'curriculumInventorySequences', maxRecords: 200});
// queue.push({endpoint: 'curriculumInventorySequenceBlocks', maxRecords: 200});
// queue.push({endpoint: 'curriculumInventoryAcademicLevels', maxRecords: 200});
// queue.push({endpoint: 'curriculumInventoryExports', maxRecords: 200});
// queue.push({endpoint: 'departments', maxRecords: 200});
// queue.push({endpoint: 'ilmSessions', maxRecords: 2000});
// queue.push({endpoint: 'learningMaterialUserRoles', maxRecords: 10});
// queue.push({endpoint: 'learningMaterialStatus', maxRecords: 10});
// queue.push({endpoint: 'learningMaterials', maxRecords: 10000});
// queue.push({endpoint: 'instructionHours', maxRecords: 10});
// queue.push({endpoint: 'instructorGroups', maxRecords: 200});
// queue.push({endpoint: 'sessionLearningMaterials', maxRecords: 8000});
// queue.push({endpoint: 'meshConcepts', maxRecords: 10000});
// queue.push({endpoint: 'meshDescriptors', maxRecords: 5000});
// queue.push({endpoint: 'meshQualifiers', maxRecords: 100});
// queue.push({endpoint: 'meshTerms', maxRecords: 40000});
// queue.push({endpoint: 'meshSemanticTypes', maxRecords: 200});
// queue.push({endpoint: 'objectives', maxRecords: 25000});
// queue.push({endpoint: 'offerings', maxRecords: 25000});
// queue.push({endpoint: 'programs', maxRecords: 100});
// queue.push({endpoint: 'programYears', maxRecords: 500});
// queue.push({endpoint: 'publishEvents', maxRecords: 20000});
// queue.push({endpoint: 'recurringEvents', maxRecords: 4000});
// queue.push({endpoint: 'reports', maxRecords: 10});
// queue.push({endpoint: 'schools', maxRecords: 20});
// queue.push({endpoint: 'sessions', maxRecords: 25000});
// queue.push({endpoint: 'sessionDescriptions', maxRecords: 3000});
// queue.push({endpoint: 'sessionTypes', maxRecords: 1000});
queue.push({endpoint: 'userRoles', maxRecords: 10});
// queue.push({endpoint: 'users', maxRecords: 100});
pullData(queue);
});
</script>
<h3>I'm loading your crap.... give it a sec, k?</h3>
<p id ='links'>
</p>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment