Coinbase Account Checker Script

in #bitcoin7 years ago

So I figured I'd share a little script I wrote a while back for checking how much my Coinbase account was worth. Its pretty crude, and needs some error checking and general improvements, but it is extremely useful for letting me check how much is in my account there without going through the hassle of logging in. It also was a good excuse to mess about with their API.

Incidentally, its also a good way of letting me know when the damn site is down ;)

So for those of you who don't need handholding, the Github repository is here.

$ python account_checker.py 
[+] Current Datetime: 2017-09-11 15:19:44.097024
[+] You have [REDACTED] LTC valued at [REDACTED] euros
[+] You have [REDACTED] ETH valued at [REDACTED] euros
[+] You have [REDACTED] EUR valued at [REDACTED] euros
[+] You have [REDACTED] BTC valued at [REDACTED] euros
[+] Total euro worth is: [REDACTED] 
$

2 minute setup...

To set it up, you need to create a Coinbase API key and secret. This ONLY needs "read" permissions for your wallets - don't give it "write" permissions. You put these in "creds.py".

You will also need to install the "coinbase" python module, pip install -r requirements.txt will take care of this if you don't already have it installed.

How it works...

So for the programmers in the audience, here is exactly how the script works.

The "main" part, part 1 - setting up the API, and zeroing some values.

Below is commented code. It gets the current date/time, creates a "client" object for the Coinbase API, gets the account information from the API as JSON, and zeroes out the global variable of "total_euro_value" so nothing goes spectacularly wrong. This also helps us enforce this value to be a float, somehow. Python can be weird...

    current_datetime = datetime.datetime.now() # get date time
    print "[+] Current Datetime: %s" %(current_datetime) # print it
    client = Client(creds.api_key, creds.api_secret) # create client object authenticated to coinbase api
    data = client.get_accounts() # get the account data as JSON
    total_euro_value = 0.00 # set our total euro value to 0 as a float.

Parsing the data (badly) and doing some maths.

So the next bit is a fucking glorious kludge, to be honest. I was having some issues cleanly parsing the JSON, so what I did next is a bit stupid to be perfectly honest. However, it works. I'll tidy it up later.

So the JSON output doesn't seem to order the currencies predictably, and I was getting sick of its shit, so I basically walk over the dicts we care about (0, 1, 2, 3) and dynamically extract the currency. Its a hack, but it works.

For each currency, we add its worth in euros to the "total_euro_value" variable as a float, so that it gets updated. This allows us to "value" our account easily.

    for x in range(0,4): # there are 4 dicts we care about.
        currency_type = data[x]["balance"]["currency"] # extract the currency type from the one we are parsing
        currency_amount = data[x]["balance"]["amount"] # extract the balance in cryptocurrency from it
        euros_amount = data[x]["native_balance"]["amount"] # extract its value in euros...
        print "[+] You have %s %s valued at %s euros" %(currency_amount, currency_type, euros_amount) # print it
        total_euro_value = total_euro_value + float(euros_amount) # add the euro value to total euro value
    print "[+] Total euro worth is: %f " %(float(total_euro_value)) # print this

logger function

This is a stupid simple function to append the current date/time and the account value (in euros) to a CSV file. I use this to chart the growth of my "live assets" over time. Below is commented source code.

def logger(date_time, total_euro_value): # define function "logger" with args date_time and total_euro_value
    f = open("log.csv", "a") # open log.csv in append mode as a file object named "f"
    message = "%s,%f\n" %(date_time, total_euro_value) # construct a message to write. the datetime as string, value as float. add a newline to the end.
    f.write(message) # write this to the file
    f.close() # close the file

Ideas?

So the next steps is having it also log the BTC/LTC/ETH/EUR balances to the file, along with their respective values in euros, so I can do better charting. It currently prints these out, but I haven't decided how to log them yet.

Let me know if this kind of thing is useful for you :) Also, I might extend this to use more exchanges at some point later on...

Sort:  

Good post 👊

This post has received a 0.39 % upvote from @drotto thanks to: @banjo.