Skip to content

Instantly share code, notes, and snippets.

@oloynet
Last active December 16, 2015 19:39
Show Gist options
  • Save oloynet/5486961 to your computer and use it in GitHub Desktop.
Save oloynet/5486961 to your computer and use it in GitHub Desktop.
This casperjs script return PDF notice insee position in France for a given 'siren' account. This script is only for your personal use at your own risk
/**
* This casperjs script return PDF notice insee position in France for a given 'siren' account.
* This script is only for your personal use at your own risk.
*
* Usage:
*
* $ casperjs avis-situation-insee.js siren-number
* $ casperjs avis-situation-insee.js 123456789
* $ casperjs avis-situation-insee.js 123456789 --filename=myfile.pdf
*
*/
var usage = 'casperjs avis-situation-insee.js siren [ --filename=insee.pdf --viewport=800x600 --user-agent=<UserAgent> --verbose --screenshot ]';
var casper = require('casper').create(),
utils = require('utils'),
siren = casper.cli.raw.get(0) || false,
nic = casper.cli.raw.get('nic') || '',
filename = casper.cli.raw.get('filename') || 'insee.pdf',
viewport = casper.cli.raw.get('viewport') || '800x600',
user_agent = casper.cli.raw.get('user-agent') || 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
verbose = casper.cli.raw.get('verbose') || false,
screenshot = casper.cli.raw.get('screenshot') || false
;
var timestamp = ( new Date() ).toISOString();
var step = 0;
var url = 'http://avis-situation-sirene.insee.fr/avisitu/';
var pdf_form = {}
if ( !checkSiren (siren) ) {
echoAndExit( 'Usage: ' + usage, true );
}
function checkSiren( siren ) {
if( ( siren.length != 9 ) || ( isNaN( siren ) ) ) {
casper.warn('Siren number is not formatted as 123456789');
return false;
}
var checksum = 0;
for( var i = 0; i < siren.length; i++) {
value = ~~siren[i] * (i % 2 == 0 ? 1 : 2);
checksum += value > 9 ? value - 9 : value;
}
if( (checksum % 10) != 0) {
casper.warn('Siren number is not good');
return false;
}
return true;
}
function echoAndExit( data, exit_level ) {
exit_level = exit_level || false;
if ( typeof data == 'object' ) {
utils.dump( data );
} else {
casper.echo( data );
}
casper.exit( exit_level );
}
var make_screenshot = function () {
if ( screenshot ) {
this.capture( utils.format( 'screenshot-%s-%s-step-%s.png', siren, timestamp, step++ ) );
verbose && this.echo( utils.format( "Make screenshot-%s-%s-step-%s.png for '%s'", siren, timestamp, step, this.getCurrentUrl() ) );
}
}
/*
* ===== start =====
*/
casper.start();
/*
* ===== open page =====
*/
casper.thenOpen( url, function() {
verbose && this.echo( utils.format( "Open url '%s'", url ) );
this.page.settings.webSecurityEnabled = false; // to enable dowload
if( user_agent ) {
this.userAgent( user_agent );
}
this.viewport( 800, 600 );
});
casper.then( make_screenshot );
casper.waitUntilVisible( '.contenu', function() {
this.fill( 'form[name="demForm"]', {
siren: siren.substr(0,3) + ' ' + siren.substr(3,3) + ' ' + siren.substr(6,3),
critere: "S",
departement: "",
departement_actif: "",
nic: "",
bSubmit: "Valider",
}, true);
});
/*
* ===== result page =====
*/
casper.then( function() {
pdf_form = {
action: '/avisitu/AvisPdf.do',
method: 'POST',
data: this.getFormValues( 'form[name="pdfForm"]'),
};
verbose && utils.dump( pdf_form );
});
casper.then( make_screenshot );
/*
* ===== download pdf =====
*/
casper.then( function() {
this.download( pdf_form.action, filename.replace(/(.*)\.(pdf)$/i, '$1-' + siren + '.$2'), pdf_form.method, pdf_form.data);
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment