Anonymous Anonymous - 1 month ago
94 0

No description

Javascript

3d model rip

var wget = require('node-wget');
var glob = require("glob");
var mkdirp = require('mkdirp');
var path = require('path');
var sleep = require('sleep');
var fs = require('fs')

process.chdir('../orlando');

var workers = 40
var currentWorkers = 0

var basePath = process.cwd()

let o = require(basePath+"/root.json")

traverse(o,processJSON, "/");

function willfollow(value) {
	return value.indexOf(".json") > -1 || value.indexOf(".b3dm") > -1 || value.indexOf(".jpeg") > -1
}

function wait(fn) {
	if (currentWorkers <= workers) {
		fn()
	} else {
		setTimeout(() => { wait(fn) },50)
	}
}

const error = (msg) => {
	fs.appendFileSync('./orlando.error.log', msg+"\n");
}

const log = (msg) => {
	fs.appendFileSync('./orlando.log', msg+"\n");
}

function download(url, dest, filePath, msg) {
	try {
    fs.accessSync(dest, fs.F_OK);
    process.stdout.clearLine();
		process.stdout.cursorTo(0);
		process.stdout.write("Already downloaded: " + filePath); 
    log("Already downloaded: " + filePath); 
    if (filePath.indexOf(".json") > -1) {
    	let json = require(dest)
    	traverse(json, processJSON, path.dirname(filePath))
    }
	} catch (e) {
		process.stdout.clearLine();
		process.stdout.cursorTo(0);
		process.stdout.write("Downloading: " + filePath); 
		currentWorkers++
		wget({url: url, dest: dest}, function(err, data, body) {
	 		currentWorkers--
	 		if (err) {
	      error('--- error start ---');
	      error(url);
	      error(dest);
	      error(err);            // error encountered \
	      error('--- error done ---');
	      wait(() => {
	      	download(url,dest,filePath, "Requeued")
	      })
	    } else {
	    	log("Saved: " + filePath + (msg ? "("+msg+")" : "")); 
				if (filePath.indexOf(".json") > -1) {
					try {
						let json = JSON.parse(body)
						traverse(json, processJSON, path.dirname(filePath))
					} catch (err) {
						error(err)
					}
				}
			}
		});
	}
}

//property and it's value
function processJSON(key,value,dir) {
	if (typeof value == "string") {
		if (willfollow(value)) {
			let filePath = path.join(dir,value)
			var url = "https://d3h9zulrmcj1j6.cloudfront.net/Orlando_Cesium"+filePath;

			mkdirp(path.join(basePath,path.dirname(filePath)), function (err) {
				log("Queuing: "+filePath)
				wait(() => {
					download(url, path.join(basePath, filePath), filePath)
				})
			});
		}
	}
}

function traverse(o,func, dir) {
	for (var i in o) {
		func.apply(this,[i,o[i], dir]);  
		if (o[i] !== null && typeof(o[i])=="object") {
			//going on step down in the object tree!!
			traverse(o[i],func, dir);
		}
	}
}