Skip to content

Instantly share code, notes, and snippets.

@cdgriffith
Last active January 19, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdgriffith/c828e2c5bff7fe1e380989d3e7464136 to your computer and use it in GitHub Desktop.
Save cdgriffith/c828e2c5bff7fe1e380989d3e7464136 to your computer and use it in GitHub Desktop.
NiceHash Transaction Parser
/*
Takes a NiceHash mining transaction CSV file and converts it to a monthly average. Ignores non-mining lines.
Requires `csv`
$ npm install csv
Example usage:
$ node transactionParser.js
Found file 'transactions-342301-2018-01-19.csv' as newest transactions record
Month Total Days Daily Average
2018-01 0.04170 19 0.00219
2017-12 0.00313 02 0.00156
*/
var fs = require('fs');
var parse = require('csv-parse');
function findNewestCSV(){
/*
Automatically find the newest transaction file in the current directory and parse it.
*/
var newest = null;
var newestDate = null;
fs.readdir(".", function(err, items) {
for (var i=0; i<items.length; i++) {
if (items[i].endsWith('.csv') && items[i].startsWith('transactions')){
var date = new Date(items[i].slice(-14, -4));
if (date > newestDate){
newestDate = date;
newest = items[i];
}
}
}
if (! newest) {
console.log("No transaction files found!");
} else {
console.log("Found file '" + newest +"' as newest transactions record");
parseFile(newest);
}
});
}
function parseFile(filename){
fs.readFile(filename, 'utf-8', function(err, data){
parseCSV(data);
});
}
function parseCSV(data){
/*
Take a CSV file from nicehash transactions and make it into something pasteable into google sheets
*/
parse(data, {columns: true}, function (err, data) {
data.forEach(function(item, i) {
if (item.Comment.startsWith('Mining')){
item.Amount = parseFloat(item.Amount.split(" ")[0]);
item['BTC/USD'] = parseFloat(item['BTC/USD']); //unused, here for later
} else {
item.Amount = 0.0;
}
});
printData(monthlyTotals(data));
})
}
function determineMonth(dateString){
/*
Transform 2018-01-19 05:40:48 into 2018-01
*/
var date = dateString.split(" ")[0];
return date.slice(0, 7);
}
function monthlyTotals(data){
var months = {};
data.forEach(function(item, i){
if (! item.Comment.startsWith('Mining')) return;
var month = determineMonth(item.Date);
if (! months[month]){
months[month] = {'total': item.Amount, 'days': 1}
} else {
months[month]['total'] += item.Amount;
months[month]['days'] += 1;
}
});
for (var month in months) {
months[month].average = months[month].total / months[month].days;
}
return months;
}
function printData(data){
console.log("\nMonth Total Days Daily Average");
for (var month in data){
console.log('' + month + ' ' + data[month].total.toFixed(5) + ' ' + data[month].days.toString().padStart(2, "00") + ' ' + data[month].average.toFixed(5) )
}
}
findNewestCSV();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment