Skip to content

Instantly share code, notes, and snippets.

@yanirs
Created January 15, 2015 02:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanirs/eddedf152f42c1ee02b2 to your computer and use it in GitHub Desktop.
Save yanirs/eddedf152f42c1ee02b2 to your computer and use it in GitHub Desktop.
PhantomJS script for importing collection dumps to Parse.com
/**
* PhantomJS script for importing collection dumps to Parse.com.
*
* Usage: phantomjs --ssl-protocol any import-parse-class.js <configFile> <dumpFile> <collectionName>
*
* Parameters:
* - configFile: path of a JSON file with contents:
* {
* "email": <Parse user email>,
* "password": <Parse user password>,
* "app": <Parse app name (the URL "https://parse.com/apps/<app>" must be valid)
* }
* - dumpFile: collection dump path (see https://parse.com/docs/data#data-import)
* - collectionName: name of the collection to be created (cannot be an existing collection)
*
* Notes:
* - The "--ssl-protocol any" flag is required for PhantomJS to connect to Parse
* - This script was tested on Ubuntu 14.04 with PhantomJS 1.9
* - More details about why this script is needed are here:
* http://yanirseroussi.com/2015/01/15/automating-parse-com-bulk-data-imports/
*/
var system = require('system');
var fs = require('fs');
if (system.args.length < 4) {
console.log('Usage: phantomjs --ssl-protocol any import-parse-class.js <configFile> <dumpFile> <collectionName>');
phantom.exit();
}
var params = JSON.parse(fs.read(system.args[1]));
['email', 'password', 'app'].forEach(function (item) {
if (!params.hasOwnProperty(item)) {
console.log('Required config file property missing: ' + item);
phantom.exit();
}
});
params.dumpFile = system.args[2];
params.collectionName = system.args[3];
var page = require('webpage').create();
var loadInProgress = false;
page.onLoadFinished = function() {
loadInProgress = false;
};
function checkPageLocation(expectedLocation) {
return page.evaluate(function () { return window.location.toString() }) === expectedLocation;
}
var steps = [
function loadLoginPage() {
var loginPage = 'https://parse.com/user_session/new';
page.open(loginPage);
loadInProgress = true;
return function () { return !loadInProgress && checkPageLocation(loginPage) };
},
function submitLoginForm() {
page.evaluate(function (email, password) {
$('#user_session_email').val(email);
$('#user_session_password').val(password);
$('#new_user_session').submit();
}, params.email, params.password);
return function () { return checkPageLocation('https://parse.com/apps') };
},
function loadCollectionPage() {
var collectionPage = 'https://parse.com/apps/' + params.app + '/collections';
page.open(collectionPage);
loadInProgress = true;
return function () {
return !loadInProgress &&
checkPageLocation(collectionPage) &&
page.evaluate(function () { return $('.import').length === 1 })
};
},
function clickImport() {
page.evaluate(function () {
$('.import').click();
});
return function () {
return page.evaluate(function () {
return $('form[class=import]').length === 1
})
};
},
function submitImportForm() {
page.uploadFile('input[name=qqfile]', params.dumpFile);
page.evaluate(function (className) {
$('input[class=class_name]').val(className);
$('form[class=import]').submit();
}, params.collectionName);
return function () {
return page.evaluate(function (className) {
return $('ul[class=class_list] a[data-name=' + className + ']').length === 1
}, params.collectionName)
}
}
];
var stepIndex = 0;
var stepConditionFulfilled = function () { return true };
setInterval(function () {
if (!stepConditionFulfilled()) {
return;
}
console.log(page.evaluate(function () {
return 'Current page: ' + window.location + ' (' + document.title + ')';
}));
var step = steps[stepIndex];
if (step) {
console.log('Running ' + step.name);
stepConditionFulfilled = step();
stepIndex++;
} else {
console.log('Done!');
phantom.exit();
}
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment