Skip to content

Instantly share code, notes, and snippets.

@nmai
Forked from illmat/iconMonster.js
Last active September 25, 2020 13:49
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmai/64e1a1e0bbc51ee8cdb7 to your computer and use it in GitHub Desktop.
Save nmai/64e1a1e0bbc51ee8cdb7 to your computer and use it in GitHub Desktop.
Downloads all IconMonstr.com SVG files. Works, but may take some time to complete. About 15% of the SVG icons cannot be retrieved through their previews so the script must download them individually.
var casper = require('casper').create({
verbose: true,
logLevevel: 'debug',
pageSettings: {
webSecurityEnabled: false
}
});;
var fs = require('fs');
var pages = [];
var failedFiles = [];
//Stage 1- Retrieve all gallery page links
casper.start('http://iconmonstr.com/page/2', function () {
var pageNumbers = this.evaluate(function () {
return $('a.page-numbers:nth-child(7)').html();
});
for (var i = 1; i <= pageNumbers; i++) {
pages.push('http://iconmonstr.com/page/' + i);
}
});
//Stage 2- Scrape and save all inline SVG previws
// If SVG preview is not available, push filename to deferred queue
casper.then(function() {
this.each(pages, function (self, page) {
self.thenOpen(page, function () {
this.echo(this.getCurrentUrl());
var fileNames = this.evaluate(function () {
var tempArray = [];
$('.thumbnail-link').each(function () {
var name = $(this).attr('href').substring(22);
name = name.substring(0, name.length - 1);
tempArray.push(name);
});
return tempArray;
});
var inlineSvgs = this.evaluate(function () {
var tempArray = [];
$('.thumbnail-preview').each(function () {
tempArray.push($(this).html());
});
return tempArray;
});
for (var i = 0; i < fileNames.length; i++) {
if(inlineSvgs[i].substring(0,4).localeCompare('<svg') == 0) {
casper.echo('Saving: ' + fileNames[i]);
inlineSvgs[i] = '<?xml version="1.0" encoding="utf-8"?>\n' +
'<!-- License Agreement at http://iconmonstr.com/license/ -->\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' +
inlineSvgs[i];
fs.write('svg/' + fileNames[i] + '.svg', inlineSvgs[i], 'w');
} else {
casper.echo('No preview, deferring download: ' + fileNames[i]);
failedFiles.push(fileNames[i]);
}
}
});
});
});
//Stage 3- Grab the AWS-signed download link from each
casper.then(function() {
//Manually fetch the URL from every page.
var counter = 0;
this.each(failedFiles, function(self, filename) {
var href = '';
this.thenOpen('http://iconmonstr.com/' + filename, function () {
counter++;
this.echo('Retrieving file [' + counter + '/' + failedFiles.length + ']: ' + filename);
href = this.evaluate(function() {
//wow very clean very readable
return 'http://s3.iconmonstr.com/' + $('input#btn-svg').attr('onclick').split("'")[1].substring(17);
});
});
this.then(function() {
this.download(href, 'svg/' + filename + '.svg');
});
});
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment