Created
July 8, 2017 15:46
-
-
Save frenchie4111/5b5238ad63bc3064f55e4b4c1b8e1b97 to your computer and use it in GitHub Desktop.
Tampermonkey Script for getting my Hashflare.io history
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
// ==UserScript== | |
// @name Hashflare History Scraper | |
// @version 0.1 | |
// @author Mike Lyons <mdl0394@gmail.com> | |
// @match https://hashflare.io/panel/history | |
// @require http://code.jquery.com/jquery-3.2.1.min.js | |
// @require https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.13/moment-timezone-with-data.js | |
// @grant GM_addStyle | |
// @grant GM_setClipboard | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var button = $( '<a class="minimalize btn btn-primary">Export</a>' ); | |
$( '.navbar-header' ).append( button ); | |
var parseRow = function( row ) { | |
let cols = $( row ).children( 'td' ); | |
let time = $( cols[ 1 ] ).attr( 'data-order' ); | |
var parsed_date = moment.tz( ( +time ) * 1000, 'GMT' ); | |
var reformated_date = parsed_date.format( 'MM/DD/YYYY' ); | |
return { | |
title: $( cols[ 0 ] ).text(), | |
date: reformated_date, | |
time: time, | |
amount_btc: $( cols[ 2 ] ).text(), | |
balance_btc: $( cols[ 3 ] ).text(), | |
amount_eth: $( cols[ 4 ] ).text(), | |
balance_eth: $( cols[ 5 ] ).text() | |
}; | |
}; | |
var getSpent = function( item, prev_balance ) { | |
var transfer_type = /via (.*) transfer/g.exec( item.title )[ 1 ]; | |
console.log( parseFloat( prev_balance ), parseFloat( item.balance_btc ), ( parseFloat( prev_balance ) - parseFloat( item.balance_btc ) ) ); | |
return ( ( transfer_type === 'balance' ) ? ( parseFloat( prev_balance ) - parseFloat( item.balance_btc ) ) : 0 ); | |
}; | |
var getValuesForTitle = function( item, prev_balance ) { | |
if( item.title.match( /Purchased.*Scrypt.*/g ) ) { | |
var purchased_scrypt = /Purchased (\d*).*Scrypt.*/g.exec( item.title )[ 1 ]; | |
var transfer_type = /via (.*) transfer/g.exec( item.title )[ 2 ]; | |
return { | |
purchased_scrypt: purchased_scrypt, | |
scrypt_spent_btc: getSpent( item, prev_balance ) | |
}; | |
} | |
if( item.title.match( /Purchased.*SHA.*/g ) ) { | |
return { | |
sha_spent_btc: getSpent( item, prev_balance ) | |
}; | |
} | |
if( item.title.match( /Purchased.*ETH.*/g ) ) { | |
return { | |
eth_spent_btc: getSpent( item, prev_balance ) | |
}; | |
} | |
if( item.title.match( /Scrypt payout \(BTC\)/g ) ) { | |
return { | |
scrypt_btc_mined: item.amount_btc | |
}; | |
} | |
if( item.title.match( /Scrypt maintenance \(BTC\)/g ) ) { | |
return { | |
scrypt_maintenance: item.amount_btc | |
}; | |
} | |
if( item.title.match( /SHA-256 payout \(BTC\)/g ) ) { | |
return { | |
sha_btc_mined: item.amount_btc | |
}; | |
} | |
if( item.title.match( /SHA-256 maintenance \(BTC\)/g ) ) { | |
return { | |
sha_maintenance: item.amount_btc | |
}; | |
} | |
return {}; | |
}; | |
var flattenTimeItem = function( item, prev_balance ) { | |
var values = getValuesForTitle( item, prev_balance ); | |
values.balance_btc = item.balance_btc; | |
return values; | |
}; | |
var pivot = function( rows ) { | |
var times = {}; | |
rows | |
.forEach( function( row ) { | |
if( !times.hasOwnProperty( row.date ) ) { | |
times[ row.date ] = []; | |
} | |
times[ row.date ].push( row ); | |
} ); | |
var times_array = []; | |
for( var time in times ) { | |
times_array.push( [ time, _.sortBy( times[ time ], ( time ) => time.time ) ] ); | |
} | |
console.log( times_array ); | |
times_array = _.sortBy( times_array, ( times_array_i ) => times_array_i[ 0 ] ); | |
var pivoted = []; | |
var prev_balance = 0; | |
for( var times_array_i in times_array ) { | |
var values = times_array[ times_array_i ]; | |
var time_info = { | |
date: values[ 0 ] | |
}; | |
values[ 1 ] | |
.forEach( function( value ) { | |
var flattened = flattenTimeItem( value, prev_balance ); | |
time_info = $.extend( time_info, flattened ); | |
prev_balance = flattened.balance_btc; | |
} ); | |
pivoted.push( time_info ); | |
} | |
return pivoted; | |
}; | |
var button_onclick = button | |
.click( function() { | |
button.text( '...' ); | |
var rows = []; | |
do { | |
$( '#DataTables_Table_3' ) | |
.children( 'tbody' ) | |
.children( 'tr' ) | |
.each( function() { | |
rows.push( parseRow( this ) ); | |
} ); | |
$( '#DataTables_Table_3_next' ).click(); | |
} while( $( '#DataTables_Table_3_next.disabled' ).length === 0 ); | |
$( '#DataTables_Table_3' ) | |
.children( 'tbody' ) | |
.children( 'tr' ) | |
.each( function() { | |
rows.push( parseRow( this ) ); | |
} ); | |
console.log( rows ); | |
var pivoted = pivot( rows ); | |
pivoted = _.sortBy( pivoted, ( pivoted_i ) => +moment( pivoted_i.date, 'MM/DD/YYYY' ) ); | |
var print_rows = []; | |
for( var i in pivoted ) { | |
var pivot_row = pivoted[ i ]; | |
console.log( pivot_row ); | |
print_rows | |
.push( [ | |
( pivot_row.date ), | |
( pivot_row.purchased_scrypt ? pivot_row.purchased_scrypt : 0 ), | |
( pivot_row.scrypt_spent_btc ? +pivot_row.scrypt_spent_btc : 0 ) + ( pivot_row.sha_spent_btc ? +pivot_row.sha_spent_btc : 0 ) + ( pivot_row.eth_spent_btc ? +pivot_row.eth_spent_btc : 0 ), | |
( pivot_row.scrypt_btc_mined ? +pivot_row.scrypt_btc_mined : 0 ) + ( pivot_row.sha_btc_mined ? +pivot_row.sha_btc_mined : 0 ), | |
( pivot_row.scrypt_maintenance ? +pivot_row.scrypt_maintenance : 0 ) + ( pivot_row.sha_maintenance ? +pivot_row.sha_maintenance : 0 ) | |
].join( ',' ) ); | |
} | |
console.log( print_rows.join( '\n' ) ); | |
GM_setClipboard( print_rows.join( '\n' ) ); | |
button.text( 'Done' ); | |
button.addClass( 'disabled' ); | |
button_onclick.unbind(); | |
} ); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment