Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save brianpow/9e9a65dcd787ae77d5a4bbea7d2cdd77 to your computer and use it in GitHub Desktop.
Save brianpow/9e9a65dcd787ae77d5a4bbea7d2cdd77 to your computer and use it in GitHub Desktop.
Dump all URLs of building instruction manuals and thumbnails
//https://www.lego.com/en-us/service/buildinginstructions
(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()
console.log("Downloading theme: " + theme.Label)
download(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
download(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 "echo '" + outFilename + "' && sleep 1 && curl -o '" + outFilename + "' -z '" + outFilename + "' '" + instruction.pdfLocation + "'" + "\n" +
"' curl -o '" + outPngFilename + "' -z '" + outPngFilename + "' '" + instruction.frontpageInfo + "'"
//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