Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import re
- import datetime
- import locale
- #set locale to en_US/english for date formatting
- #Linux:
- #locale.setlocale(locale.LC_TIME, "en_US")
- #Windows:
- locale.setlocale(locale.LC_TIME, "english")
- def process_ledger(ledgerjson, currency):
- for entry in ledgerjson:
- #different texts:
- #lending
- #Interest Payment on wallet deposit
- if re.match(r"^Interest Payment on wallet (deposit|trading|exchange)$", entry[0]):
- wallet = re.match(r"^Interest Payment on wallet (deposit|trading|exchange)$", entry[0]).group(1).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Interest received for lending " + currency
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- print "\tIncome:Bitfinex:Lending"
- #affiliation
- #Earned fees from user xxx on wallet trading
- elif re.match(r"^Earned fees from user \d+ on wallet (deposit|trading|exchange)$",entry[0]):
- userid = re.match(r"^Earned fees from user (\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1).capitalize()
- wallet = re.match(r"^Earned fees from user (\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Affiliation income in " + currency
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- print "\tIncome:Bitfinex:Affiliation:User" + userid
- #trading
- #Trading fees for xxx.xxx BTC @ xxx.xxx on BFX (x.xx%) on wallet trading
- elif re.match(r"^Trading fees for \d+\.\d+ (BTC|USD|LTC) @ \d+\.\d+ on (BFX|BSTP|MTGOX) \(\d+\.\d+%\) on wallet (deposit|trading|exchange)$",entry[0]):
- wallet = re.match(r"^Trading fees for (\d+\.\d+) (BTC|USD|LTC) @ (\d+\.\d+) on (BFX|BSTP|MTGOX) \((\d+\.\d+)%\) on wallet (deposit|trading|exchange)$", entry[0]).group(6).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Paid trading fees in " + currency + " for a trade"
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- print "\tExpenses:Bitfinex:Fees:Trade"
- #Position #xxx swap on wallet trading
- elif re.match(r"^Position #\d+ swap on wallet (deposit|trading|exchange)$",entry[0]):
- positionid = re.match(r"^Position #(\d+) swap on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Position #(\d+) swap on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Paid swap fees in " + currency + " for a trade"
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- print "\tExpenses:Bitfinex:Fees:Swap"
- #Position #xxx closed @ xxx.xxx on wallet trading
- elif re.match(r"^Position #\d+ closed @ \d+\.\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- positionid = re.match(r"^Position #(\d+) closed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Position #(\d+) closed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(3).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Closed a trade and settled in " + currency
- if entry[1] == "":
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- else:
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- print "\tIncome:Bitfinex:Trades:Close"
- #Position #xxx claimed @ xxx.xxx on wallet trading
- elif re.match(r"^Position #\d+ claimed @ \d+\.\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- positionid = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(3).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Claimed a trade and settled in " + currency
- if entry[1] == "":
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- else:
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- print "\tIncome:Bitfinex:Trades:Claim"
- #Position #xxx claimed @ xxx.xxx (fee: xxx.xxx BTC) on wallet trading
- elif re.match(r"^Position #\d+ claimed @ \d+\.\d+ \(fee: \d+\.\d+ (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$",entry[0]):
- positionid = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- fee = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(3)
- feecurrency = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(4)
- wallet = re.match(r"^Position #(\d+) claimed @ (\d+\.\d+) \(fee: (\d+\.\d+) (BTC|USD|LTC)\) on wallet (deposit|trading|exchange)$", entry[0]).group(5).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Claimed a trade and settled in " + currency
- if entry[1] == "":
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- else:
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- print "\tIncome:Bitfinex:Trades:Claim"
- # seems like the fee is already deducted in the displayed amount,
- # we first need to credit it from income, then deduct it to expenses
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Received fee for claimed trade in " + currency
- print "\tAssets:Bitfinex:" + wallet + "\t" + fee + " " + feecurrency
- print "\tIncome:Bitfinex:Trades:Claim"
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + positionid + ") Paid fee for claimed trade in " + currency
- print "\tAssets:Bitfinex:" + wallet + "\t-" + fee + " " + feecurrency
- print "\tExpenses:Bitfinex:Fees:Claim"
- #Settlement @ xxx.xxx on wallet trading
- elif re.match(r"^Settlement @ \d+\.\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- wallet = re.match(r"^Settlement @ (\d+\.\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Force liquidated a trade and settled in " + currency
- if entry[1] == "":
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- else:
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- print "\tIncome:Bitfinex:Trades:Settle"
- #external deposits/withdrawals
- #Wire Transfer Withdrawal #xxx on wallet deposit
- elif re.match(r"^Wire Transfer Withdrawal #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- withdrawalid = re.match(r"^Wire Transfer Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Wire Transfer Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + withdrawalid +") Wire withdrawal of " + currency
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- #you might want to change this to the account your payment actually went to
- print "\tAssets:Yourbank:Bitfinex:Withdrawals"
- #Bitcoin Withdrawal #xxx on wallet deposit
- elif re.match(r"^Bitcoin Withdrawal #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- withdrawalid = re.match(r"^Bitcoin Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Bitcoin Withdrawal #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + withdrawalid +") Bitcoin withdrawal of " + currency
- #entry[2] - this is a debit transaction!
- print "\tAssets:Bitfinex:" + wallet + "\t-" + entry[2] + " " + currency
- #you might want to change this to the account your payment actually went to
- print "\tAssets:Bitcoin:Bitfinex:Withdrawals"
- #Deposit (WIRE) #xxx on wallet deposit
- elif re.match(r"^Deposit \(WIRE\) #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- depositid = re.match(r"^Deposit \(WIRE\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Deposit \(WIRE\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + depositid +") Wire deposit of " + currency
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- #you might want to change this to the account your payment actually originated from
- print "\tAssets:Yourbank:Bitfinex:Deposits"
- #Deposit (BITCOIN) #xxx on wallet deposit
- elif re.match(r"^Deposit \(BITCOIN\) #\d+ on wallet (deposit|trading|exchange)$",entry[0]):
- depositid = re.match(r"^Deposit \(BITCOIN\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- wallet = re.match(r"^Deposit \(BITCOIN\) #(\d+) on wallet (deposit|trading|exchange)$", entry[0]).group(2).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " (#" + depositid +") Bitcoin deposit of " + currency
- print "\tAssets:Bitfinex:" + wallet + "\t" + entry[1] + " " + currency
- #you might want to change this to the account your payment actually originated from
- print "\tAssets:Bitcoin:Bitfinex:Deposits"
- #internal movement between accounts
- #warning: entries are always in pairs here! One for crediting one account, one debiting the other
- #probably just use one of both, discard the other
- #Transfer of x.xxx USD from wallet trading to deposit on wallet deposit
- elif re.match(r"^Transfer of \d+\.\d+ (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$",entry[0]):
- if entry[1] == "":
- #No credit, this is the second line for the transfer.
- #It is assumed that the debit transaction was captured too and is processed.
- None
- else:
- amount = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(1)
- currency = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(2)
- fromwallet = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(3).capitalize()
- towallet = re.match(r"^Transfer of (\d+\.\d+) (BTC|USD|LTC) from wallet (deposit|trading|exchange) to (deposit|trading|exchange) on wallet (deposit|trading|exchange)$", entry[0]).group(4).capitalize()
- print datetime.datetime.strptime(entry[4], "%d %b %H:%M").replace(2013).strftime("%Y-%m-%d") + " Moving " + currency + " between wallets"
- print "\tAssets:Bitfinex:" + fromwallet + "\t-" + entry[1] + " " + currency
- print "\tAssets:Bitfinex:" + towallet + "\t" + entry[1] + " " + currency
- else:
- print "UNKNOWN ENTRY... DATA: ", entry
- with open("usd.json", "rb") as jsonfile:
- ledgerjson = json.loads(jsonfile.read())
- currency = "USD"
- process_ledger(ledgerjson, currency)
- with open("btc.json", "rb") as jsonfile:
- ledgerjson = json.loads(jsonfile.read())
- currency = "BTC"
- process_ledger(ledgerjson, currency)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement