Skip to content

Instantly share code, notes, and snippets.

@vitalbone
Last active February 3, 2016 20:53
Show Gist options
  • Save vitalbone/afbac6c4377ec4a7c47b to your computer and use it in GitHub Desktop.
Save vitalbone/afbac6c4377ec4a7c47b to your computer and use it in GitHub Desktop.
📼 My first parser
var http = require('http')
var url = 'http://finance.google.com/finance/info?client=ig&q=INDEXASX:XAO'

// method for retrieving texty-json from URL
var download = (url, callback) => {
	http.get(url, res => {
		var data = ''
		res.on('data', chunk => {
			data += chunk
		})
		res.on('end', () => {
			callback(data)
		})
	}).on('error', () => {
		callback(null)
	})
}

// method for cleaning up data somewhat
var cleanData = (data, cb) => {
	var dat = data.trim()
	var da = dat.replace(/[^\:\w\,]/gi, '')
	// return console.log(da);
	return cb(da)
}

// method for turning text into JSON object
var objTransform = (data, cb) => {
	var result = {};
	data.split(/\s*\,\s*/).forEach(function(el){
		var parts = el.split(/\s*:\s*/); result[parts[0]] = parts[1];
	});
	return cb(JSON.stringify(result, null, 2))
	// return result;
}

// actually using all the preceding methods together to win.
download(url, (data) => {
	if (data) {
		cleanData(data, (da) => {
			objTransform(da, (result) => {
				// this is where you want to output into MAX
				console.log(result);
			})
		})
	} else {
		console.log('error');
	}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment