Skip to content

Instantly share code, notes, and snippets.

@siergiej
Last active December 19, 2015 14:39
Show Gist options
  • Save siergiej/5971230 to your computer and use it in GitHub Desktop.
Save siergiej/5971230 to your computer and use it in GitHub Desktop.
var dump = './dump.sql';
var dateformat = 'YYYY-MM-DD HH:mm:ss'
var db = require('sqlite-wrapper')(dump);
var moment = require('moment');
var _ = require('underscore');
var findFirst = function (errors, results) {
if(errors){
throw errors;
}
var last_trade = results[0].Money_Trade__
console.log('Last trade in SQL:'+last_trade+'('+results[0].Date+')');
var mtgoxClient = require('mtgox-apiv2');
var client = new mtgoxClient();
var update = function(last_trade){
client.fetchTrades(last_trade, function(err, trades){
if(err){
throw err;
}
if(trades.result != 'success'){
throw 'MtGox returned invalid data';
}
var insert50 = function(page){
var first = page*20;
var last = (page+1)*20;
db.insertAll('dump', rows.slice(first, last), function(err){
// SQLite can't handle inserting 1000 rows. Need to hack...
if(err){
throw err;
}
if(page < 50-1) insert50(page+1);
});
}
var rows = _.map(trades.data, function(trade){
return {
'Currency__': trade.price_currency,
'Item': trade.item,
'Amount': trade.amount_int,
'Bid_User_Rest_App__': null,
'Type': trade.trade_type,
'Money_Trade__': trade.tid,
'Price': trade.price_int,
'`Primary`': (trade.primary == 'Y')?1:0,
'Ask_User_Rest_App__': null,
'Date': moment.unix(trade.date).format(dateformat),
'Properties': trade.properties
}
});
console.log('Importing 1000 trades to SQL dump, starting at: '+moment.unix(trades.data[0].date).format(dateformat));
insert50(0);
var last_trade = _.last(trades.data).tid;
if (trades.data.length >= 1000) update(last_trade);
});
}
update(last_trade);
}
db.select(
'dump', //table
null, //joins
null, // columns,
'1==1', // whereClause,
null, // whereValues,
findFirst, // callback,
'Money_Trade__ DESC', // orderBy,
'0,1', // limit,
false // distinct
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment