Skip to content

Instantly share code, notes, and snippets.

@thelebster
Created May 22, 2014 21:11
Show Gist options
  • Select an option

  • Save thelebster/7ab5495d458200f03497 to your computer and use it in GitHub Desktop.

Select an option

Save thelebster/7ab5495d458200f03497 to your computer and use it in GitHub Desktop.
Get XLS reports from http://smsaero.ru/. Uses CasperJS - http://casperjs.org. Run script: casperjs smsaero.js --email=fakemail@fake.com --password=fakepassword
var casper = require('casper').create({
//verbose: true,
//logLevel: 'debug',
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22'
});
casper.cli.drop("cli");
casper.cli.drop("casper-path");
if (casper.cli.args.length === 0 && Object.keys(casper.cli.options).length === 0) {
casper.echo("No arg nor option passed").exit();
}
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
casper.start('http://smsaero.ru/', function() { });
casper.then(function() {
this.fill('form#form-login', {
'user[email]': casper.cli.get("email"),
'user[password]': casper.cli.get("password")
}, true);
});
casper.waitForUrl('http://smsaero.ru/cabinet/sendings/', function() {
this.echo('We\'re logged in.');
this.capture('_cabinet_sendings_main.png');
});
var links = [];
function getLinks() {
var links = document.querySelectorAll('.sendings-container .new-sendings-table tr.done');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('data-id');
});
};
var page = 0;
function loadNext(casper) {
casper.waitFor(function() {
if (this.visible('.showMore')) {
this.thenClick('.showMore a');
this.echo('load next results...');
}
return true;
}, function() { // then
//require('utils').dump(this.getElementInfo('.opaco .loader'));
if (this.visible('.opaco .loader')) {
this.waitWhileVisible('.opaco .loader', function() {
this.capture('_cabinet_sendings_main' + page++ + '.png');
this.emit('results.loaded');
}, function() {
this.echo('next results not loaded!');
}, 5000);
}
}, function() {
this.echo("Loading failed.").exit();
}, 500);
};
casper.on('results.loaded', function() {
loadNext(this);
});
casper.then(function() {
//require('utils').dump(this.getElementInfo('.showMore'));
this.waitUntilVisible('.showMore', function() {
loadNext(this);
});
});
casper.then(function() {
this.echo('get links...');
links = this.evaluate(getLinks);
this.echo('downloading...');
this.each(links, function(self, link) {
var data = {type: 'detailed', id: link};
this.download('http://smsaero.ru/cabinet/statisticexport/', link + '.xls', 'POST', data);
this.echo('download ' + link + '.xls');
});
});
casper.run(function() {
this.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment