Last active
October 17, 2017 14:19
-
-
Save brianpow/9e0a45b3391df0cfb5df34202feee9e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var baseUrl = "https://www.lego.com//service/biservice/searchbytheme?fromIndex={index}&onlyAlternatives=false&theme={theme}" | |
var themes = JSON.parse($("div.product-search").attr("data-search-themes")) | |
var products = [] | |
next = function() { | |
if (themes.length) { | |
theme = themes.pop() | |
dowload(0, theme.key, theme.Label) | |
} else { | |
show(products) | |
console.log("done!") | |
} | |
} | |
download = function(index, theme, category) { | |
let url = baseUrl.replace("{index}", index).replace("{theme}", theme) | |
console.log("Downloading " + url) | |
$.getJSON(url).done(parse(url, index, theme, category)).fail(function() { | |
console.log("Error occured!") | |
next() | |
}).always(function() { | |
if (products.length % 100 == 0) | |
show(products) | |
}) | |
} | |
parse = function(url, index, theme, category) { | |
return function(data) { | |
products = products.concat(data.products) | |
if (data.moreData) { | |
index += 10 | |
dowload(index, theme, category) | |
} else { | |
next() | |
} | |
} | |
} | |
clean = function(name) { | |
return name.replace(/'/g, "'\"'\"'").replace(/\//g, " or ").replace(/:/g, "_") | |
} | |
show = function(products) { | |
if (!$("#bookmarkletLink").length) | |
$("<textarea id=bookmarkletLink />").prependTo(document.body) | |
if (!$("#bookmarkletCommand").length) | |
$("<textarea id=bookmarkletCommand />").prependTo(document.body) | |
$("#bookmarkletLink").val(JSON.stringify(products)) | |
$("#bookmarkletCommand").val(function() { | |
return products.map(function(product) { | |
return product.buildingInstructions.map(function(instruction) { | |
let parts = instruction.pdfLocation.split("/"), | |
parts2 = parts[parts.length - 1].split("?"), | |
originalFilname = clean(decodeURI(parts2[0])) | |
parts = instruction.frontpageInfo.split("/") | |
parts2 = parts[parts.length - 1].split("?") | |
let originalPngFilname = clean(decodeURI(parts2[0])) | |
let outFilename = [clean(product.themeName), clean(product.productName), clean(product.productId), originalFilname].join(" - ") | |
let outPngFilename = [clean(product.themeName), clean(product.productName), clean(product.productId), originalPngFilname].join(" - ") | |
return "[ ! -f '" + outFilename + "' ] && " + "echo '" + outFilename + "' && sleep 1 && curl -o '" + outFilename + "' -z '" + outFilename + "' '" + instruction.pdfLocation + "'" + "\n" + | |
"[ ! -f '" + outPngFilename + "' ] && " + "echo '" + outPngFilename + "' && curl -o '" + outPngFilename + "' -z '" + outPngFilename + "' '" + instruction.frontpageInfo + "'" | |
}).join("\n") | |
}).join("\n") | |
}) | |
} | |
next() | |
})(jQuery) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment