Created
May 17, 2016 16:30
-
-
Save martin-cowie/94b749092146affcc4332d3161f615fd to your computer and use it in GitHub Desktop.
Simple Node script to push real price data into a MongoDB collection.
This file contains 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
#!/usr/bin/env node | |
var request = require('request'); | |
var MongoClient = require('mongodb').MongoClient; | |
var assert = require('assert'); | |
const truefxURL = "http://webrates.truefx.com/rates/connect.html"; | |
const pollFreq = 500; | |
const mongoURL = (process.argv.length == 3) ? process.argv[2] : 'mongodb://localhost/truefx'; | |
const mongoCollectionName = "currencies"; | |
var mongoCollection; | |
// Connect to Mongo, get the collection | |
MongoClient.connect(mongoURL, function(err, db) { | |
assert.equal(null, err); | |
console.info(`Connected succesfully to ${mongoURL}`); | |
mongoCollection = db.collection(mongoCollectionName); | |
setInterval(placeTrueFxRequest, pollFreq); | |
console.info(`Polling ${truefxURL} every ${pollFreq}ms and populating ${mongoCollectionName}`); | |
}); | |
function placeTrueFxRequest() { | |
request({ | |
url: truefxURL, | |
method: "GET", | |
qs: {f: 'csv'} | |
}, function(error, response, body){ | |
if (!error && response.statusCode == 200) { | |
// Print out the response body | |
processCurrencyPairs(body); | |
} else { | |
console.error(`statusCode: ${response.statusCode}`) | |
} | |
} | |
); | |
} | |
function processCurrencyPairs(csvData) { | |
const lines = csvData.trim().split("\n"); | |
var result = {}; | |
lines.forEach(function(line) { | |
var parts = line.split(","); | |
var pairName = parts.shift(); | |
var millis = parts.shift(); | |
var bidBig = parts.shift(); | |
var bidPoints = parts.shift(); | |
var offerBig = parts.shift(); | |
var offerPoints = parts.shift(); | |
var high = parts.shift(); | |
var low = parts.shift(); | |
var open = parts.shift(); | |
var document = { | |
pairName: pairName, | |
timestamp: millis, | |
bid: {big: bidBig, points: bidPoints}, | |
offer: {big: offerBig, points: offerPoints}, | |
high: high, | |
low: low, | |
open: open | |
} | |
mongoCollection.update({pairName: pairName}, document, {upsert: true}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment