-
-
Save markcheno/a7d4593ac12d5247f2be to your computer and use it in GitHub Desktop.
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
var request = require('request'); | |
var sqlite3 = require('sqlite3').verbose(); | |
var db = new sqlite3.Database('fx.db'); | |
var heartbeat = 10000; | |
function parse_quote (str) { | |
var quote = []; | |
var lines = str.split('\n'); | |
for (var line_idx = 0; line_idx < lines.length; line_idx++) { | |
var line = lines[line_idx]; | |
var line_parts = line.split(','); | |
if (line_parts.length == 9) { | |
var symbol = line_parts[0].split('/').join('').toLowerCase(); | |
var timestamp = parseInt(line_parts[1]); | |
var bid = parseInt(line_parts[3]); | |
var ask = parseInt(line_parts[5]); | |
var spr = ask - bid; | |
quote.push([symbol, timestamp, bid, ask, spr]); | |
} | |
} | |
return quote; | |
}; | |
function get_quote (cb) { | |
request.get('http://webrates.truefx.com/rates/connect.html?f=csv&c=EUR/USD,USD/JPY,GBP/USD,EUR/GBP,USD/CHF,EUR/JPY,EUR/CHF,USD/CAD,AUD/USD,GBP/JPY&s=y', function (err, res, bdy) { | |
if (err) { | |
return cb(err); | |
} | |
var quote = parse_quote(bdy); | |
return cb(null, quote); | |
}); | |
}; | |
function insert_quote (quote) { | |
for (var quote_idx = 0; quote_idx < quote.length; quote_idx++) { | |
db.run('INSERT INTO fx (symbol, timestamp, bid, ask, spr) VALUES (?, ?, ?, ?, ?)', quote[quote_idx]); | |
} | |
}; | |
function select_all (cb) { | |
db.all('SELECT * FROM fx', cb); | |
}; | |
function select_symbol (symbol, cb) { | |
db.all('SELECT * FROM fx WHERE symbol = ?', [symbol], cb); | |
}; | |
function select_symbol_tail (symbol, oldest_timestamp, cb) { | |
db.all('SELECT * FROM fx WHERE symbol = ? AND timestamp > ?', [symbol, oldest_timestamp], cb); | |
}; | |
function establish_table () { | |
db.run('CREATE TABLE fx (rowid INTEGER PRIMARY KEY AUTOINCREMENT, symbol TEXT NOT NULL, timestamp INTEGER NOT NULL, bid INTEGER NOT NULL, ask INTEGER NOT NULL, spr INTEGER NOT NULL)'); | |
}; | |
function minutes_ago (minutes) { | |
var ago_in_ms = (minutes * 60) * 1000; | |
return new Date().getTime() - ago_in_ms; | |
}; | |
function hours_ago (hours) { | |
var ago_in_ms = ((hours * 60) * 60) * 1000; | |
return new Date().getTime() - ago_in_ms; | |
}; | |
function days_ago (days) { | |
var ago_in_ms = (((days * 24) * 60) * 60) * 1000; | |
return new Date().getTime() - ago_in_ms; | |
}; | |
function heart () { | |
get_quote(function (err, quote) { | |
if (err) { | |
console.log(err); | |
return setTimeout(heart, heartbeat); | |
} | |
console.log(quote); | |
console.log(); | |
insert_quote(quote); | |
return setTimeout(heart, heartbeat); | |
}); | |
}; | |
establish_table(); | |
heart(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment